[Console] Escape synopsis output

This commit is contained in:
Jan Schädlich 2021-05-23 17:28:21 +02:00 committed by Nicolas Grekas
parent 53d9b105a0
commit 960cb520df
4 changed files with 27 additions and 2 deletions

View File

@ -833,7 +833,7 @@ class Application implements ResetInterface
private function finishRenderThrowableOrException(OutputInterface $output): void
{
if (null !== $this->runningCommand) {
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);
$output->writeln(sprintf('<info>%s</info>', OutputFormatter::escape(sprintf($this->runningCommand->getSynopsis(), $this->getName()))), OutputInterface::VERBOSITY_QUIET);
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
}
}

View File

@ -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();

View File

@ -192,7 +192,8 @@ class CommandTest extends TestCase
$command = new \TestCommand();
$command->addOption('foo');
$command->addArgument('bar');
$this->assertEquals('namespace:name [--foo] [--] [<bar>]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
$command->addArgument('info');
$this->assertEquals('namespace:name [--foo] [--] [<bar> [<info>]]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
}
public function testAddGetUsages()

View File

@ -0,0 +1,7 @@
In ApplicationTest.php line %d:
some exception
foo [<info>]