Skip to content

Commit

Permalink
Fix: Pad lines in code coverage report only when colors are shown
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Mar 9, 2024
1 parent fb2c09c commit dd59ec9
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/Report/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Check warning on line 313 in src/Report/Text.php

View check run for this annotation

Codecov / codecov/patch

src/Report/Text.php#L313

Added line #L313 was not covered by tests
}
}

0 comments on commit dd59ec9

Please sign in to comment.