diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php
index e71a16d109035..ed6d00190ee83 100644
--- a/src/Symfony/Component/Console/Application.php
+++ b/src/Symfony/Component/Console/Application.php
@@ -833,7 +833,7 @@ public function renderThrowable(\Throwable $e, OutputInterface $output): void
private function finishRenderThrowableOrException(OutputInterface $output): void
{
if (null !== $this->runningCommand) {
- $output->writeln(sprintf('%s', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);
+ $output->writeln(sprintf('%s', OutputFormatter::escape(sprintf($this->runningCommand->getSynopsis(), $this->getName()))), OutputInterface::VERBOSITY_QUIET);
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
}
}
diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php
index d293697d422c5..1634c01991b0a 100644
--- a/src/Symfony/Component/Console/Tests/ApplicationTest.php
+++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php
@@ -940,6 +940,23 @@ public function testRenderExceptionStackTraceContainsRootException()
$this->assertStringContainsString('Dummy type "class@anonymous" is invalid.', $tester->getDisplay(true));
}
+ public function testRenderExceptionEscapesLinesOfSynopsis()
+ {
+ $application = new Application();
+ $application->setAutoExit(false);
+ $application
+ ->register('foo')
+ ->setCode(function () {
+ throw new \Exception('some exception');
+ })
+ ->addArgument('info')
+ ;
+ $tester = new ApplicationTester($application);
+
+ $tester->run(['command' => 'foo'], ['decorated' => false]);
+ $this->assertStringMatchesFormatFile(self::$fixturesPath.'/application_rendersynopsis_escapesline.txt', $tester->getDisplay(true), '->renderException() escapes lines containing formatting of synopsis');
+ }
+
public function testRun()
{
$application = new Application();
diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php
index 10a476d192e31..7648b80ff0f08 100644
--- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php
+++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php
@@ -192,7 +192,8 @@ public function testGetSynopsis()
$command = new \TestCommand();
$command->addOption('foo');
$command->addArgument('bar');
- $this->assertEquals('namespace:name [--foo] [--] []', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
+ $command->addArgument('info');
+ $this->assertEquals('namespace:name [--foo] [--] [ []]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
}
public function testAddGetUsages()
diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_rendersynopsis_escapesline.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_rendersynopsis_escapesline.txt
new file mode 100644
index 0000000000000..a781326c621be
--- /dev/null
+++ b/src/Symfony/Component/Console/Tests/Fixtures/application_rendersynopsis_escapesline.txt
@@ -0,0 +1,7 @@
+
+In ApplicationTest.php line %d:
+
+ some exception
+
+
+foo []