Skip to content

Commit

Permalink
Escape Symfony Console output (#650)
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 authored Mar 28, 2022
1 parent 76b895b commit ebf5561
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/Runners/PHPUnit/ResultPrinter.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions test/Unit/Runners/PHPUnit/BaseRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ private function assertJunitXmlIsCorrect(string $path): void

// these numbers represent the tests in fixtures/failing_tests
// so will need to be updated when tests are added or removed
static::assertCount(6, $suites);
static::assertCount(24, $cases);
static::assertCount(6, $failures);
static::assertCount(7, $suites);
static::assertCount(25, $cases);
static::assertCount(7, $failures);
static::assertCount(2, $warnings);
static::assertCount(4, $skipped);
static::assertCount(3, $errors);
Expand Down
16 changes: 16 additions & 0 deletions test/Unit/Runners/PHPUnit/ResultPrinterTest.php
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion test/Unit/Runners/PHPUnit/SuiteLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public function testTestMethodsOfParentClassesAreCorrectlyLoaded(): void
$this->bareOptions['--path'] = $this->fixture('failing_tests');
$loader = $this->loadSuite();

static::assertCount(24, $loader->getTestMethods());
static::assertCount(25, $loader->getTestMethods());
}

public function testTestMethodsFromErroringDataProviderReturnTheSimpleMethodToBeRunInTheSubprocess(): void
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/failing_tests/FailingSymfonyOutputCollisionTest.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 ebf5561

Please sign in to comment.