Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from niels-nijens/non-interactive-mode
Browse files Browse the repository at this point in the history
Added environment variables to enforce non-interactive mode when executing git commands
  • Loading branch information
niels-nijens committed May 14, 2016
2 parents d393b64 + 3ae19e9 commit bf0dd70
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 49 deletions.
23 changes: 18 additions & 5 deletions src/Adapter/GitAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function supportsRepository()
return true;
}

$result = $this->processExecutor->execute(sprintf('git ls-remote --heads %s', ProcessUtils::escapeArgument($this->repositoryUrl)));
$result = $this->processExecutor->execute(sprintf('git ls-remote --heads %s', ProcessUtils::escapeArgument($this->repositoryUrl)), null, $this->getEnvironmentVariables());
if ($result->isSuccessful()) {
return true;
}
Expand All @@ -40,7 +40,7 @@ public function getBranches()
{
$branches = array();

$result = $this->processExecutor->execute(sprintf('git ls-remote --heads %s', ProcessUtils::escapeArgument($this->repositoryUrl)));
$result = $this->processExecutor->execute(sprintf('git ls-remote --heads %s', ProcessUtils::escapeArgument($this->repositoryUrl)), null, $this->getEnvironmentVariables());
if ($result->isSuccessful()) {
foreach ($result->getOutputAsArray() as $branch) {
$matches = array();
Expand All @@ -60,7 +60,7 @@ public function getTags()
{
$tags = array();

$result = $this->processExecutor->execute(sprintf('git ls-remote --tags %s', ProcessUtils::escapeArgument($this->repositoryUrl)));
$result = $this->processExecutor->execute(sprintf('git ls-remote --tags %s', ProcessUtils::escapeArgument($this->repositoryUrl)), null, $this->getEnvironmentVariables());
if ($result->isSuccessful()) {
foreach ($result->getOutputAsArray() as $tag) {
$matches = array();
Expand All @@ -82,15 +82,28 @@ public function checkout($version)

$escapedVersion = ProcessUtils::escapeArgument($version);
if ($this->processExecutor->isDirectory($this->repositoryDirectory) && $this->processExecutor->execute('git rev-parse --is-inside-work-tree', $this->repositoryDirectory)->isSuccessful()) {
$checkoutSuccesful = ($this->processExecutor->execute('git fetch', $this->repositoryDirectory)->isSuccessful() && $this->processExecutor->execute(sprintf('git checkout %s', $escapedVersion), $this->repositoryDirectory)->isSuccessful() && $this->processExecutor->execute('git pull', $this->repositoryDirectory)->isSuccessful());
$checkoutSuccesful = ($this->processExecutor->execute('git fetch', $this->repositoryDirectory, $this->getEnvironmentVariables())->isSuccessful() && $this->processExecutor->execute(sprintf('git checkout %s', $escapedVersion), $this->repositoryDirectory, $this->getEnvironmentVariables())->isSuccessful() && $this->processExecutor->execute('git pull', $this->repositoryDirectory, $this->getEnvironmentVariables())->isSuccessful());
} else {
$escapedRepositoryUrl = ProcessUtils::escapeArgument($this->repositoryUrl);
$escapedRepositoryDirectory = ProcessUtils::escapeArgument($this->repositoryDirectory);

$result = $this->processExecutor->execute(sprintf('git clone -b %s --single-branch %s %s', $escapedVersion, $escapedRepositoryUrl, $escapedRepositoryDirectory));
$result = $this->processExecutor->execute(sprintf('git clone -b %s --single-branch %s %s', $escapedVersion, $escapedRepositoryUrl, $escapedRepositoryDirectory), null, $this->getEnvironmentVariables());
$checkoutSuccesful = $result->isSuccessful();
}

return $checkoutSuccesful;
}

/**
* Returns the environment variables for Git commands.
*
* @return array
*/
private function getEnvironmentVariables()
{
return array(
'GIT_TERMINAL_PROMPT' => '0',
'GIT_ASKPASS' => 'echo',
);
}
}
4 changes: 2 additions & 2 deletions src/Process/ProcessExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function getWorkingDirectory()
/**
* {@inheritdoc}
*/
public function execute($command, $workingDirectory = null)
public function execute($command, $workingDirectory = null, array $environmentVariables = null)
{
if ($workingDirectory === null) {
$workingDirectory = $this->getWorkingDirectory();
}

$process = new Process($command, $workingDirectory);
$process = new Process($command, $workingDirectory, $environmentVariables);
$process->setTimeout(300);
$process->run();

Expand Down
3 changes: 2 additions & 1 deletion src/Process/ProcessExecutorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ public function isDirectory($path);
*
* @param string $command
* @param string|null $workingDirectory
* @param array|null $environmentVariables
*
* @return ProcessExecutionResult
*/
public function execute($command, $workingDirectory = null);
public function execute($command, $workingDirectory = null, array $environmentVariables = null);

/**
* Returns the ProcessExecutionResult instance of the last executed command.
Expand Down
95 changes: 54 additions & 41 deletions tests/Adapter/GitAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,50 +85,55 @@ public function provideTestSupportsRepository()
{
$provideTest = array();

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo('git --version'))
->willReturn(new ProcessExecutionResult(1, '', ''));
$provideTest[] = array('https://github.com/accompli/chrono.git', $processExecutorMock, false);

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo('git --version'))
->willReturn(new ProcessExecutionResult(0, '', ''));
$provideTest[] = array('https://github.com/accompli/chrono.git', $processExecutorMock, true);

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo('git --version'))
->willReturn(new ProcessExecutionResult(0, '', ''));
$provideTest[] = array('[email protected]:accompli/chrono.git', $processExecutorMock, true);

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->exactly(2))
->method('execute')
->withConsecutive(
array($this->equalTo('git --version')),
array($this->equalTo(sprintf('git ls-remote --heads %s', ProcessUtils::escapeArgument('[email protected]:accompli/chrono'))))
array($this->equalTo('git --version')),
array($this->equalTo(sprintf('git ls-remote --heads %s', ProcessUtils::escapeArgument('[email protected]:accompli/chrono'))))
)
->willReturnOnConsecutiveCalls(
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', '')
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', '')
);
$provideTest[] = array('[email protected]:accompli/chrono', $processExecutorMock, true);

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->exactly(2))
->method('execute')
->withConsecutive(
array($this->equalTo('git --version')),
array($this->equalTo(sprintf('git ls-remote --heads %s', ProcessUtils::escapeArgument('svn+ssh://example.com/accompli/chrono'))))
array($this->equalTo('git --version')),
array($this->equalTo(sprintf('git ls-remote --heads %s', ProcessUtils::escapeArgument('svn+ssh://example.com/accompli/chrono'))), $this->equalTo(null), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
)
->willReturnOnConsecutiveCalls(
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(1, '', '')
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(1, '', '')
);
$provideTest[] = array('svn+ssh://example.com/accompli/chrono', $processExecutorMock, false);

Expand All @@ -146,17 +151,19 @@ public function provideTestGetBranches()

$provideTest = array();

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo($branchesCommand))
->with($this->equalTo($branchesCommand), $this->equalTo(null), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
->willReturn(new ProcessExecutionResult(1, '', ''));
$provideTest[] = array($processExecutorMock, array());

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo($branchesCommand))
->with($this->equalTo($branchesCommand), $this->equalTo(null), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
->willReturn(new ProcessExecutionResult(0, "6f8ce5e29cdcd894d6b7aaeb590e656f9348ac3a refs/heads/master\n162eb4911ffc5e30496b5ed6c40c5182c1fd22d8 refs/heads/1.0\n", ''));
$provideTest[] = array($processExecutorMock, array('6f8ce5e29cdcd894d6b7aaeb590e656f9348ac3a' => 'master', '162eb4911ffc5e30496b5ed6c40c5182c1fd22d8' => '1.0'));

Expand All @@ -174,17 +181,19 @@ public function provideTestGetTags()

$provideTest = array();

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo($tagsCommand))
->with($this->equalTo($tagsCommand), $this->equalTo(null), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
->willReturn(new ProcessExecutionResult(1, '', ''));
$provideTest[] = array($processExecutorMock, array());

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo($tagsCommand))
->with($this->equalTo($tagsCommand), $this->equalTo(null), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
->willReturn(new ProcessExecutionResult(0, "bd82122129c6da32086988a89f8820e05128f1c9 refs/tags/0.1.0^{}\n3c6a664eea344ce2b9f1ce49922c4f12f57fca82 refs/tags/0.1.1^{}\n", ''));
$provideTest[] = array($processExecutorMock, array('bd82122129c6da32086988a89f8820e05128f1c9' => '0.1.0', '3c6a664eea344ce2b9f1ce49922c4f12f57fca82' => '0.1.1'));

Expand All @@ -203,61 +212,65 @@ public function provideTestCheckout()

$provideTest = array();

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('isDirectory')
->willReturn(false);
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo($cloneCommand))
->with($this->equalTo($cloneCommand), $this->equalTo(null), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
->willReturn(new ProcessExecutionResult(1, '', ''));
$provideTest[] = array($processExecutorMock, false);

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('isDirectory')
->willReturn(true);
$processExecutorMock->expects($this->exactly(3))
->method('execute')
->withConsecutive(
array($this->equalTo('git rev-parse --is-inside-work-tree'), $this->equalTo('/git/working-directory')),
array($this->equalTo('git fetch')),
array($this->equalTo($checkoutCommand), $this->equalTo('/git/working-directory'))
array($this->equalTo('git rev-parse --is-inside-work-tree'), $this->equalTo('/git/working-directory'), $this->equalTo(null)),
array($this->equalTo('git fetch'), $this->equalTo('/git/working-directory'), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo'))),
array($this->equalTo($checkoutCommand), $this->equalTo('/git/working-directory'), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
)
->willReturnOnConsecutiveCalls(
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(1, '', '')
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(1, '', '')
);
$provideTest[] = array($processExecutorMock, false);

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('isDirectory')
->willReturn(false);
$processExecutorMock->expects($this->once())
->method('execute')
->with($this->equalTo($cloneCommand))
->with($this->equalTo($cloneCommand), $this->equalTo(null), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
->willReturn(new ProcessExecutionResult(0, '', ''));
$provideTest[] = array($processExecutorMock, true);

$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')->getMock();
$processExecutorMock = $this->getMockBuilder('Accompli\Chrono\Process\ProcessExecutorInterface')
->getMock();
$processExecutorMock->expects($this->once())
->method('isDirectory')
->willReturn(true);
$processExecutorMock->expects($this->exactly(4))
->method('execute')
->withConsecutive(
array($this->equalTo('git rev-parse --is-inside-work-tree'), $this->equalTo('/git/working-directory')),
array($this->equalTo('git fetch')),
array($this->equalTo($checkoutCommand), $this->equalTo('/git/working-directory')),
array($this->equalTo('git pull'), $this->equalTo('/git/working-directory'))
array($this->equalTo('git rev-parse --is-inside-work-tree'), $this->equalTo('/git/working-directory'), $this->equalTo(null)),
array($this->equalTo('git fetch'), $this->equalTo('/git/working-directory'), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo'))),
array($this->equalTo($checkoutCommand), $this->equalTo('/git/working-directory'), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo'))),
array($this->equalTo('git pull'), $this->equalTo('/git/working-directory'), $this->equalTo(array('GIT_TERMINAL_PROMPT' => '0', 'GIT_ASKPASS' => 'echo')))
)
->willReturnOnConsecutiveCalls(
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', '')
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', ''),
new ProcessExecutionResult(0, '', '')
);
$provideTest[] = array($processExecutorMock, true);

Expand Down
16 changes: 16 additions & 0 deletions tests/Process/ProcessExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ public function testExecute()
$this->assertEquals('', $processExecutionResult->getErrorOutput());
}

/**
* Tests if ProcessExecutor::execute returns the expected result when using an environment variable.
*/
public function testExecuteWithEnvironmentVariable()
{
$command = 'echo $FOO';
if (DIRECTORY_SEPARATOR === '\\') {
$command = 'echo %FOO%';
}

$processExecutor = new ProcessExecutor();
$processExecutionResult = $processExecutor->execute($command, null, array('FOO' => 'Bar'));

$this->assertSame('Bar'.PHP_EOL, $processExecutionResult->getOutput());
}

/**
* Tests if ProcessExecutor::getLastProcessExecutionResult returns the same ProcessExecutionResult instance as ProcessExecutor::execute.
*/
Expand Down

0 comments on commit bf0dd70

Please sign in to comment.