Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit10f89d1

Browse files
committed
[Console] allow configuring exit code behavior inRunCommandMessage
1 parente2922e6 commit10f89d1

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
*/
1919
finalclass RunCommandFailedExceptionextends RuntimeException
2020
{
21-
publicfunction__construct(\Throwable$exception,publicreadonlyRunCommandContext$context)
21+
publicfunction__construct(\Throwable|string$exception,publicreadonlyRunCommandContext$context)
2222
{
23-
parent::__construct($exception->getMessage(),$exception->getCode(),$exception);
23+
parent::__construct(
24+
$exceptioninstanceof \Throwable ?$exception->getMessage() :$exception,
25+
$exceptioninstanceof \Throwable ?$exception->getCode() :0,
26+
$exceptioninstanceof \Throwable ?$exception :null,
27+
);
2428
}
2529
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ final class RunCommandContext extends RunCommandMessage
1818
{
1919
publicfunction__construct(RunCommandMessage$message,publicreadonlyint$exitCode,publicreadonlystring$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‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@
1111

1212
namespaceSymfony\Component\Console\Messenger;
1313

14+
useSymfony\Component\Console\Exception\RunCommandFailedException;
15+
1416
/**
1517
* @author Kevin Bond <kevinbond@gmail.com>
1618
*/
1719
class RunCommandMessageimplements \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
publicfunction__construct(
2026
publicreadonlystring$input,
27+
publicreadonlybool$throwOnFailure =true,
2128
publicreadonlybool$catchExceptions =false,
2229
) {
2330
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function __invoke(RunCommandMessage $message): RunCommandContext
3939
thrownewRunCommandFailedException($e,newRunCommandContext($message, Command::FAILURE,$output->fetch()));
4040
}
4141

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

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
useSymfony\Component\Console\Command\Command;
1717
useSymfony\Component\Console\Exception\RunCommandFailedException;
1818
useSymfony\Component\Console\Input\InputInterface;
19+
useSymfony\Component\Console\Input\InputOption;
1920
useSymfony\Component\Console\Messenger\RunCommandMessage;
2021
useSymfony\Component\Console\Messenger\RunCommandMessageHandler;
2122
useSymfony\Component\Console\Output\OutputInterface;
@@ -55,13 +56,31 @@ public function testExecutesCommandThatThrowsException()
5556
publicfunctiontestExecutesCommandThatCatchesThrownException()
5657
{
5758
$handler =newRunCommandMessageHandler($this->createApplicationWithCommand());
58-
$context =$handler(newRunCommandMessage('test:command --throw -v', catchExceptions:true));
59+
$context =$handler(newRunCommandMessage('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+
publicfunctiontestThrowOnNonSuccess()
67+
{
68+
$handler =newRunCommandMessageHandler($this->createApplicationWithCommand());
69+
70+
try {
71+
$handler(newRunCommandMessage('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
privatefunctioncreateApplicationWithCommand():Application
6685
{
6786
$application =newApplication();
@@ -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
thrownew \RuntimeException('exception message');
85105
}
86106

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp