Skip to content

Commit dba55ef

Browse files
committed
Render all line breaks according to the exception message
1 parent 800f306 commit dba55ef

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/Symfony/Component/Console/Application.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ public function renderException($e, $output)
681681
$width = 1 << 31;
682682
}
683683
$lines = array();
684-
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
684+
foreach (preg_split('/\r?\n/', trim($e->getMessage())) as $line) {
685685
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
686686
// pre-format lines to get the right string length
687687
$lineLength = Helper::strlen($line) + 4;
@@ -1133,9 +1133,8 @@ private function splitStringByWidth($string, $width)
11331133
$lines[] = str_pad($line, $width);
11341134
$line = $char;
11351135
}
1136-
if ('' !== $line) {
1137-
$lines[] = count($lines) ? str_pad($line, $width) : $line;
1138-
}
1136+
1137+
$lines[] = count($lines) ? str_pad($line, $width) : $line;
11391138

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

src/Symfony/Component/Console/Tests/ApplicationTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,22 @@ public function testRenderExceptionEscapesLines()
608608
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_escapeslines.txt', $tester->getDisplay(true), '->renderException() escapes lines containing formatting');
609609
}
610610

611+
public function testRenderExceptionLineBreaks()
612+
{
613+
$application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
614+
$application->setAutoExit(false);
615+
$application->expects($this->any())
616+
->method('getTerminalWidth')
617+
->will($this->returnValue(120));
618+
$application->register('foo')->setCode(function () {
619+
throw new \InvalidArgumentException("\n\nline 1 with extra spaces \nline 2\n\nline 4\n");
620+
});
621+
$tester = new ApplicationTester($application);
622+
623+
$tester->run(array('command' => 'foo'), array('decorated' => false));
624+
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_linebreaks.txt', $tester->getDisplay(true), '->renderException() keep multiple line breaks');
625+
}
626+
611627
public function testRun()
612628
{
613629
$application = new Application();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
[InvalidArgumentException]
4+
line 1 with extra spaces
5+
line 2
6+
7+
line 4
8+
9+
10+
foo
11+

0 commit comments

Comments
 (0)