Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugger: detect default source context #853

Merged
merged 3 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions src/Debugger/Daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -179,13 +185,12 @@ private function defaultDescription()
return gethostname() . ' - ' . getcwd();
}

private function defaultExtSourceContext()
private function defaultSourceContext()
{
$sourceContextFile = $this->sourceRoot . '/source-contexts.json';
$sourceContextFile = implode(DIRECTORY_SEPARATOR, [$this->sourceRoot, 'source-context.json']);
if (file_exists($sourceContextFile)) {
return json_decode(file_get_contents($sourceContextFile), true);
} else {
return [];
}
return [];
}
}
26 changes: 24 additions & 2 deletions tests/unit/Debugger/DaemonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]);
Expand Down Expand Up @@ -102,7 +103,7 @@ public function testSpecifyExtSourceContext()
]);
}

public function testDefaultSourceContext()
public function testEmptyDefaultSourceContext()
{
$this->debuggee->register(Argument::any())->shouldBeCalled();
$this->client->debuggee(null, Argument::withEntry('extSourceContexts', []))
Expand All @@ -114,6 +115,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__, 'data']);
$daemon = new Daemon($root, [
'client' => $this->client->reveal(),
'storage' => $this->storage->reveal()
]);
}

public function testFetchesBreakpoints()
{
$resp = [
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/unit/Debugger/data/source-context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"git": {
"revisionId": "81b20d097da02ebb6c6fdfbf6900c67a90f2c54b",
"url": "https://github.com/GoogleCloudPlatform/google-cloud-php.git"
}
}