Skip to content

Commit

Permalink
Add method to retrieve the working directory in ProcessOutputInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
ptlis committed Nov 5, 2020
1 parent 326cd17 commit 92a0b20
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 53 deletions.
9 changes: 9 additions & 0 deletions src/Interfaces/ProcessOutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ public function getExitCode(): int;
* @return string
*/
public function getExecutedCommand(): string;

/**
* Get the working directory that command was executed in.
*
* This is here purely to make debugging commands easier.
*
* @return string
*/
public function getWorkingDirectory(): string;
}
5 changes: 3 additions & 2 deletions src/Mock/MockCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,14 @@ public function buildCommand(): CommandInterface
* @param string $stdOut
* @param string $stdErr
* @param string $command
* @param string $workingDirectory
*
* @return $this
*/
public function addMockResult(int $exitCode, string $stdOut, string $stdErr, string $command): CommandBuilderInterface
public function addMockResult(int $exitCode, string $stdOut, string $stdErr, string $command, string $workingDirectory): CommandBuilderInterface
{
$mockResultList = $this->mockResultList;
$mockResultList[] = new ProcessOutput($exitCode, $stdOut, $stdErr, $command);
$mockResultList[] = new ProcessOutput($exitCode, $stdOut, $stdErr, $command, $workingDirectory);

return new MockCommandBuilder(
$mockResultList,
Expand Down
2 changes: 1 addition & 1 deletion src/Mock/MockProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function (TimerInterface $timer) use ($eventLoop, $deferred, &$fullStdOut, &$ful
// Process has terminated
if (!$this->isRunning()) {
$eventLoop->cancelTimer($timer);
$output = new ProcessOutput($this->result->getExitCode(), $fullStdOut, $fullStdErr, $this->command);
$output = new ProcessOutput($this->result->getExitCode(), $fullStdOut, $fullStdErr, $this->command, '.');

// Resolve or reject promise
if (0 === $output->getExitCode()) {
Expand Down
7 changes: 6 additions & 1 deletion src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ final class Process implements ProcessInterface
/** @var EnvironmentInterface */
private $environment;

/** @var string */
private $cwdOverride;

/** @var string[] */
private $envVarList;

Expand Down Expand Up @@ -74,6 +77,7 @@ public function __construct(
ProcessObserverInterface $observer = null
) {
$this->environment = $environment;
$this->cwdOverride = $cwdOverride;
$this->observer = $observer;
$this->envVarList = $envVarList;
if (is_null($this->observer)) {
Expand Down Expand Up @@ -283,7 +287,8 @@ private function getProcessOutput(): ProcessOutputInterface
$this->exitCode,
$this->fullStdOut,
$this->fullStdErr,
$envVarString . $this->getCommand()
$envVarString . $this->getCommand(),
$this->cwdOverride
);
$this->observer->processExited($this->pid, $this->output);
}
Expand Down
12 changes: 11 additions & 1 deletion src/ProcessOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ final class ProcessOutput implements ProcessOutputInterface
/** @var string */
private $command;

/** @var string */
private $workingDirectory;


public function __construct(
int $exitCode,
string $stdOut,
string $stdErr,
string $command
string $command,
string $workingDirectory
) {
$this->exitCode = $exitCode;
$this->stdOut = $stdOut;
$this->stdErr = $stdErr;
$this->command = $command;
$this->workingDirectory = $workingDirectory;
}

public function getStdOut(): string
Expand Down Expand Up @@ -70,6 +75,11 @@ public function getExecutedCommand(): string
return $this->command;
}

public function getWorkingDirectory(): string
{
return $this->workingDirectory;
}

/**
* Accepts console output as a string and returns an array of it split by newlines.
*/
Expand Down
23 changes: 15 additions & 8 deletions tests/Integration/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function testRun(): void
0,
'Test command' . PHP_EOL . 'if=/dev/sha1 of=/dev/sdb2' . PHP_EOL,
'',
'./tests/commands/unix/test_binary \'if=/dev/sha1 of=/dev/sdb2\''
'./tests/commands/unix/test_binary \'if=/dev/sha1 of=/dev/sdb2\'',
getcwd()
),
$command->runSynchronous()
);
Expand Down Expand Up @@ -73,7 +74,8 @@ public function testRunFromHome(): void
0,
'Test command' . PHP_EOL . 'if=/dev/sha1 of=/dev/sdb2' . PHP_EOL,
'',
'~/test_binary \'if=/dev/sha1 of=/dev/sdb2\''
'~/test_binary \'if=/dev/sha1 of=/dev/sdb2\'',
getcwd()
),
$command->runSynchronous()
);
Expand All @@ -85,9 +87,10 @@ public function testRunHomeCwd(): void
{
$originalPath = getenv('PATH');

$pathToCommand = realpath(getcwd() . '/tests/commands/unix');
$fakeHomePath = realpath(getcwd() . '/tests/commands/unix');

putenv('HOME=' . $pathToCommand);

putenv('HOME=' . $fakeHomePath);

$path = '~/sleep_binary';

Expand All @@ -104,7 +107,8 @@ public function testRunHomeCwd(): void
0,
'',
'',
'~/sleep_binary'
'~/sleep_binary',
$fakeHomePath . '/'
),
$command->runSynchronous()
);
Expand All @@ -129,7 +133,8 @@ public function testRunWithSleep(): void
0,
'',
'',
'./tests/commands/unix/sleep_binary'
'./tests/commands/unix/sleep_binary',
getcwd()
),
$command->runSynchronous()
);
Expand All @@ -155,7 +160,8 @@ public function testRunWithEnvVariable(): void
0,
'VALUE' . PHP_EOL,
'',
'TEST_VAR=\'VALUE\' ./tests/commands/unix/echo_env_binary'
'TEST_VAR=\'VALUE\' ./tests/commands/unix/echo_env_binary',
getcwd()
),
$command->runSynchronous()
);
Expand All @@ -178,7 +184,8 @@ public function testRunWithError(): void
5,
'',
'Fatal Error' . PHP_EOL,
'./tests/commands/unix/error_binary'
'./tests/commands/unix/error_binary',
getcwd()
),
$command->runSynchronous()
);
Expand Down
40 changes: 20 additions & 20 deletions tests/Unit/Mocks/MockCommandBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class MockCommandBuilderTest extends ptlisShellCommandTestcase
public function testMockCommandBuilderOne(): void
{
$builder = new MockCommandBuilder(
[new ProcessOutput(0, 'hello world', '', 'foo \'--foo bar\' \-d 10\' \'if=/bar\' \'wop\'')]
[new ProcessOutput(0, 'hello world', '', 'foo \'--foo bar\' \-d 10\' \'if=/bar\' \'wop\'', '.')]
);

$builder = $builder
->setCommand('foo')
->addMockResult(0, 'hello world', '', 'foo \'--foo bar\' \-d 10\' \'if=/bar\' \'wop\'')
->addMockResult(0, 'hello world', '', 'foo \'--foo bar\' \-d 10\' \'if=/bar\' \'wop\'', '.')
->addArgument('--foo bar')
->addArgument('-d 10')
->addArgument('if=/bar')
Expand All @@ -46,7 +46,7 @@ public function testMockCommandBuilderOne(): void
'wop'
],
[],
new ProcessOutput(0, 'hello world', '', 'foo \'--foo bar\' \-d 10\' \'if=/bar\' \'wop\'')
new ProcessOutput(0, 'hello world', '', 'foo \'--foo bar\' \-d 10\' \'if=/bar\' \'wop\'', '.')
);

