Skip to content

Commit

Permalink
Escape Symfony Console output
Browse files Browse the repository at this point in the history
When output contained `<bg=%s>` Symfony Console tried to interpret `%s` color, it needs to be escaped.
  • Loading branch information
simPod committed Jan 18, 2022
1 parent 56773f6 commit 546f466
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Runners/PHPUnit/ResultPrinter.php
Expand Up @@ -9,6 +9,7 @@
use ParaTest\Logging\LogInterpreter;
use PHPUnit\Util\Color;
use SebastianBergmann\Timer\ResourceUsageFormatter;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Output\OutputInterface;

use function array_filter;
Expand Down Expand Up @@ -200,10 +201,13 @@ public function printResults(): void
$toFilter[] = $this->getSkipped();
}

$failures = array_filter($toFilter);
$failures = array_filter($toFilter);
$escapedFailures = array_map(static function (string $failure): string {
return OutputFormatter::escape($failure);
}, $failures);

$this->output->write($this->getHeader());
$this->output->write(implode("---\n\n", $failures));
$this->output->write(implode("---\n\n", $escapedFailures));
$this->output->write($this->getFooter());

if ($this->teamcityLogFileHandle === null) {
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/ResultTester.php
Expand Up @@ -16,6 +16,8 @@ abstract class ResultTester extends TestBase
/** @var Suite */
protected $failureSuite;
/** @var Suite */
protected $otherFailureSuite;
/** @var Suite */
protected $otherErrorSuite;
/** @var Suite */
protected $mixedSuite;
Expand All @@ -36,6 +38,7 @@ final public function setUpTest(): void
$this->warningSuite = $this->getSuiteWithResult('single-warning.xml', 1);
$this->otherErrorSuite = $this->getSuiteWithResult('single-werror2.xml', 1);
$this->failureSuite = $this->getSuiteWithResult('single-wfailure.xml', 3);
$this->otherFailureSuite = $this->getSuiteWithResult('single-wfailure2.xml', 3);
$this->mixedSuite = $this->getSuiteWithResult('mixed-results.xml', 7);
$this->skipped = $this->getSuiteWithResult('single-skipped.xml', 1);
$this->passingSuite = $this->getSuiteWithResult('single-passing.xml', 3);
Expand Down
16 changes: 16 additions & 0 deletions test/Unit/Runners/PHPUnit/ResultPrinterTest.php
Expand Up @@ -466,6 +466,22 @@ public function testColorsForSkipped(): void
static::assertStringNotContainsString('UnitTestWithMethodAnnotationsTest', $output);
}

public function testColorsParsing(): void
{
$this->options = $this->createOptionsFromArgv(['--colors' => true, '--verbose' => true]);
$this->printer = new ResultPrinter($this->interpreter, $this->output, $this->options);
$this->printer->addTest($this->otherFailureSuite);

$this->printer->start();
$this->printer->printFeedback($this->otherFailureSuite);
$this->printer->printResults();

$output = $this->output->fetch();
static::assertStringContainsString('FAILURES', $output);
static::assertStringContainsString('FailingSymfonyOutputCollisionTest', $output);
static::assertStringContainsString('<bg=%s>', $output);
}

public function testSkippedOutpusMessagesWithVerbose(): void
{
$this->options = $this->createOptionsFromArgv(['--colors' => true, '--verbose' => 1]);
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/failing_tests/FailingSymfonyOutputCollisionTest.php
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace ParaTest\Tests\fixtures\failing_tests;

use PHPUnit\Framework\TestCase;

/**
* @internal
*/
final class FailingSymfonyOutputCollisionTest extends TestCase
{
public function testInvalidLogic(): void
{
$this->assertSame('<bg=%s> </>', '');
}
}
17 changes: 17 additions & 0 deletions test/fixtures/results/single-wfailure2.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="UnitTestWithMethodAnnotationsTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/UnitTestWithMethodAnnotationsTest.php" tests="3" assertions="3" failures="1" errors="0" time="0.005895">
<testcase name="testInvalidLogic" class="UnitTestWithMethodAnnotationsTest" file="/home/brian/Projects/parallel-phpunit/test/fixtures/tests/FailingSymfonyOutputCollisionTest.php" line="18" assertions="1" time="0.001632">
<failure type="PHPUnit\Framework\ExpectationFailedException">FailingSymfonyOutputCollisionTest::testInvalidLogic
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'&lt;bg=%s&gt; &lt;/&gt;'
+''

/home/brian/Projects/parallel-phpunit/test/fixtures/tests/FailingSymfonyOutputCollisionTest.php:18
</failure>
</testcase>
</testsuite>
</testsuites>

0 comments on commit 546f466

Please sign in to comment.