Skip to content

Commit

Permalink
bug #24219 [Console] Preserving line breaks between sentences accordi…
Browse files Browse the repository at this point in the history
…ng to the exception message (yceruto)

This PR was merged into the 2.7 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24212
| License       | MIT
| Doc PR        | -

Commits
-------

e2d4904 Render all line breaks according to the exception message
  • Loading branch information
chalasr committed Sep 20, 2017
2 parents 139398f + e2d4904 commit a40c94d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
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) {
$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

0 comments on commit a40c94d

Please sign in to comment.