From a7eb77e93cafeacfc889a7d5656918b5c3245702 Mon Sep 17 00:00:00 2001 From: Jan Pecha Date: Mon, 5 Aug 2024 15:25:46 +0200 Subject: [PATCH] RunnerResult: accepts raw output & error output (#100) --- src/RunnerResult.php | 63 +++++++++++++++++++++++++--- src/Runners/CliRunner.php | 2 +- src/Runners/MemoryRunner.php | 6 +-- tests/GitPhp/RunnerResult.phpt | 75 ++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 tests/GitPhp/RunnerResult.phpt diff --git a/src/RunnerResult.php b/src/RunnerResult.php index 4bbf9fc..9703a36 100644 --- a/src/RunnerResult.php +++ b/src/RunnerResult.php @@ -11,20 +11,20 @@ class RunnerResult /** @var int */ private $exitCode; - /** @var string[] */ + /** @var string|string[] */ private $output; - /** @var string[] */ + /** @var string|string[] */ private $errorOutput; /** * @param string $command * @param int $exitCode - * @param string[] $output - * @param string[] $errorOutput + * @param string|string[] $output + * @param string|string[] $errorOutput */ - public function __construct($command, $exitCode, array $output, array $errorOutput) + public function __construct($command, $exitCode, $output, $errorOutput) { $this->command = (string) $command; $this->exitCode = (int) $exitCode; @@ -65,6 +65,10 @@ public function getExitCode() */ public function getOutput() { + if (is_string($this->output)) { + return $this->splitOutput($this->output); + } + return $this->output; } @@ -74,6 +78,10 @@ public function getOutput() */ public function getOutputAsString() { + if (is_string($this->output)) { + return $this->output; + } + return implode("\n", $this->output); } @@ -83,7 +91,8 @@ public function getOutputAsString() */ public function getOutputLastLine() { - $lastLine = end($this->output); + $output = $this->getOutput(); + $lastLine = end($output); return is_string($lastLine) ? $lastLine : NULL; } @@ -93,6 +102,10 @@ public function getOutputLastLine() */ public function hasOutput() { + if (is_string($this->output)) { + return trim($this->output) !== ''; + } + return !empty($this->output); } @@ -102,15 +115,36 @@ public function hasOutput() */ public function getErrorOutput() { + if (is_string($this->errorOutput)) { + return $this->splitOutput($this->errorOutput); + } + return $this->errorOutput; } + /** + * @return string + */ + public function getErrorOutputAsString() + { + if (is_string($this->errorOutput)) { + return $this->errorOutput; + } + + return implode("\n", $this->errorOutput); + } + + /** * @return bool */ public function hasErrorOutput() { + if (is_string($this->errorOutput)) { + return trim($this->errorOutput) !== ''; + } + return !empty($this->errorOutput); } @@ -127,4 +161,21 @@ public function toText() . implode("\n", $this->getErrorOutput()) . "\n\n" . '=> ' . $this->getExitCode() . "\n\n"; } + + + /** + * @param string $output + * @return string[] + */ + private function splitOutput($output) + { + $output = str_replace(["\r\n", "\r"], "\n", $output); + $output = rtrim($output, "\n"); + + if ($output === '') { + return []; + } + + return explode("\n", $output); + } } diff --git a/src/Runners/CliRunner.php b/src/Runners/CliRunner.php index 0acacd7..462e61c 100644 --- a/src/Runners/CliRunner.php +++ b/src/Runners/CliRunner.php @@ -80,7 +80,7 @@ public function run($cwd, array $args, array $env = NULL) } $returnCode = proc_close($process); - return new RunnerResult($command, $returnCode, $this->convertOutput($stdout), $this->convertOutput($stderr)); + return new RunnerResult($command, $returnCode, $stdout, $stderr); } diff --git a/src/Runners/MemoryRunner.php b/src/Runners/MemoryRunner.php index 2a06d43..4982c9c 100644 --- a/src/Runners/MemoryRunner.php +++ b/src/Runners/MemoryRunner.php @@ -33,12 +33,12 @@ public function __construct($cwd) /** * @param array $args * @param array $env - * @param array $output - * @param array $errorOutput + * @param string|array $output + * @param string|array $errorOutput * @param int $exitCode * @return self */ - public function setResult(array $args, array $env, array $output, array $errorOutput = [], $exitCode = 0) + public function setResult(array $args, array $env, $output, $errorOutput = [], $exitCode = 0) { $cmd = $this->commandProcessor->process('git', $args, $env); $this->results[$cmd] = new RunnerResult($cmd, $exitCode, $output, $errorOutput); diff --git a/tests/GitPhp/RunnerResult.phpt b/tests/GitPhp/RunnerResult.phpt new file mode 100644 index 0000000..cee84c0 --- /dev/null +++ b/tests/GitPhp/RunnerResult.phpt @@ -0,0 +1,75 @@ +hasOutput()); + Assert::same(['foo', 'bar'], $result->getOutput()); + Assert::same("foo\r\nbar\r\n", $result->getOutputAsString()); + Assert::same('bar', $result->getOutputLastLine()); + + Assert::true($result->hasErrorOutput()); + Assert::same(['error', 'error2'], $result->getErrorOutput()); + Assert::same("error\r\nerror2\r\n", $result->getErrorOutputAsString()); +}); + + +test(function () { + $result = new RunnerResult('cat-file', 0, "\r\n", "\r\n"); + + Assert::false($result->hasOutput()); + Assert::same([], $result->getOutput()); + Assert::same("\r\n", $result->getOutputAsString()); + Assert::null($result->getOutputLastLine()); + + Assert::false($result->hasErrorOutput()); + Assert::same([], $result->getErrorOutput()); + Assert::same("\r\n", $result->getErrorOutputAsString()); +}); + + +test(function () { + $result = new RunnerResult('cat-file', 0, '', ''); + + Assert::false($result->hasOutput()); + Assert::same([], $result->getOutput()); + Assert::same('', $result->getOutputAsString()); + Assert::null($result->getOutputLastLine()); + + Assert::false($result->hasErrorOutput()); + Assert::same([], $result->getErrorOutput()); + Assert::same('', $result->getErrorOutputAsString()); +}); + + +test(function () { + $result = new RunnerResult('cat-file', 0, ['foo', 'bar'], ['error', 'error2']); + + Assert::true($result->hasOutput()); + Assert::same(['foo', 'bar'], $result->getOutput()); + Assert::same("foo\nbar", $result->getOutputAsString()); + Assert::same('bar', $result->getOutputLastLine()); + + Assert::true($result->hasErrorOutput()); + Assert::same(['error', 'error2'], $result->getErrorOutput()); + Assert::same("error\nerror2", $result->getErrorOutputAsString()); +}); + + +test(function () { + $result = new RunnerResult('cat-file', 0, [], []); + + Assert::false($result->hasOutput()); + Assert::same([], $result->getOutput()); + Assert::same('', $result->getOutputAsString()); + Assert::null($result->getOutputLastLine()); + + Assert::false($result->hasErrorOutput()); + Assert::same([], $result->getErrorOutput()); + Assert::same('', $result->getErrorOutputAsString()); +});