8000 bug #38991 [Console] Fix ANSI when stdErr is not a tty (jderusse) · symfony/symfony@75f8ac1 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 75f8ac1

Browse files
committed
bug #38991 [Console] Fix ANSI when stdErr is not a tty (jderusse)
This PR was merged into the 4.4 branch. Discussion ---------- [Console] Fix ANSI when stdErr is not a tty | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38981 | License | MIT | Doc PR | - Taking the @wouterj 's comment into account (#38981 (comment)) This PR prevents using the same Formatter for stdOut and stdErr when possible. When user send a custom formatter (or call `setFormatter`) the previous logic is kept. Otherwise, symfony is asked to create the Formatter, and thus is able to clone the formatter. In a future PR targeting 5.3, we could improve the constructor to let people inject 2 distinguished formatters Commits ------- f3a398b Fix ANSI when stdErr is not a tty
2 parents 61b3872 + f3a398b commit 75f8ac1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Symfony/Component/Console/Output/ConsoleOutput.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public function __construct(int $verbosity = self::VERBOSITY_NORMAL, bool $decor
4141
{
4242
parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
4343

44+
if (null === $formatter) {
45+
// for BC reasons, stdErr has it own Formatter only when user don't inject a specific formatter.
46+
$this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated);
47+
48+
return;
49+
}
50+
4451
$actualDecorated = $this->isDecorated();
4552
$this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
4653

src/Symfony/Component/Console/Tests/Output/ConsoleOutputTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@
1818

1919
class ConsoleOutputTest extends TestCase
2020
{
21-
public function testConstructor()
21+
public function testConstructorWithoutFormatter()
2222
{
2323
$output = new ConsoleOutput(Output::VERBOSITY_QUIET, true);
2424
$this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
25-
$this->assertSame($output->getFormatter(), $output->getErrorOutput()->getFormatter(), '__construct() takes a formatter or null as the third argument');
25+
$this->assertNotSame($output->getFormatter(), $output->getErrorOutput()->getFormatter(), 'ErrorOutput should use it own formatter');
26+
}
27+
28+
public function testConstructorWithFormatter()
29+
{
30+
$output = new ConsoleOutput(Output::VERBOSITY_QUIET, true, $formatter = new OutputFormatter());
31+
$this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
32+
$this->assertSame($formatter, $output->getFormatter());
33+
$this->assertSame($formatter, $output->getErrorOutput()->getFormatter(), 'Output and ErrorOutput should use the same provided formatter');
2634
}
2735

2836
public function testSetFormatter()
@@ -31,6 +39,7 @@ public function testSetFormatter()
3139
$outputFormatter = new OutputFormatter();
3240
$output->setFormatter($outputFormatter);
3341
$this->assertSame($outputFormatter, $output->getFormatter());
42+
$this->assertSame($outputFormatter, $output->getErrorOutput()->getFormatter());
3443
}
3544

3645
public function testSetVerbosity()

0 commit comments

Comments
 (0)
0