Skip to content

Commit

Permalink
RunnerResult: accepts raw output & error output (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Aug 5, 2024
1 parent 41c63c2 commit a7eb77e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 10 deletions.
63 changes: 57 additions & 6 deletions src/RunnerResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,6 +65,10 @@ public function getExitCode()
*/
public function getOutput()
{
if (is_string($this->output)) {
return $this->splitOutput($this->output);
}

return $this->output;
}

Expand All @@ -74,6 +78,10 @@ public function getOutput()
*/
public function getOutputAsString()
{
if (is_string($this->output)) {
return $this->output;
}

return implode("\n", $this->output);
}

Expand All @@ -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;
}

Expand All @@ -93,6 +102,10 @@ public function getOutputLastLine()
*/
public function hasOutput()
{
if (is_string($this->output)) {
return trim($this->output) !== '';
}

return !empty($this->output);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Runners/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function run($cwd, array $args, array $env = NULL)
}

$returnCode = proc_close($process);

This comment has been minimized.

Copy link
@Brunojoe1994

Brunojoe1994 Sep 20, 2024

$returncode = proc_open $process;

return new RunnerResult($command, $returnCode, $this->convertOutput($stdout), $this->convertOutput($stderr));
return new RunnerResult($command, $returnCode, $stdout, $stderr);
}


Expand Down
6 changes: 3 additions & 3 deletions src/Runners/MemoryRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public function __construct($cwd)
/**
* @param array<mixed> $args
* @param array<string, scalar> $env
* @param array<string> $output
* @param array<string> $errorOutput
* @param string|array<string> $output
* @param string|array<string> $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);
Expand Down
75 changes: 75 additions & 0 deletions tests/GitPhp/RunnerResult.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

use Tester\Assert;
use CzProject\GitPhp\RunnerResult;

require __DIR__ . '/bootstrap.php';

test(function () {
$result = new RunnerResult('cat-file', 0, "foo\r\nbar\r\n", "error\r\nerror2\r\n");

Assert::true($result->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());
});

0 comments on commit a7eb77e

Please sign in to comment.