diff --git a/src/CLI/Console.php b/src/CLI/Console.php index 4b8af2a..43057bc 100644 --- a/src/CLI/Console.php +++ b/src/CLI/Console.php @@ -131,7 +131,7 @@ public static function exit(int $status = 0): void * @param int $timeout * @return int */ - public static function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1, callable $onProgress = null): int + public static function execute(string $cmd, string $stdin, string &$output, int $timeout = -1, callable $onProgress = null): int { $cmd = '( '.$cmd.' ) 3>/dev/null ; echo $? >&3'; @@ -142,8 +142,7 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri $pipes ); $start = \time(); - $stdout = ''; - $stderr = ''; + $output = ''; $status = ''; if (\is_resource($process)) { @@ -159,12 +158,19 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri while (\is_resource($process)) { $stdoutContents = \stream_get_contents($pipes[1]) ?: ''; $stderrContents = \stream_get_contents($pipes[2]) ?: ''; - if (isset($onProgress) && (! empty($stdoutContents) || ! empty($stderrContents))) { - $onProgress($stdoutContents, $stderrContents, $process); + + $outputContents = $stdoutContents ?? ''; + + if (! empty($stderrContents)) { + $separator = empty($outputContents) ? '' : "\n"; + $outputContents .= $separator.$stderrContents; + } + + if (isset($onProgress) && (! empty($outputContents))) { + $onProgress($outputContents, $process); } - $stdout .= $stdoutContents; - $stderr .= $stderrContents; + $output .= $outputContents; $status .= \stream_get_contents($pipes[3]); if ($timeout > 0 && \time() - $start > $timeout) { diff --git a/tests/CLI/ConsoleTest.php b/tests/CLI/ConsoleTest.php index d80582e..e37ec63 100755 --- a/tests/CLI/ConsoleTest.php +++ b/tests/CLI/ConsoleTest.php @@ -28,99 +28,80 @@ public function testLogs() public function testExecuteBasic() { - $stdout = ''; - $stderr = ''; + $output = ''; $stdin = ''; - $code = Console::execute('php -r "echo \'hello world\';"', $stdin, $stdout, $stderr, 10); + $code = Console::execute('php -r "echo \'hello world\';"', $stdin, $output, 10); - $this->assertEquals('', $stderr); - $this->assertEquals('hello world', $stdout); + $this->assertEquals('hello world', $output); $this->assertEquals(0, $code); } public function testExecuteStream() { - $stdout = ''; - $stderr = ''; + $output = ''; $stdin = ''; - $stdoutStream = ''; - $stderrStream = ''; - $code = Console::execute('printf 1 && sleep 1 && printf 2 && sleep 1 && printf 3 && sleep 1 && printf 4 && sleep 1 && printf 5', $stdin, $stdout, $stderr, 10, function ($stdout, $stderr) use (&$stdoutStream, &$stderrStream) { - $stdoutStream .= $stdout; - $stderrStream .= $stderr; + $outputStream = ''; + $code = Console::execute('printf 1 && sleep 1 && printf 2 && sleep 1 && printf 3 && sleep 1 && printf 4 && sleep 1 && printf 5', $stdin, $output, 10, function ($output) use (&$outputStream) { + $outputStream .= $output; }); - $this->assertEquals('', $stderr); - $this->assertEquals('12345', $stdout); - $this->assertEquals('', $stderrStream); - $this->assertEquals('12345', $stdoutStream); + $this->assertEquals('12345', $output); + $this->assertEquals('12345', $outputStream); $this->assertEquals(0, $code); } public function testExecuteStdOut() { - $stdout = ''; - $stderr = ''; + $output = ''; $stdin = ''; - $code = Console::execute('>&1 echo "success"', $stdin, $stdout, $stderr, 3); + $code = Console::execute('>&1 echo "success"', $stdin, $output, 3); - $this->assertEquals('', $stderr); - $this->assertEquals("success\n", $stdout); + $this->assertEquals("success\n", $output); $this->assertEquals(0, $code); } public function testExecuteStdErr() { - $stdout = ''; - $stderr = ''; + $output = ''; $stdin = ''; - $code = Console::execute('>&2 echo "error"', $stdin, $stdout, $stderr, 3); + $code = Console::execute('>&2 echo "error"', $stdin, $output, 3); - $this->assertEquals("error\n", $stderr); - $this->assertEquals('', $stdout); + $this->assertEquals("error\n", $output); $this->assertEquals(0, $code); } public function testExecuteExitCode() { - $stdout = ''; - $stderr = ''; + $output = ''; $stdin = ''; - $code = Console::execute('php -r "echo \'hello world\'; exit(2);"', $stdin, $stdout, $stderr, 10); + $code = Console::execute('php -r "echo \'hello world\'; exit(2);"', $stdin, $output, 10); - $this->assertEquals('', $stderr); - $this->assertEquals('hello world', $stdout); + $this->assertEquals('hello world', $output); $this->assertEquals(2, $code); - $stdout = ''; - $stderr = ''; + $output = ''; $stdin = ''; - $code = Console::execute('php -r "echo \'hello world\'; exit(100);"', $stdin, $stdout, $stderr, 10); + $code = Console::execute('php -r "echo \'hello world\'; exit(100);"', $stdin, $output, 10); - $this->assertEquals('', $stderr); - $this->assertEquals('hello world', $stdout); + $this->assertEquals('hello world', $output); $this->assertEquals(100, $code); } public function testExecuteTimeout() { - $stdout = ''; - $stderr = ''; + $output = ''; $stdin = ''; - $code = Console::execute('php -r "sleep(1); echo \'hello world\'; exit(0);"', $stdin, $stdout, $stderr, 3); + $code = Console::execute('php -r "sleep(1); echo \'hello world\'; exit(0);"', $stdin, $output, 3); - $this->assertEquals('', $stderr); - $this->assertEquals('hello world', $stdout); + $this->assertEquals('hello world', $output); $this->assertEquals(0, $code); - $stdout = ''; - $stderr = ''; + $output = ''; $stdin = ''; - $code = Console::execute('php -r "sleep(4); echo \'hello world\'; exit(0);"', $stdin, $stdout, $stderr, 3); + $code = Console::execute('php -r "sleep(4); echo \'hello world\'; exit(0);"', $stdin, $output, 3); - $this->assertEquals('', $stderr); - $this->assertEquals('', $stdout); + $this->assertEquals('', $output); $this->assertEquals(1, $code); } @@ -128,13 +109,11 @@ public function testLoop() { $file = __DIR__.'/../resources/loop.php'; $stdin = ''; - $stdout = ''; - $stderr = ''; - $code = Console::execute('php '.$file, $stdin, $stdout, $stderr, 30); + $output = ''; + $code = Console::execute('php '.$file, $stdin, $output, 30); - $this->assertEquals('', $stderr); - $this->assertGreaterThan(30, count(explode("\n", $stdout))); - $this->assertLessThan(50, count(explode("\n", $stdout))); + $this->assertGreaterThan(30, count(explode("\n", $output))); + $this->assertLessThan(50, count(explode("\n", $output))); $this->assertEquals(1, $code); } }