From 12f641a4095e2115da125b5250babdec889915cc Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 17 Jan 2018 11:14:35 -0800 Subject: [PATCH] Debugger: Default source context and default uniquifier (#839) * 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. --- src/Debugger/Daemon.php | 21 +++++++++- tests/snippets/Debugger/DaemonTest.php | 4 +- tests/unit/Debugger/DaemonTest.php | 38 +++++++++++++++---- tests/unit/Debugger/example/file.php | 3 ++ .../Debugger/example/nested/folder/file2.php | 3 ++ 5 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 tests/unit/Debugger/example/file.php create mode 100644 tests/unit/Debugger/example/nested/folder/file2.php diff --git a/src/Debugger/Daemon.php b/src/Debugger/Daemon.php index c5595cbd53e0..1eb193da0a58 100644 --- a/src/Debugger/Daemon.php +++ b/src/Debugger/Daemon.php @@ -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, @@ -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']; @@ -168,7 +185,7 @@ private function defaultExtSourceContext() if (file_exists($sourceContextFile)) { return json_decode(file_get_contents($sourceContextFile), true); } else { - return null; + return []; } } } diff --git a/tests/snippets/Debugger/DaemonTest.php b/tests/snippets/Debugger/DaemonTest.php index 3d0a41551543..1509030bddeb 100644 --- a/tests/snippets/Debugger/DaemonTest.php +++ b/tests/snippets/Debugger/DaemonTest.php @@ -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()); @@ -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'); diff --git a/tests/unit/Debugger/DaemonTest.php b/tests/unit/Debugger/DaemonTest.php index 03609250bb1f..12a7bae8013f 100644 --- a/tests/unit/Debugger/DaemonTest.php +++ b/tests/unit/Debugger/DaemonTest.php @@ -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(), @@ -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(), @@ -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(), @@ -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 = [ diff --git a/tests/unit/Debugger/example/file.php b/tests/unit/Debugger/example/file.php new file mode 100644 index 000000000000..483e80a1ddf2 --- /dev/null +++ b/tests/unit/Debugger/example/file.php @@ -0,0 +1,3 @@ +