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: Default source context and default uniquifier #839

Merged
merged 7 commits into from
Jan 17, 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
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));

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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()
{
$breakpoints = [
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