Skip to content

Commit

Permalink
Debugger: Default source context and default uniquifier (#839)
Browse files Browse the repository at this point in the history
* Fix default source context and default uniquifier.

The extSourceContext field should be empty when there is no
source_contexts.json file available.

The default uniquifier should be based off a SHA1 of available files to
debug (.php files). We add the file size as a proxy for detecting
changes.

* Add test for ensuring the default source context is empty

* Add test for recursively looking at source files to generate uniquifier

* Actually add the test for default uniquifier

* Fix snippet test. Source root needs to exist

* clean up prophecy arguments

* Fix default uniquifier test for windows.

The paths generated are different on windows so we should not check for
the exact value of the generated uniquifier.
  • Loading branch information
chingor13 authored and dwsupplee committed Jan 17, 2018
1 parent 8278c28 commit 12f641a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
21 changes: 19 additions & 2 deletions src/Debugger/Daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function __construct($sourceRoot, array $options = [])

$description = array_key_exists('description', $options)
? $options['description']
: $uniquifier;
: $this->defaultDescription();

$this->debuggee = $client->debuggee(null, [
'uniquifier' => $uniquifier,
Expand Down Expand Up @@ -155,6 +155,23 @@ private function setBreakpoints($breakpoints)
}

private function defaultUniquifier()
{
$dir = new \RecursiveDirectoryIterator($this->sourceRoot);
$iterator = new \RecursiveIteratorIterator($dir);
$regex = new \RegexIterator(
$iterator,
'/^.+\.php$/i',
\RecursiveRegexIterator::GET_MATCH
);

$files = array_keys(iterator_to_array($regex));
return sha1(implode(':', array_map(function ($filename) {
$relativeFilename = str_replace($this->sourceRoot, '', $filename);
return $relativeFilename . ':' . filesize($filename);
}, $files)));
}

private function defaultDescription()
{
if (isset($_SERVER['GAE_SERVICE'])) {
return $_SERVER['GAE_SERVICE'] . ' - ' . $_SERVER['GAE_VERSION'];
Expand All @@ -168,7 +185,7 @@ private function defaultExtSourceContext()
if (file_exists($sourceContextFile)) {
return json_decode(file_get_contents($sourceContextFile), true);
} else {
return null;
return [];
}
}
}
4 changes: 2 additions & 2 deletions tests/snippets/Debugger/DaemonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testClass()
'storage' => $this->storage->reveal()
];
$snippet = $this->snippetFromClass(Daemon::class);
$snippet->replace('new Daemon(\'/path/to/source/root\')', 'new Daemon(\'/path/to/source/root\', $options)');
$snippet->replace('new Daemon(\'/path/to/source/root\')', 'new Daemon(__DIR__, $options)');
$snippet->addLocal('options', $options);
$res = $snippet->invoke('daemon');
$this->assertInstanceOf(Daemon::class, $res->returnVal());
Expand All @@ -62,7 +62,7 @@ public function testRun()
'client' => $this->client->reveal(),
'storage' => $this->storage->reveal()
];
$daemon = new Daemon('/path', $options);
$daemon = new Daemon(__DIR__, $options);
$snippet = $this->snippetFromMethod(Daemon::class, 'run');
$snippet->addLocal('daemon', $daemon);
$res = $snippet->invoke('daemon');
Expand Down
38 changes: 30 additions & 8 deletions tests/unit/Debugger/DaemonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public function setUp()
public function testSpecifyUniquifier()
{
$this->debuggee->register(Argument::any())->shouldBeCalled();
$this->client->debuggee(null, Argument::that(function ($options) {
return $options['uniquifier'] == 'some uniquifier';
}))->willReturn($this->debuggee->reveal())->shouldBeCalled();
$this->client->debuggee(null, Argument::withEntry('uniquifier', 'some uniquifier'))
->willReturn($this->debuggee->reveal())->shouldBeCalled();

$daemon = new Daemon('.', [
'client' => $this->client->reveal(),
Expand All @@ -55,13 +54,25 @@ public function testSpecifyUniquifier()
]);
}

public function testSpecifyDescription()
public function testGeneratesDefaultUniquifier()
{
$this->debuggee->register(Argument::any())->shouldBeCalled();
$this->client->debuggee(null, Argument::that(function ($options) {
return $options['description'] == 'some description';
return preg_match('/[a-z0-9]{32}/', $options['uniquifier']);
}))->willReturn($this->debuggee->reveal())->shouldBeCalled();

$daemon = new Daemon(__DIR__ . '/example', [
'client' => $this->client->reveal(),
'storage' => $this->storage->reveal()
]);
}

public function testSpecifyDescription()
{
$this->debuggee->register(Argument::any())->shouldBeCalled();
$this->client->debuggee(null, Argument::withEntry('description', 'some description'))
->willReturn($this->debuggee->reveal())->shouldBeCalled();

$daemon = new Daemon('.', [
'client' => $this->client->reveal(),
'storage' => $this->storage->reveal(),
Expand All @@ -81,9 +92,8 @@ public function testSpecifyExtSourceContext()
'labels' => []
];
$this->debuggee->register(Argument::any())->shouldBeCalled();
$this->client->debuggee(null, Argument::that(function ($options) use ($context) {
return $options['extSourceContexts'] == [$context];
}))->willReturn($this->debuggee->reveal())->shouldBeCalled();
$this->client->debuggee(null, Argument::withEntry('extSourceContexts', [$context]))
->willReturn($this->debuggee->reveal())->shouldBeCalled();

$daemon = new Daemon('.', [
'client' => $this->client->reveal(),
Expand All @@ -92,6 +102,18 @@ public function testSpecifyExtSourceContext()
]);
}

public function testDefaultSourceContext()
{
$this->debuggee->register(Argument::any())->shouldBeCalled();
$this->client->debuggee(null, Argument::withEntry('extSourceContexts', []))
->willReturn($this->debuggee->reveal())->shouldBeCalled();

$daemon = new Daemon('.', [
'client' => $this->client->reveal(),
'storage' => $this->storage->reveal()
]);
}

public function testFetchesBreakpoints()
{
$resp = [
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/Debugger/example/file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

// This test php file would contain an app
3 changes: 3 additions & 0 deletions tests/unit/Debugger/example/nested/folder/file2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

// This test php file would contain an app

0 comments on commit 12f641a

Please sign in to comment.