8000 [Console] allow configuring exit code behavior in `RunCommandMessage` · symfony/symfony@10f89d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10f89d1

Browse files
committed
[Console] allow configuring exit code behavior in RunCommandMessage
1 parent e2922e6 commit 10f89d1

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

src/Symfony/Component/Console/Exception/RunCommandFailedException.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
*/
1919
final class RunCommandFailedException extends RuntimeException
2020
{
21-
public function __construct(\Throwable $exception, public readonly RunCommandContext $context)
21+
public function __construct(\Throwable|string $exception, public readonly RunCommandContext $context)
2222
{
23-
parent::__construct($exception->getMessage(), $exception->getCode(), $exception);
23+
parent::__construct(
24+
$exception instanceof \Throwable ? $exception->getMessage() : $exception,
25+
$exception instanceof \Throwable ? $exception->getCode() : 0,
26+
$exception instanceof \Throwable ? $exception : null,
27+
);
2428
}
2529
}

src/Symfony/Component/Console/Messenger/RunCommandContext.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ final class RunCommandContext extends RunCommandMessage
1818
{
1919
public function __construct(RunCommandMessage $message, public readonly int $exitCode, public readonly string $output)
2020
{
21-
parent::__construct($message->input, $message->catchExceptions);
21+
parent::__construct($message->input, $message->throwOnFailure, $message->catchExceptions);
2222
}
2323
}

src/Symfony/Component/Console/Messenger/RunCommandMessage.php

+7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@
1111

1212
namespace Symfony\Component\Console\Messenger;
1313

14+
use Symfony\Component\Console\Exception\RunCommandFailedException;
15+
1416
/**
1517
* @author Kevin Bond <kevinbond@gmail.com>
1618
*/
1719
class RunCommandMessage implements \Stringable
1820
{
21+
/**
22+
* @param bool $throwOnFailure If the command has a non-zero exit code, throw {@see RunCommandFailedException}
23+
* @param bool $catchExceptions @see Application::setCatchExceptions()
24+
*/
1925
public function __construct(
2026
public readonly string $input,
27+
public readonly bool $throwOnFailure = true,
2128
public readonly bool $catchExceptions = false,
2229
) {
2330
}

src/Symfony/Component/Console/Messenger/RunCommandMessageHandler.php

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function __invoke(RunCommandMessage $message): RunCommandContext
3939
throw new RunCommandFailedException($e, new RunCommandContext($message, Command::FAILURE, $output->fetch()));
4040
}
4141

42+
if ($message->throwOnFailure && Command::SUCCESS !== $exitCode) {
43+
throw new RunCommandFailedException(sprintf('Command "%s" exited with code "%s".', $message->input, $exitCode), new RunCommandContext($message, $exitCode, $output->fetch()));
44+
}
45+
4246
return new RunCommandContext($message, $exitCode, $output->fetch());
4347
}
4448
}

src/Symfony/Component/Console/Tests/Messenger/RunCommandMessageHandlerTest.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Command\Command;
1717
use Symfony\Component\Console\Exception\RunCommandFailedException;
1818
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Input\InputOption;
1920
use Symfony\Component\Console\Messenger\RunCommandMessage;
2021
use Symfony\Component\Console\Messenger\RunCommandMessageHandler;
2122
use Symfony\Component\Console\Output\OutputInterface;
@@ -55,13 +56,31 @@ public function testExecutesCommandThatThrowsException()
5556
public function testExecutesCommandThatCatchesThrownException()
5657
{
5758
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
58-
$context = $handler(new RunCommandMessage('test:command --throw -v', catchExceptions: true));
59+
$context = $handler(new RunCommandMessage('test:command --throw -v', throwOnFailure: false, catchExceptions: true));
5960

6061
$this->assertSame(1, $context->exitCode);
6162
$this->assertStringContainsString('[RuntimeException]', $context->output);
6263
$this->assertStringContainsString('exception message', $context->output);
6364
}
6465

66+
public function testThrowOnNonSuccess()
67+
{
68+
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
69+
70+
try {
71+
$handler(new RunCommandMessage('test:command --exit=1'));
72+
} catch (RunCommandFailedException $e) {
73+
$this->assertSame(1, $e->context->exitCode);
74+
$this->assertStringContainsString('some message', $e->context->output);
75+
$this->assertSame('Command "test:command --exit=1" exited with code "1".', $e->getMessage());
76+
$this->assertNull($e->getPrevious());
77+
78+
return;
79+
}
80+
81+
$this->fail('Exception not thrown.');
82+
}
83+
6584
private function createApplicationWithCommand(): Application
6685
{
6786
$application = new Application();
@@ -73,6 +92,7 @@ public function configure(): void
7392
$this
7493
->setName('test:command')
7594
->addOption('throw')
95+
->addOption('exit', null, InputOption::VALUE_REQUIRED, 0)
7696
;
7797
}
7898

@@ -84,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
84104
throw new \RuntimeException('exception message');
85105
}
86106

87-
return Command::SUCCESS;
107+
return (int) $input->getOption('exit');
88108
}
89109
},
90110
]);

0 commit comments

Comments
 (0)
0