Skip to content

Commit

Permalink
junit: eliminate newlines inside <failure> (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Aug 6, 2024
1 parent 47ea1c8 commit 7feaa97
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
31 changes: 13 additions & 18 deletions src/Result/JunitFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function count;
use function extension_loaded;
use function htmlspecialchars;
use function implode;
use function sprintf;
use function strlen;
use function strpos;
Expand Down Expand Up @@ -161,18 +160,14 @@ private function createSymbolBasedTestSuite(string $title, array $errors, int $m
foreach ($errors as $symbol => $usages) {
$xml .= sprintf('<testcase name="%s">', $this->escape($symbol));

$failureUsage = [];

foreach ($usages as $index => $usage) {
$failureUsage[] = $this->relativizeUsage($usage);
$xml .= sprintf('<failure>%s</failure>', $this->escape($this->relativizeUsage($usage)));

if ($index === $maxShownUsages) {
if ($index === $maxShownUsages - 1) {
break;
}
}

$xml .= sprintf('<failure>%s</failure>', $this->escape(implode("\n", $failureUsage)));

$xml .= '</testcase>';
}

Expand All @@ -194,17 +189,17 @@ private function createPackageBasedTestSuite(string $title, array $errors, int $
$printedSymbols = 0;

foreach ($usagesPerClassname as $symbol => $usages) {
$printedSymbols++;
$xml .= sprintf(
'<failure message="%s">%s</failure>',
$symbol,
$this->escape(
implode("\n", $this->createUsages($usages, $maxShownUsages))
)
);

if ($printedSymbols === $maxShownUsages) {
break;
foreach ($this->createUsages($usages, $maxShownUsages) as $usage) {
$printedSymbols++;
$xml .= sprintf(
'<failure message="%s">%s</failure>',
$symbol,
$this->escape($usage)
);

if ($printedSymbols === $maxShownUsages) {
break 2;
}
}
}

Expand Down
20 changes: 13 additions & 7 deletions tests/JunitFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public function testPrintResult(): void
10,
0.123,
[],
['Unknown\\Thing' => [new SymbolUsage('/app/app/init.php', 1093, SymbolKind::CLASSLIKE)]],
['Unknown\\Thing' => [
new SymbolUsage('/app/app/init.php', 1091, SymbolKind::CLASSLIKE),
new SymbolUsage('/app/app/init.php', 1093, SymbolKind::CLASSLIKE),
]],
['Unknown\\function' => [new SymbolUsage('/app/app/foo.php', 51, SymbolKind::FUNCTION)]],
[
'shadow/package' => [
Expand Down Expand Up @@ -76,7 +79,7 @@ public function testPrintResult(): void
});
$verboseOutput = $this->getFormatterNormalizedOutput(static function ($formatter) use ($analysisResult): void {
$options = new CliOptions();
$options->verbose = true;
$options->showAllUsages = true;
$formatter->format($analysisResult, $options, new Configuration());
});

Expand All @@ -85,7 +88,7 @@ public function testPrintResult(): void
<testsuites>
<testsuite name="unknown classes" failures="1">
<testcase name="Unknown\Thing">
<failure>app/init.php:1093</failure>
<failure>app/init.php:1091</failure>
</testcase>
</testsuite>
<testsuite name="unknown functions" failures="1">
Expand Down Expand Up @@ -120,6 +123,7 @@ public function testPrintResult(): void
<testsuites>
<testsuite name="unknown classes" failures="1">
<testcase name="Unknown\Thing">
<failure>app/init.php:1091</failure>
<failure>app/init.php:1093</failure>
</testcase>
</testsuite>
Expand All @@ -135,9 +139,11 @@ public function testPrintResult(): void
<testcase name="shadow/package">
<failure message="Forth\Provider">src/bootstrap.php:873</failure>
<failure message="Shadow\Comparator">src/Printer.php:25</failure>
<failure message="Shadow\Utils">src/Utils.php:19
src/Utils.php:22
src/Application.php:128</failure>
<failure message="Shadow\Utils">src/Utils.php:19</failure>
<failure message="Shadow\Utils">src/Utils.php:22</failure>
<failure message="Shadow\Utils">src/Application.php:128</failure>
<failure message="Shadow\Utils">src/Controller.php:229</failure>
<failure message="Third\Parser">src/bootstrap.php:317</failure>
</testcase>
</testsuite>
<testsuite name="dev dependencies in production code" failures="1">
Expand All @@ -151,7 +157,7 @@ public function testPrintResult(): void
<testsuite name="unused dependencies" failures="1">
<testcase name="dead/package"></testcase>
</testsuite>
<!-- showing only first 3 example failure usages -->
<!-- showing all failure usages -->
</testsuites>
OUT;

Expand Down

0 comments on commit 7feaa97

Please sign in to comment.