From 0f55f2565c01ec0d09d8e5d6ce8352f2a62c1c37 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Tue, 18 Jan 2022 11:41:42 +0100 Subject: [PATCH] Escape Symfony Console output When output contained `` Symfony Console tried to interpret `%s` color, it needs to be escaped. --- src/Runners/PHPUnit/ResultPrinter.php | 8 ++++++-- test/Unit/ResultTester.php | 3 +++ test/Unit/Runners/PHPUnit/BaseRunnerTest.php | 4 ++-- .../Unit/Runners/PHPUnit/ResultPrinterTest.php | 16 ++++++++++++++++ test/Unit/Runners/PHPUnit/SuiteLoaderTest.php | 2 +- .../FailingSymfonyOutputCollisionTest.php | 18 ++++++++++++++++++ test/fixtures/results/single-wfailure2.xml | 17 +++++++++++++++++ 7 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/failing_tests/FailingSymfonyOutputCollisionTest.php create mode 100644 test/fixtures/results/single-wfailure2.xml diff --git a/src/Runners/PHPUnit/ResultPrinter.php b/src/Runners/PHPUnit/ResultPrinter.php index 942fe5d2..5516b556 100644 --- a/src/Runners/PHPUnit/ResultPrinter.php +++ b/src/Runners/PHPUnit/ResultPrinter.php @@ -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; @@ -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) { diff --git a/test/Unit/ResultTester.php b/test/Unit/ResultTester.php index d9338b1b..16b43a5a 100644 --- a/test/Unit/ResultTester.php +++ b/test/Unit/ResultTester.php @@ -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; @@ -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); diff --git a/test/Unit/Runners/PHPUnit/BaseRunnerTest.php b/test/Unit/Runners/PHPUnit/BaseRunnerTest.php index 4a1e8dd6..a4ce1e7d 100644 --- a/test/Unit/Runners/PHPUnit/BaseRunnerTest.php +++ b/test/Unit/Runners/PHPUnit/BaseRunnerTest.php @@ -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(7, $suites); static::assertCount(24, $cases); - static::assertCount(6, $failures); + static::assertCount(7, $failures); static::assertCount(2, $warnings); static::assertCount(4, $skipped); static::assertCount(3, $errors); diff --git a/test/Unit/Runners/PHPUnit/ResultPrinterTest.php b/test/Unit/Runners/PHPUnit/ResultPrinterTest.php index 525ac135..202200dc 100644 --- a/test/Unit/Runners/PHPUnit/ResultPrinterTest.php +++ b/test/Unit/Runners/PHPUnit/ResultPrinterTest.php @@ -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('', $output); + } + public function testSkippedOutpusMessagesWithVerbose(): void { $this->options = $this->createOptionsFromArgv(['--colors' => true, '--verbose' => 1]); diff --git a/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php b/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php index 0bf60e37..4d289ee8 100644 --- a/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php +++ b/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php @@ -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 diff --git a/test/fixtures/failing_tests/FailingSymfonyOutputCollisionTest.php b/test/fixtures/failing_tests/FailingSymfonyOutputCollisionTest.php new file mode 100644 index 00000000..ebec7db8 --- /dev/null +++ b/test/fixtures/failing_tests/FailingSymfonyOutputCollisionTest.php @@ -0,0 +1,18 @@ +assertSame(' ', ''); + } +} diff --git a/test/fixtures/results/single-wfailure2.xml b/test/fixtures/results/single-wfailure2.xml new file mode 100644 index 00000000..3e45d157 --- /dev/null +++ b/test/fixtures/results/single-wfailure2.xml @@ -0,0 +1,17 @@ + + + + + FailingSymfonyOutputCollisionTest::testInvalidLogic + Failed asserting that two strings are identical. + --- Expected + +++ Actual + @@ @@ + -'<bg=%s> </>' + +'' + + /home/brian/Projects/parallel-phpunit/test/fixtures/tests/FailingSymfonyOutputCollisionTest.php:18 + + + +