$this->assertEquals(
Expand All @@ -60,7 +60,7 @@ public function testMockCommandBuilderOne(): void
);

$this->assertEquals(
new ProcessOutput(0, 'hello world', '', 'foo \'--foo bar\' \-d 10\' \'if=/bar\' \'wop\''),
new ProcessOutput(0, 'hello world', '', 'foo \'--foo bar\' \-d 10\' \'if=/bar\' \'wop\'', '.'),
$builtCommand->runSynchronous()
);
}
Expand All @@ -77,7 +77,7 @@ public function testMockCommandBuilderTwo(): void
'bat'
]
)
->addMockResult(1, 'hurray!', '', 'bar \'baz\' \'bat\'')
->addMockResult(1, 'hurray!', '', 'bar \'baz\' \'bat\'', '.')
->addEnvironmentVariable('key', 'value')
->addEnvironmentVariables(['test' => 'message'])
->buildCommand();
Expand All @@ -87,7 +87,7 @@ public function testMockCommandBuilderTwo(): void
'bar',
['baz', 'bat'],
[],
new ProcessOutput(1, 'hurray!', '', 'bar \'baz\' \'bat\''),
new ProcessOutput(1, 'hurray!', '', 'bar \'baz\' \'bat\'', '.'),
['key' => 'value', 'test' => 'message']
);

Expand All @@ -102,7 +102,7 @@ public function testMockCommandBuilderTwo(): void
);

$this->assertEquals(
new ProcessOutput(1, 'hurray!', '', 'bar \'baz\' \'bat\''),
new ProcessOutput(1, 'hurray!', '', 'bar \'baz\' \'bat\'', '.'),
$builtCommand->runSynchronous()
);

Expand All @@ -117,17 +117,17 @@ public function testMockCommandMultiUseOne(): void
$builder = new MockCommandBuilder();

$builtCommand1 = $builder
->addMockResult(1, 'hurray!', '', 'bar')
->addMockResult(1, 'hurray!', '', 'bar', '.')
->setCommand('bar')
->buildCommand();

$expectResult1 = new ProcessOutput(1, 'hurray!', '', 'bar');
$expectResult1 = new ProcessOutput(1, 'hurray!', '', 'bar', '.');
$expectCommand1 = new MockCommand(
new UnixEnvironment(),
'bar',
[],
[],
new ProcessOutput(1, 'hurray!', '', 'bar')
new ProcessOutput(1, 'hurray!', '', 'bar', '.')
);

