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 c06fe6c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/Report/Text.php
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;
}
}
14 changes: 14 additions & 0 deletions 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)
10 changes: 10 additions & 0 deletions tests/tests/Report/TextTest.php
Expand Up @@ -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);
Expand Down

0 comments on commit c06fe6c

Please sign in to comment.