Skip to content

[Console] Preserving line breaks between sentences according to the exception message #24219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ public function renderException($e, $output)
$width = 1 << 31;
}
$lines = array();
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
foreach (preg_split('/\r?\n/', trim($e->getMessage())) as $line) {
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
// pre-format lines to get the right string length
$lineLength = Helper::strlen($line) + 4;
Expand Down Expand Up @@ -1133,9 +1133,8 @@ private function splitStringByWidth($string, $width)
$lines[] = str_pad($line, $width);
$line = $char;
}
if ('' !== $line) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yceruto i think this is trimming related in fact :)

throw new \Exception("\n\nline 1 with extra spaces        \nline 2\n\nline 4\n");

Before/After
image

image

So here's a better test ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

$lines[] = count($lines) ? str_pad($line, $width) : $line;
}

$lines[] = count($lines) ? str_pad($line, $width) : $line;

mb_convert_variables($encoding, 'utf8', $lines);

Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,22 @@ public function testRenderExceptionEscapesLines()
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_escapeslines.txt', $tester->getDisplay(true), '->renderException() escapes lines containing formatting');
}

public function testRenderExceptionLineBreaks()
{
$application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
$application->setAutoExit(false);
$application->expects($this->any())
->method('getTerminalWidth')
->will($this->returnValue(120));
$application->register('foo')->setCode(function () {
throw new \InvalidArgumentException("\n\nline 1 with extra spaces \nline 2\n\nline 4\n");
});
$tester = new ApplicationTester($application);

$tester->run(array('command' => 'foo'), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_linebreaks.txt', $tester->getDisplay(true), '->renderException() keep multiple line breaks');
}

public function testRun()
{
$application = new Application();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


[InvalidArgumentException]
line 1 with extra spaces
line 2

line 4


foo