Skip to content

Commit aa7e0c8

Browse files
localheinzsebastianbergmann
authored andcommitted
Fix: Pad lines in code coverage report only when colors are shown
1 parent c600ab6 commit aa7e0c8

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/Report/Text.php

+23-14
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

1212
use const PHP_EOL;
13+
use function array_map;
1314
use function date;
1415
use function ksort;
16+
use function max;
1517
use function sprintf;
18+
use function str_pad;
19+
use function strlen;
1620
use SebastianBergmann\CodeCoverage\CodeCoverage;
1721
use SebastianBergmann\CodeCoverage\Node\File;
1822
use SebastianBergmann\CodeCoverage\Util\Percentage;
@@ -156,28 +160,31 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
156160
$report->numberOfExecutableLines(),
157161
);
158162

163+
$padding = max(array_map('strlen', [$classes, $methods, $lines]));
164+
159165
if ($this->showOnlySummary) {
160-
$title = 'Code Coverage Report Summary:';
166+
$title = 'Code Coverage Report Summary:';
167+
$padding = max($padding, strlen($title));
161168

162-
$output .= $this->format($colors['header'], $title);
169+
$output .= $this->format($colors['header'], $padding, $title);
163170
} else {
164171
$date = date(' Y-m-d H:i:s');
165172
$title = 'Code Coverage Report:';
166173

167-
$output .= $this->format($colors['header'], $title);
168-
$output .= $this->format($colors['header'], $date);
169-
$output .= $this->format($colors['header'], '');
170-
$output .= $this->format($colors['header'], ' Summary:');
174+
$output .= $this->format($colors['header'], $padding, $title);
175+
$output .= $this->format($colors['header'], $padding, $date);
176+
$output .= $this->format($colors['header'], $padding, '');
177+
$output .= $this->format($colors['header'], $padding, ' Summary:');
171178
}
172179

173-
$output .= $this->format($colors['classes'], $classes);
174-
$output .= $this->format($colors['methods'], $methods);
180+
$output .= $this->format($colors['classes'], $padding, $classes);
181+
$output .= $this->format($colors['methods'], $padding, $methods);
175182

176183
if ($hasBranchCoverage) {
177-
$output .= $this->format($colors['paths'], $paths);
178-
$output .= $this->format($colors['branches'], $branches);
184+
$output .= $this->format($colors['paths'], $padding, $paths);
185+
$output .= $this->format($colors['branches'], $padding, $branches);
179186
}
180-
$output .= $this->format($colors['lines'], $lines);
187+
$output .= $this->format($colors['lines'], $padding, $lines);
181188

182189
if ($this->showOnlySummary) {
183190
return $output . PHP_EOL;
@@ -297,10 +304,12 @@ private function printCoverageCounts(int $numberOfCoveredElements, int $totalNum
297304
sprintf($format, $totalNumberOfElements) . ')';
298305
}
299306

300-
private function format(string $color, false|string $string): string
307+
private function format(string $color, int $padding, false|string $string): string
301308
{
302-
$reset = $color ? self::COLOR_RESET : '';
309+
if ($color === '') {
310+
return (string) $string . PHP_EOL;
311+
}
303312

304-
return $color . (string) $string . $reset . PHP_EOL;
313+
return $color . str_pad((string) $string, $padding) . self::COLOR_RESET . PHP_EOL;
305314
}
306315
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
Code Coverage Report: 
4+
 %s 
5+
 
6+
 Summary: 
7+
 Classes: 0.00% (0/1)
8+
 Methods: 75.00% (3/4)
9+
 Lines: 62.50% (5/8)
10+
11+
SomeNamespace\BankAccount
12+
Methods: ( 0/ 0) Lines: ( 0/ 0)
13+
SomeNamespace\BankAccountTrait
14+
Methods: 75.00% ( 3/ 4) Lines: 62.50% ( 5/ 8)

tests/tests/Report/TextTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ public function testTextForNamespacedBankAccountTest(): void
5757
);
5858
}
5959

60+
public function testTextForNamespacedBankAccountTestWhenColorsAreEnabled(): void
61+
{
62+
$text = new Text(Thresholds::default(), true, false);
63+
64+
$this->assertStringMatchesFormatFile(
65+
TEST_FILES_PATH . 'NamespacedBankAccount-text-with-colors.txt',
66+
str_replace(PHP_EOL, "\n", $text->process($this->getLineCoverageForNamespacedBankAccount(), true)),
67+
);
68+
}
69+
6070
public function testTextForFileWithIgnoredLines(): void
6171
{
6272
$text = new Text(Thresholds::default(), false, false);

0 commit comments

Comments
 (0)