Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Fixes missing output on ProcessFailedException exception #47285

Merged
merged 2 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Illuminate/Process/Exceptions/ProcessFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Illuminate\Process\Exceptions;

use Illuminate\Contracts\Process\ProcessResult;
use Symfony\Component\Console\Exception\RuntimeException;
use RuntimeException;

class ProcessFailedException extends RuntimeException
{
Expand All @@ -24,9 +24,19 @@ public function __construct(ProcessResult $result)
{
$this->result = $result;

parent::__construct(
sprintf('The process "%s" failed.', $result->command()),
$result->exitCode() ?? 1,
$error = sprintf('The command "%s" failed.'."\n\nExit Code: %s",
$result->command(),
$result->exitCode(),
);

if (! empty($result->output())) {
$error .= sprintf("\n\nOutput:\n================\n%s", $result->output());
}

if (! empty($result->errorOutput())) {
$error .= sprintf("\n\nError Output:\n================\n%s", $result->errorOutput());
}

parent::__construct($error, $result->exitCode() ?? 1);
}
}
116 changes: 114 additions & 2 deletions tests/Process/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,21 +424,133 @@ public function testRealProcessesCanHaveErrorOutput()
$this->assertEquals("Hello World\n", $result->errorOutput());
}

public function testRealProcessesCanThrow()
public function testFakeProcessesCanThrowWithoutOutput()
{
$this->expectException(ProcessFailedException::class);
$this->expectExceptionMessage(<<<'EOT'
The command "exit 1;" failed.

Exit Code: 1
EOT
);

$factory = new Factory;
$factory->fake(fn () => $factory->result(exitCode: 1));
$result = $factory->path(__DIR__)->run('exit 1;');

$result->throw();
}

public function testRealProcessesCanThrowWithoutOutput()
{
if (windows_os()) {
$this->markTestSkipped('Requires Linux.');
}

$this->expectException(ProcessFailedException::class);
$this->expectExceptionMessage(<<<'EOT'
The command "exit 1;" failed.

Exit Code: 1
EOT
);

$factory = new Factory;
$result = $factory->path(__DIR__)->run('exit 1;');

$result->throw();
}

public function testFakeProcessesCanThrowWithErrorOutput()
{
$this->expectException(ProcessFailedException::class);
$this->expectExceptionMessage(<<<'EOT'
The command "echo "Hello World" >&2; exit 1;" failed.

Exit Code: 1

Error Output:
================
Hello World
EOT
);

$factory = new Factory;
$factory->fake(fn () => $factory->result(errorOutput: 'Hello World', exitCode: 1));
$result = $factory->path(__DIR__)->run('echo "Hello World" >&2; exit 1;');

$result->throw();
}

public function testRealProcessesCanThrowWithErrorOutput()
{
if (windows_os()) {
$this->markTestSkipped('Requires Linux.');
}

$this->expectException(ProcessFailedException::class);
$this->expectExceptionMessage('The process "echo "Hello World" >&2; exit 1;" failed.');
$this->expectExceptionMessage(<<<'EOT'
The command "echo "Hello World" >&2; exit 1;" failed.

Exit Code: 1

Error Output:
================
Hello World
EOT
);

$factory = new Factory;
$result = $factory->path(__DIR__)->run('echo "Hello World" >&2; exit 1;');

$result->throw();
}

public function testFakeProcessesCanThrowWithOutput()
{
$this->expectException(ProcessFailedException::class);
$this->expectExceptionMessage(<<<'EOT'
The command "echo "Hello World" >&1; exit 1;" failed.

Exit Code: 1

Output:
================
Hello World
EOT
);

$factory = new Factory;
$factory->fake(fn () => $factory->result(output: 'Hello World', exitCode: 1));
$result = $factory->path(__DIR__)->run('echo "Hello World" >&1; exit 1;');

$result->throw();
}

public function testRealProcessesCanThrowWithOutput()
{
if (windows_os()) {
$this->markTestSkipped('Requires Linux.');
}

$this->expectException(ProcessFailedException::class);
$this->expectExceptionMessage(<<<'EOT'
The command "echo "Hello World" >&1; exit 1;" failed.

Exit Code: 1

Output:
================
Hello World
EOT
);

$factory = new Factory;
$result = $factory->path(__DIR__)->run('echo "Hello World" >&1; exit 1;');

$result->throw();
}

public function testRealProcessesCanTimeout()
{
if (windows_os()) {
Expand Down