From c06fe6c3d120f278c86d67f50afe9c3d2b39ea21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 9 Mar 2024 16:02:32 +0100 Subject: [PATCH] Fix: Pad lines in code coverage report only when colors are shown --- src/Report/Text.php | 37 ++++++++++++------- ...NamespacedBankAccount-text-with-colors.txt | 14 +++++++ tests/tests/Report/TextTest.php | 10 +++++ 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 tests/_files/NamespacedBankAccount-text-with-colors.txt diff --git a/src/Report/Text.php b/src/Report/Text.php index 56b23746..a307aa4b 100644 --- a/src/Report/Text.php +++ b/src/Report/Text.php @@ -10,9 +10,13 @@ namespace SebastianBergmann\CodeCoverage\Report; use const PHP_EOL; +use function array_map; use function date; use function ksort; +use function max; use function sprintf; +use function str_pad; +use function strlen; use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\Node\File; use SebastianBergmann\CodeCoverage\Util\Percentage; @@ -156,28 +160,31 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin $report->numberOfExecutableLines(), ); + $padding = max(array_map('strlen', [$classes, $methods, $lines])); + if ($this->showOnlySummary) { - $title = 'Code Coverage Report Summary:'; + $title = 'Code Coverage Report Summary:'; + $padding = max($padding, strlen($title)); - $output .= $this->format($colors['header'], $title); + $output .= $this->format($colors['header'], $padding, $title); } else { $date = date(' Y-m-d H:i:s'); $title = 'Code Coverage Report:'; - $output .= $this->format($colors['header'], $title); - $output .= $this->format($colors['header'], $date); - $output .= $this->format($colors['header'], ''); - $output .= $this->format($colors['header'], ' Summary:'); + $output .= $this->format($colors['header'], $padding, $title); + $output .= $this->format($colors['header'], $padding, $date); + $output .= $this->format($colors['header'], $padding, ''); + $output .= $this->format($colors['header'], $padding, ' Summary:'); } - $output .= $this->format($colors['classes'], $classes); - $output .= $this->format($colors['methods'], $methods); + $output .= $this->format($colors['classes'], $padding, $classes); + $output .= $this->format($colors['methods'], $padding, $methods); if ($hasBranchCoverage) { - $output .= $this->format($colors['paths'], $paths); - $output .= $this->format($colors['branches'], $branches); + $output .= $this->format($colors['paths'], $padding, $paths); + $output .= $this->format($colors['branches'], $padding, $branches); } - $output .= $this->format($colors['lines'], $lines); + $output .= $this->format($colors['lines'], $padding, $lines); if ($this->showOnlySummary) { return $output . PHP_EOL; @@ -297,10 +304,12 @@ private function printCoverageCounts(int $numberOfCoveredElements, int $totalNum sprintf($format, $totalNumberOfElements) . ')'; } - private function format(string $color, false|string $string): string + private function format(string $color, int $padding, false|string $string): string { - $reset = $color ? self::COLOR_RESET : ''; + if ($color === '') { + return (string) $string . PHP_EOL; + } - return $color . (string) $string . $reset . PHP_EOL; + return $color . str_pad((string) $string, $padding) . self::COLOR_RESET . PHP_EOL; } } diff --git a/tests/_files/NamespacedBankAccount-text-with-colors.txt b/tests/_files/NamespacedBankAccount-text-with-colors.txt new file mode 100644 index 00000000..83f3901e --- /dev/null +++ b/tests/_files/NamespacedBankAccount-text-with-colors.txt @@ -0,0 +1,14 @@ + + +Code Coverage Report:  + %s  +  + Summary:  + Classes: 0.00% (0/1) + Methods: 75.00% (3/4) + Lines: 62.50% (5/8) + +SomeNamespace\BankAccount + Methods: ( 0/ 0) Lines: ( 0/ 0) +SomeNamespace\BankAccountTrait + Methods: 75.00% ( 3/ 4) Lines: 62.50% ( 5/ 8) diff --git a/tests/tests/Report/TextTest.php b/tests/tests/Report/TextTest.php index 16a99d36..8cefd57c 100644 --- a/tests/tests/Report/TextTest.php +++ b/tests/tests/Report/TextTest.php @@ -57,6 +57,16 @@ public function testTextForNamespacedBankAccountTest(): void ); } + public function testTextForNamespacedBankAccountTestWhenColorsAreEnabled(): void + { + $text = new Text(Thresholds::default(), true, false); + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'NamespacedBankAccount-text-with-colors.txt', + str_replace(PHP_EOL, "\n", $text->process($this->getLineCoverageForNamespacedBankAccount(), true)), + ); + } + public function testTextForFileWithIgnoredLines(): void { $text = new Text(Thresholds::default(), false, false);