Skip to content

Commit

Permalink
Support non-tty systems (CI)
Browse files Browse the repository at this point in the history
  • Loading branch information
hranicka committed Sep 3, 2018
1 parent a386c2d commit 5892ddb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Core/Client/Docker.php
Expand Up @@ -231,6 +231,11 @@ public function exec($command, array $args, $service)
$args[] = $service;
$args[] = $command;

// Disable TTY if is not supported by the host (CI)
if (!$this->runner->hasTty()) {
array_unshift($args, '-T');
}

return $this->perform('exec', '', $args);
}

Expand Down
27 changes: 25 additions & 2 deletions src/Core/ProcessRunner.php
Expand Up @@ -20,10 +20,12 @@ class ProcessRunner
public function run($command, $envVars)
{
$process = new Process(escapeshellcmd($command), null, $envVars);
if ('WIN' !== strtoupper(substr(PHP_OS, 0, 3))) {
$process->setTimeout(2 * 3600);

if ($this->hasTty()) {
$process->setTty(true);
}
$process->setTimeout(2 * 3600);

try {
return $process->mustRun();
} catch (ProcessFailedException $e) {
Expand All @@ -39,4 +41,25 @@ public function run($command, $envVars)

return null;
}

/**
* @return bool
*/
public function hasTty()
{
// Extracted from \Symfony\Component\Process\Process::setTty
if ('\\' === \DIRECTORY_SEPARATOR) {
// Windows platform does not have TTY
$isTtySupported = false;
} else {
// TTY mode requires /dev/tty to be read/writable.
$isTtySupported = (bool) @proc_open('echo 1 >/dev/null', [
['file', '/dev/tty', 'r'],
['file', '/dev/tty', 'w'],
['file', '/dev/tty', 'w'],
], $pipes);
}

return $isTtySupported;
}
}
4 changes: 4 additions & 0 deletions tests/Tests/Unit/TestCase.php
Expand Up @@ -144,6 +144,10 @@ function () {
)
);

$processRunnerMock
->method('hasTty')
->willReturn(true);

return new Docker($options, $processRunnerMock);
}

Expand Down

0 comments on commit 5892ddb

Please sign in to comment.