From e02f6a2b121490acbbdda7d5e6d4d6dc8d99bcad Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 18 Jan 2018 15:40:45 -0800 Subject: [PATCH 1/3] Detect default source context from source-context.json source-contexts.json is deprecated and should not be used. Currently, gcloud produces both, but will only create the singular form in the future. --- src/Debugger/Daemon.php | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Debugger/Daemon.php b/src/Debugger/Daemon.php index 1eb193da0a58..b75bc92a233b 100644 --- a/src/Debugger/Daemon.php +++ b/src/Debugger/Daemon.php @@ -79,24 +79,30 @@ public function __construct($sourceRoot, array $options = []) ? $options['client'] : new DebuggerClient(); - $this->sourceRoot = realpath($sourceRoot); + $options += [ + 'sourceContext' => [], + 'extSourceContext' => [], + 'uniquifier' => null, + 'description' => null + ]; - $extSourceContext = array_key_exists('extSourceContext', $options) - ? [$options['extSourceContext']] - : $this->defaultExtSourceContext(); + $this->sourceRoot = realpath($sourceRoot); - $uniquifier = array_key_exists('uniquifier', $options) - ? $options['uniquifier'] - : $this->defaultUniquifier(); + $sourceContext = $options['sourceContext'] ?: $this->defaultSourceContext(); + $extSourceContext = $options['extSourceContext']; + if (!$extSourceContext && $sourceContext) { + $extSourceContext = [ + 'context' => $sourceContext + ]; + } - $description = array_key_exists('description', $options) - ? $options['description'] - : $this->defaultDescription(); + $uniquifier = $options['uniquifier'] ?: $this->defaultUniquifier(); + $description = $options['description'] ?: $this->defaultDescription(); $this->debuggee = $client->debuggee(null, [ 'uniquifier' => $uniquifier, 'description' => $description, - 'extSourceContexts' => $extSourceContext + 'extSourceContexts' => $extSourceContext ? [$extSourceContext] : [] ]); $this->debuggee->register(); @@ -179,13 +185,12 @@ private function defaultDescription() return gethostname() . ' - ' . getcwd(); } - private function defaultExtSourceContext() + private function defaultSourceContext() { - $sourceContextFile = $this->sourceRoot . '/source-contexts.json'; + $sourceContextFile = implode(DIRECTOR_SEPARATOR, [$this->sourceRoot, 'source-context.json']); if (file_exists($sourceContextFile)) { return json_decode(file_get_contents($sourceContextFile), true); - } else { - return []; } + return []; } } From 6ae604cb50b9e7dc6390710184130941eccc8cb0 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 18 Jan 2018 15:50:58 -0800 Subject: [PATCH 2/3] Add test for reading source-context.json file --- src/Debugger/Daemon.php | 2 +- tests/unit/Debugger/DaemonTest.php | 23 ++++++++++++++++++- .../unit/Debugger/example/source-context.json | 6 +++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/unit/Debugger/example/source-context.json diff --git a/src/Debugger/Daemon.php b/src/Debugger/Daemon.php index b75bc92a233b..f7f2f5e99527 100644 --- a/src/Debugger/Daemon.php +++ b/src/Debugger/Daemon.php @@ -187,7 +187,7 @@ private function defaultDescription() private function defaultSourceContext() { - $sourceContextFile = implode(DIRECTOR_SEPARATOR, [$this->sourceRoot, 'source-context.json']); + $sourceContextFile = implode(DIRECTORY_SEPARATOR, [$this->sourceRoot, 'source-context.json']); if (file_exists($sourceContextFile)) { return json_decode(file_get_contents($sourceContextFile), true); } diff --git a/tests/unit/Debugger/DaemonTest.php b/tests/unit/Debugger/DaemonTest.php index 12a7bae8013f..0a5f045ca97a 100644 --- a/tests/unit/Debugger/DaemonTest.php +++ b/tests/unit/Debugger/DaemonTest.php @@ -102,7 +102,7 @@ public function testSpecifyExtSourceContext() ]); } - public function testDefaultSourceContext() + public function testEmptyDefaultSourceContext() { $this->debuggee->register(Argument::any())->shouldBeCalled(); $this->client->debuggee(null, Argument::withEntry('extSourceContexts', [])) @@ -114,6 +114,27 @@ public function testDefaultSourceContext() ]); } + public function testDefaultSourceContext() + { + $expectedSourceContext = [ + 'context' => [ + 'git' => [ + 'revisionId' => '81b20d097da02ebb6c6fdfbf6900c67a90f2c54b', + 'url' => 'https://github.com/GoogleCloudPlatform/google-cloud-php.git' + ] + ] + ]; + $this->debuggee->register(Argument::any())->shouldBeCalled(); + $this->client->debuggee(null, Argument::withEntry('extSourceContexts', [$expectedSourceContext])) + ->willReturn($this->debuggee->reveal())->shouldBeCalled(); + + $root = implode(DIRECTORY_SEPARATOR, [__DIR__, 'example']); + $daemon = new Daemon($root, [ + 'client' => $this->client->reveal(), + 'storage' => $this->storage->reveal() + ]); + } + public function testFetchesBreakpoints() { $resp = [ diff --git a/tests/unit/Debugger/example/source-context.json b/tests/unit/Debugger/example/source-context.json new file mode 100644 index 000000000000..0ca647d8f1ed --- /dev/null +++ b/tests/unit/Debugger/example/source-context.json @@ -0,0 +1,6 @@ +{ + "git": { + "revisionId": "81b20d097da02ebb6c6fdfbf6900c67a90f2c54b", + "url": "https://github.com/GoogleCloudPlatform/google-cloud-php.git" + } +} From eb43674727a55a35b2b5239f585b4d21ec736820 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 19 Jan 2018 10:07:10 -0800 Subject: [PATCH 3/3] Rename example->data folder for debugger tests --- tests/unit/Debugger/DaemonTest.php | 5 +++-- tests/unit/Debugger/{example => data}/file.php | 0 .../unit/Debugger/{example => data}/nested/folder/file2.php | 0 tests/unit/Debugger/{example => data}/source-context.json | 0 4 files changed, 3 insertions(+), 2 deletions(-) rename tests/unit/Debugger/{example => data}/file.php (100%) rename tests/unit/Debugger/{example => data}/nested/folder/file2.php (100%) rename tests/unit/Debugger/{example => data}/source-context.json (100%) diff --git a/tests/unit/Debugger/DaemonTest.php b/tests/unit/Debugger/DaemonTest.php index 0a5f045ca97a..bf93b114f22a 100644 --- a/tests/unit/Debugger/DaemonTest.php +++ b/tests/unit/Debugger/DaemonTest.php @@ -61,7 +61,8 @@ public function testGeneratesDefaultUniquifier() return preg_match('/[a-z0-9]{32}/', $options['uniquifier']); }))->willReturn($this->debuggee->reveal())->shouldBeCalled(); - $daemon = new Daemon(__DIR__ . '/example', [ + $root = implode(DIRECTORY_SEPARATOR, [__DIR__, 'data']); + $daemon = new Daemon($root, [ 'client' => $this->client->reveal(), 'storage' => $this->storage->reveal() ]); @@ -128,7 +129,7 @@ public function testDefaultSourceContext() $this->client->debuggee(null, Argument::withEntry('extSourceContexts', [$expectedSourceContext])) ->willReturn($this->debuggee->reveal())->shouldBeCalled(); - $root = implode(DIRECTORY_SEPARATOR, [__DIR__, 'example']); + $root = implode(DIRECTORY_SEPARATOR, [__DIR__, 'data']); $daemon = new Daemon($root, [ 'client' => $this->client->reveal(), 'storage' => $this->storage->reveal() diff --git a/tests/unit/Debugger/example/file.php b/tests/unit/Debugger/data/file.php similarity index 100% rename from tests/unit/Debugger/example/file.php rename to tests/unit/Debugger/data/file.php diff --git a/tests/unit/Debugger/example/nested/folder/file2.php b/tests/unit/Debugger/data/nested/folder/file2.php similarity index 100% rename from tests/unit/Debugger/example/nested/folder/file2.php rename to tests/unit/Debugger/data/nested/folder/file2.php diff --git a/tests/unit/Debugger/example/source-context.json b/tests/unit/Debugger/data/source-context.json similarity index 100% rename from tests/unit/Debugger/example/source-context.json rename to tests/unit/Debugger/data/source-context.json