-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Debugger: Validate breakpoint file and line (#834)
* Validate the breakpoint file/line early * case insensitive file extension validation * Fix license year on new file * If the line starts with *, check to see if it's in a multiline comment. This won't catch comments that are multiline that don't begin with *. * Cleanup case statements * Add comments about this validation not being exhaustive
- Loading branch information
Showing
2 changed files
with
313 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Tests\Unit\Debugger; | ||
|
||
use Google\Cloud\Debugger\StatusMessage; | ||
use Google\Cloud\Debugger\Breakpoint; | ||
use Google\Cloud\Debugger\SourceLocation; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @group debugger | ||
*/ | ||
class BreakpointValidationTest extends TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
if (!extension_loaded('stackdriver_debugger')) { | ||
$this->markTestSkipped('Breakpoint validation requires stackdriver_debugger extension'); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider conditionsToTest | ||
*/ | ||
public function testValidatesCondition($condition, $expectedValid) | ||
{ | ||
$breakpoint = new Breakpoint([ | ||
'location' => [ | ||
'path' => __FILE__, | ||
'line' => __LINE__ | ||
], | ||
'condition' => $condition | ||
]); | ||
$this->assertEquals($expectedValid, $breakpoint->validate()); | ||
if (!$expectedValid) { | ||
$status = $breakpoint->status()->jsonSerialize(); | ||
$this->assertTrue($status['isError']); | ||
$this->assertEquals(StatusMessage::REFERENCE_BREAKPOINT_CONDITION, $status['refersTo']); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider conditionsToTest | ||
*/ | ||
public function testValidatesExpression($expression, $expectedValid) | ||
{ | ||
$breakpoint = new Breakpoint([ | ||
'location' => [ | ||
'path' => __FILE__, | ||
'line' => __LINE__ | ||
], | ||
'expressions' => [$expression] | ||
]); | ||
$this->assertEquals($expectedValid, $breakpoint->validate()); | ||
if (!$expectedValid) { | ||
$status = $breakpoint->status()->jsonSerialize(); | ||
$this->assertTrue($status['isError']); | ||
$this->assertEquals(StatusMessage::REFERENCE_BREAKPOINT_EXPRESSION, $status['refersTo']); | ||
} | ||
} | ||
|
||
public function conditionsToTest() | ||
{ | ||
return [ | ||
['3 + 3', true], | ||
['3 + 3 == 6', true], | ||
['foo(3)', false], | ||
['abs(-3)', false], | ||
['$foo->bar', true], | ||
['$foo->bar()', false] | ||
]; | ||
} | ||
|
||
public function testValidatesMissingFile() | ||
{ | ||
$breakpoint = new Breakpoint([ | ||
'location' => [ | ||
'path' => __DIR__ . '/missing_file.php', | ||
'line' => __LINE__ | ||
] | ||
]); | ||
$this->assertFalse($breakpoint->validate()); | ||
$status = $breakpoint->status()->jsonSerialize(); | ||
$this->assertTrue($status['isError']); | ||
$this->assertEquals( | ||
StatusMessage::REFERENCE_BREAKPOINT_SOURCE_LOCATION, | ||
$status['refersTo'] | ||
); | ||
} | ||
|
||
public function testValidatesFileType() | ||
{ | ||
$breakpoint = new Breakpoint([ | ||
'location' => [ | ||
'path' => __DIR__ . '/../../../composer.json', | ||
'line' => __LINE__ | ||
] | ||
]); | ||
$this->assertFalse($breakpoint->validate()); | ||
$status = $breakpoint->status()->jsonSerialize(); | ||
$this->assertTrue($status['isError']); | ||
$this->assertEquals( | ||
StatusMessage::REFERENCE_BREAKPOINT_SOURCE_LOCATION, | ||
$status['refersTo'] | ||
); | ||
} | ||
|
||
public function testValidatesEmptyLine() | ||
{ | ||
$breakpoint = new Breakpoint([ | ||
'location' => [ | ||
'path' => __FILE__, | ||
'line' => __LINE__ - 6 | ||
] | ||
]); | ||
$this->assertFalse($breakpoint->validate()); | ||
$status = $breakpoint->status()->jsonSerialize(); | ||
$this->assertTrue($status['isError']); | ||
$this->assertEquals( | ||
StatusMessage::REFERENCE_BREAKPOINT_SOURCE_LOCATION, | ||
$status['refersTo'] | ||
); | ||
} | ||
|
||
public function testValidatesCommentLine() | ||
{ | ||
$breakpoint = new Breakpoint([ | ||
'location' => [ | ||
'path' => __FILE__, | ||
'line' => 2 | ||
] | ||
]); | ||
$this->assertFalse($breakpoint->validate()); | ||
$status = $breakpoint->status()->jsonSerialize(); | ||
$this->assertTrue($status['isError']); | ||
$this->assertEquals( | ||
StatusMessage::REFERENCE_BREAKPOINT_SOURCE_LOCATION, | ||
$status['refersTo'] | ||
); | ||
} | ||
|
||
public function testValidatesNonCommentMultiplication() | ||
{ | ||
$value = 6 | ||
* 3; | ||
|
||
$breakpoint = new Breakpoint([ | ||
'location' => [ | ||
'path' => __FILE__, | ||
'line' => __LINE__ - 5 | ||
] | ||
]); | ||
$this->assertTrue($breakpoint->validate()); | ||
} | ||
} |