Skip to content

Commit

Permalink
Merge pull request #30 from utopia-php/feat-logs-combine
Browse files Browse the repository at this point in the history
Feat: Combine logs
  • Loading branch information
eldadfux authored Aug 5, 2023
2 parents fb3b9d1 + 03592ad commit 5b93663
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 59 deletions.
20 changes: 13 additions & 7 deletions src/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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)) {
Expand All @@ -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) {
Expand Down
83 changes: 31 additions & 52 deletions tests/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,113 +28,92 @@ 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);
}

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);
}
}

0 comments on commit 5b93663

Please sign in to comment.