Skip to content

Commit

Permalink
Merge pull request #462 from jolicode/easier-process-debug
Browse files Browse the repository at this point in the history
Display more info about failed process with -v flag
  • Loading branch information
pyrech authored May 28, 2024
2 parents 922b6ea + 6f569de commit 3586792
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 10 deletions.
1 change: 1 addition & 0 deletions bin/generate-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
add_test(['list', '--raw', '--format', 'txt', '--short'], 'List', needRemote: true, skipOnBinary: true);
// Transient test, disabled for now
// add_test(['parallel:sleep', '--sleep5', '0', '--sleep7', '0', '--sleep10', '0'], 'ParallelSleep');
add_test(['run:exception', '-v'], 'RunExceptionVerbose');
add_test(['symfony:greet', 'World', '--french', 'COUCOU', '--punctuation', '!'], 'SymfonyGreet', skipOnBinary: true);
add_test(['symfony:hello'], 'SymfonyHello', skipOnBinary: true);
// In /tmp
Expand Down
6 changes: 3 additions & 3 deletions examples/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
function info(): void
{
if (!output()->isVeryVerbose()) {
output()->writeln('Re-run with -vv, -vvv to different output.');
output()->writeln('Re-run with -vv, -vvv for different output.');
}

log('Hello, this is an "info" log message.', 'info');
Expand All @@ -27,7 +27,7 @@ function error(): void
function with_context(): void
{
if (!output()->isVerbose()) {
output()->writeln('Re-run with -v, -vv, -vvv to different output.');
output()->writeln('Re-run with -v, -vv, -vvv for different output.');
}

log('Hello, I\'have a context!', 'error', context: [
Expand All @@ -39,7 +39,7 @@ function with_context(): void
function all_level(): void
{
if (!output()->isVerbose()) {
output()->writeln('Re-run with -v, -vv, -vvv to different output.');
output()->writeln('Re-run with -v, -vv, -vvv for different output.');
}

$levels = [
Expand Down
10 changes: 10 additions & 0 deletions examples/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ function testFile(): int
return exit_code('test -f unknown-file');
}

#[AsTask(description: 'Run a command that will fail')]
function exception(): void
{
if (!output()->isVerbose()) {
output()->writeln('Re-run with -v, -vv, -vvv for different output.');
}

run('echo foo; echo bar>&2; exit 1', pty: false, quiet: true);
}

#[AsTask(description: 'Run a sub-process and display information about it, with ProcessHelper')]
function with_process_helper(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Runner/ProcessRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function run(
if (0 !== $exitCode) {
$this->logger->notice(sprintf('Command finished with an error (exit code=%d).', $process->getExitCode()));
if (!$context->allowFailure) {
if ($context->verbosityLevel->isVeryVerbose()) {
if ($context->verbosityLevel->isVerbose()) {
throw new ProcessFailedException($process);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Examples/LogAllLevelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testLogAllLevel(): void
{
$process = $this->runTask(['log:all-level']);
$this->assertSame(0, $process->getExitCode());
$this->assertStringContainsString('Re-run with -v, -vv, -vvv to different output.', $process->getOutput());
$this->assertStringContainsString('Re-run with -v, -vv, -vvv for different output.', $process->getOutput());
$this->assertStringContainsString('EMERGENCY [castor] level: emergency', $process->getOutput());
$this->assertStringContainsString('ALERT [castor] level: alert', $process->getOutput());
$this->assertStringContainsString('CRITICAL [castor] level: critical', $process->getOutput());
Expand Down
4 changes: 2 additions & 2 deletions tests/Examples/LogInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testLogInfo(): void
{
$process = $this->runTask(['log:info']);
$this->assertSame(0, $process->getExitCode());
$this->assertStringContainsString('Re-run with -vv, -vvv to different output.', $process->getOutput());
$this->assertStringContainsString('Re-run with -vv, -vvv for different output.', $process->getOutput());
$this->assertSame('', $process->getErrorOutput());
}

Expand All @@ -20,7 +20,7 @@ public function testLogInfo2(): void
{
$process = $this->runTask(['log:info', '-v']);
$this->assertSame(0, $process->getExitCode());
$this->assertStringContainsString('Re-run with -vv, -vvv to different output.', $process->getOutput());
$this->assertStringContainsString('Re-run with -vv, -vvv for different output.', $process->getOutput());
$this->assertSame('', $process->getErrorOutput());
}

Expand Down
1 change: 1 addition & 0 deletions tests/Generated/ListTest.php.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ qa:phpstan:update update depende
quiet:quiet Executes something but does not output anything
remote-import:remote-task-class Use a class that extends a class imported from a remote package
remote-import:remote-tasks Use functions imported from remote packages
run:exception Run a command that will fail
run:ls Run a sub-process and display information about it
run:test-file Run a sub-process and return its exit code, with get_exit_code() function
run:variables Run a sub-process with environment variables and display information about it
Expand Down
22 changes: 22 additions & 0 deletions tests/Generated/RunExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Generated;

use Castor\Tests\TaskTestCase;
use Symfony\Component\Process\Exception\ProcessFailedException;

class RunExceptionTest extends TaskTestCase
{
// run:exception
public function test(): void
{
$process = $this->runTask(['run:exception']);

if (1 !== $process->getExitCode()) {
throw new ProcessFailedException($process);
}

$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
}
}
7 changes: 7 additions & 0 deletions tests/Generated/RunExceptionTest.php.err.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
In run.php line 63:

The command "echo foo; echo bar>&2; exit 1" failed.


run:exception

1 change: 1 addition & 0 deletions tests/Generated/RunExceptionTest.php.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Re-run with -v, -vv, -vvv for different output.
22 changes: 22 additions & 0 deletions tests/Generated/RunExceptionVerboseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Generated;

use Castor\Tests\TaskTestCase;
use Symfony\Component\Process\Exception\ProcessFailedException;

class RunExceptionVerboseTest extends TaskTestCase
{
// run:exception
public function test(): void
{
$process = $this->runTask(['run:exception', '-v']);

if (1 !== $process->getExitCode()) {
throw new ProcessFailedException($process);
}

$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
}
}
34 changes: 34 additions & 0 deletions tests/Generated/RunExceptionVerboseTest.php.err.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
In ProcessRunner.php line XXXX:

[Symfony\Component\Process\Exception\ProcessFailedException]
The command "echo foo; echo bar>&2; exit 1" failed.

Exit Code: 1(General error)

Working directory: ...

Output:
================
foo


Error Output:
================
bar


Exception trace:
at .../src/Runner/ProcessRunner.php:XXXX
Castor\Runner\ProcessRunner->run() at .../src/functions.php:XXXX
Castor\run() at .../examples/run.php:XXXX
run\exception() at .../src/Console/Command/TaskCommand.php:XXXX
Castor\Console\Command\TaskCommand->execute() at .../vendor/symfony/console/Command/Command.php:XXXX
Symfony\Component\Console\Command\Command->run() at .../vendor/symfony/console/Application.php:XXXX
Symfony\Component\Console\Application->doRunCommand() at .../src/Console/Application.php:XXXX
Castor\Console\Application->doRunCommand() at .../vendor/symfony/console/Application.php:XXXX
Symfony\Component\Console\Application->doRun() at .../src/Console/Application.php:XXXX
Castor\Console\Application->doRun() at .../vendor/symfony/console/Application.php:XXXX
Symfony\Component\Console\Application->run() at .../bin/castor:XXXX

run:exception

1 change: 1 addition & 0 deletions tests/Generated/RunExceptionVerboseTest.php.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hh:mm:ss NOTICE [castor] Command finished with an error (exit code=1).
2 changes: 1 addition & 1 deletion tests/Generated/TwoDefaultContextTest.php.err.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
In ContextRegistry.php line XXXX:

Function "two()" is not properly configured:
You cannot define two contexts with the same name "default". There is one already defined in "castor.php:7".
You cannot define two contexts with the same name "default". There is one already defined in "castor.php:XXXX".
Defined in "castor.php" line 13.


11 changes: 9 additions & 2 deletions tests/Helper/OutputCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ public static function cleanOutput(string $string): string
$string = str_replace("scp: Connection closed\n", '', $string);
$string = str_replace("lost connection\n", '', $string);

// Phar, trace
$string = str_replace(\dirname(__DIR__, 2), '...', $string);
$string = str_replace('phar://', '', $string);
$string = str_replace('tools/phar/build/castor/', '', $string);
$string = str_replace('.../castor/', '.../', $string);
$string = preg_replace("{require\\(\\) at .*/castor:\\d+\n}", '', $string);

// Clean line numbers
$string = preg_replace('{In ([A-Z]\w+).php line \d+:}m', 'In \1.php line XXXX:', $string);
$string = preg_replace('{In functions.php line \d+:}m', 'In functions.php line XXXX:', $string);
$string = preg_replace('{\.php:\d+}m', '.php:XXXX', $string);
$string = preg_replace('{castor:\d+}m', 'castor:XXXX', $string);

// Clean the time
$string = preg_replace('{^\d\d:\d\d:\d\d }m', 'hh:mm:ss ', $string);
Expand All @@ -35,8 +44,6 @@ public static function cleanOutput(string $string): string
$string = ltrim($string, "\n"); // Trim output start to avoid empty lines (like after removing remote import warnings)
$string = preg_replace('/ +$/m', '', $string); // Remove trailing space

$string = str_replace(\dirname(__DIR__, 2), '...', $string);

// Fix the watcher path when running the tests with local project VS in the phar / static
$string = str_replace('.../src/Runner/../../tools/watcher/bin/watcher-linux-amd64', 'watcher', $string);
$string = str_replace('/tmp/watcher-linux-amd64', 'watcher', $string);
Expand Down

0 comments on commit 3586792

Please sign in to comment.