$this->assertEquals(
Expand All @@ -137,16 +137,16 @@ public function testMockCommandMultiUseOne(): void

$builtCommand2 = $builder
->setCommand('baz')
->addMockResult(0, 'success', '', 'baz')
->addMockResult(0, 'success', '', 'baz', '.')
->buildCommand();

$expectResult2 = new ProcessOutput(0, 'success', '', 'baz');
$expectResult2 = new ProcessOutput(0, 'success', '', 'baz', '.');
$expectCommand2 = new MockCommand(
new UnixEnvironment(),
'baz',
[],
[],
new ProcessOutput(0, 'success', '', 'baz')
new ProcessOutput(0, 'success', '', 'baz', '.')
);

$this->assertEquals(
Expand All @@ -164,20 +164,20 @@ public function testMockCommandMultiUseTwo(): void
{
$builder = new MockCommandBuilder();
$primedBuilder = $builder
->addMockResult(1, 'hurray!', '', 'bar')
->addMockResult(0, 'success', '', 'baz');
->addMockResult(1, 'hurray!', '', 'bar', '.')
->addMockResult(0, 'success', '', 'baz', '.');

$builtCommand1 = $primedBuilder
->setCommand('bar')
->buildCommand();

$expectResult1 = new ProcessOutput(1, 'hurray!', '', 'bar');
$expectResult1 = new ProcessOutput(1, 'hurray!', '', 'bar', '.');
$expectCommand1 = new MockCommand(
new UnixEnvironment(),
'bar',
[],
[],
new ProcessOutput(1, 'hurray!', '', 'bar')
new ProcessOutput(1, 'hurray!', '', 'bar', '.')
);

$this->assertEquals(
Expand All @@ -189,13 +189,13 @@ public function testMockCommandMultiUseTwo(): void
->setCommand('baz')
->buildCommand();

$expectResult2 = new ProcessOutput(0, 'success', '', 'baz');
$expectResult2 = new ProcessOutput(0, 'success', '', 'baz', '.');
$expectCommand2 = new MockCommand(
new UnixEnvironment(),
'baz',
[],
[],
new ProcessOutput(0, 'success', '', 'baz')
new ProcessOutput(0, 'success', '', 'baz', '.')
);

$this->assertEquals(
Expand Down Expand Up @@ -347,7 +347,7 @@ public function testClearTwo(): void
$command = $builder
->setCommand('foo')
->addArgument('--test')
->addMockResult(0, 'bar', '', 'foo \'--test\'')
->addMockResult(0, 'bar', '', 'foo \'--test\'', '.')
->buildCommand();

$this->assertEquals(
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/Mocks/MockCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public function testRunSynchronous(): void
$path,
['foo'],
['--test=\'123\''],
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\''),
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\'', '.'),
['FOO' => 'bar']
);

$this->assertEquals('binary \'foo\' --test=\'123\'', $command->__toString());

$this->assertEquals(
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\''),
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\'', '.'),
$command->runSynchronous()
);
}
Expand All @@ -49,7 +49,7 @@ public function testRunPromiseSuccess(): void
$path,
['foo'],
['--test=\'123\''],
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\''),
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\'', '.'),
['FOO' => 'bar']
);

Expand All @@ -66,7 +66,7 @@ public function testRunPromiseSuccess(): void
function(ProcessOutput $result) use (&$successCalled) {
$successCalled = true;
$this->assertEquals(
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\''),
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\'', '.'),
$result
);
},
Expand All @@ -78,7 +78,7 @@ function(ProcessOutput $result) use (&$failureCalled) {
$eventLoop->run();

$this->assertEquals(
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\''),
new ProcessOutput(0, 'hello world', '', 'binary \'foo\' --test=\'123\'', '.'),
$command->runSynchronous()
);

Expand All @@ -95,7 +95,7 @@ public function testRunPromiseFailure(): void
$path,
['foo'],
['--test=\'123\''],
new ProcessOutput(1, 'error', '', ''),
new ProcessOutput(1, 'error', '', '', '.'),
['FOO' => 'bar']
);

Expand All @@ -115,7 +115,7 @@ function(ProcessOutput $result) use (&$successCalled) {
function(ProcessOutput $result) use (&$failureCalled) {
$failureCalled = true;
$this->assertEquals(
new ProcessOutput(1, 'error', '', ''),
new ProcessOutput(1, 'error', '', '', '.'),
$result
);
}
Expand All @@ -124,7 +124,7 @@ function(ProcessOutput $result) use (&$failureCalled) {
$eventLoop->run();

$this->assertEquals(
new ProcessOutput(1, 'error', '', ''),
new ProcessOutput(1, 'error', '', '', '.'),
$command->runSynchronous()
);

Expand Down
Loading

0 comments on commit 92a0b20

Please sign in to comment.