From 960cb520dfcb26b8409d0e2b0011dfb547852754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4dlich?= Date: Sun, 23 May 2021 17:28:21 +0200 Subject: [PATCH] [Console] Escape synopsis output --- src/Symfony/Component/Console/Application.php | 2 +- .../Component/Console/Tests/ApplicationTest.php | 17 +++++++++++++++++ .../Console/Tests/Command/CommandTest.php | 3 ++- .../application_rendersynopsis_escapesline.txt | 7 +++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/application_rendersynopsis_escapesline.txt diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index e71a16d109..ed6d00190e 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -833,7 +833,7 @@ class Application implements ResetInterface 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 d293697d42..1634c01991 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -940,6 +940,23 @@ class ApplicationTest extends TestCase $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 10a476d192..7648b80ff0 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -192,7 +192,8 @@ class CommandTest extends TestCase $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 0000000000..a781326c62 --- /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 []