Skip to content

Commit

Permalink
Improves coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Mar 2, 2023
1 parent 91da4cd commit 2845876
Showing 1 changed file with 29 additions and 43 deletions.
72 changes: 29 additions & 43 deletions src/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace NunoMaduro\Collision;

use RuntimeException;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Node\Directory;
use SebastianBergmann\CodeCoverage\Node\File;
use SebastianBergmann\Environment\Runtime;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
use function Termwind\render;
use function Termwind\renderUsing;
use function Termwind\terminal;

/**
* @internal
Expand Down Expand Up @@ -88,10 +91,6 @@ public static function report(OutputInterface $output): float

$totalCoverage = $codeCoverage->getReport()->percentageOfExecutedLines();

$totalWidth = (new Terminal())->getWidth();

$dottedLineLength = $totalWidth;

/** @var Directory<File|Directory> $report */
$report = $codeCoverage->getReport();

Expand All @@ -106,51 +105,38 @@ public static function report(OutputInterface $output): float
$dirname,
$basename,
]);
$rawName = $dirname === '.' ? $basename : implode(DIRECTORY_SEPARATOR, [
$dirname,
$basename,
]);

$linesExecutedTakenSize = 0;

if ($file->percentageOfExecutedLines()->asString() != '0.00%') {
$linesExecutedTakenSize = strlen($uncoveredLines = trim(implode(', ', self::getMissingCoverage($file)))) + 1;
$name .= sprintf(' <fg=red>%s</>', $uncoveredLines);
}

$percentage = $file->numberOfExecutableLines() === 0
? '100.0'
: number_format($file->percentageOfExecutedLines()->asFloat(), 1, '.', '');

$takenSize = strlen($rawName.$percentage) + 8 + $linesExecutedTakenSize; // adding 3 space and percent sign
$color = $percentage === '100.0' ? 'green' : ($percentage === '0.0' ? 'red' : 'yellow');

$percentage = sprintf(
'<fg=%s%s>%s</>',
$percentage === '100.0' ? 'green' : ($percentage === '0.0' ? 'red' : 'yellow'),
$percentage === '100.0' ? ';options=bold' : '',
$percentage
);
$truncateAt = max(1, terminal()->width() - 12);

$output->writeln(sprintf(
' <fg=white>%s</> <fg=#6C7280>%s</> %s <fg=#6C7280>%%</>',
$name,
str_repeat('.', max($dottedLineLength - $takenSize, 1)),
$percentage
));
renderUsing($output);
render(<<<HTML
<div class="flex mx-2">
<span class="truncate-{$truncateAt}">{$name}</span>
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
<span class="text-{$color}">{$percentage}%</span>
</div>
HTML);
}

$output->writeln('');

$rawName = 'Total Coverage';

$takenSize = strlen($rawName.$totalCoverage->asString()) + 6;
$totalCoverageAsString = $totalCoverage->asFloat() === 0.0
? '0.0'
: number_format($totalCoverage->asFloat(), 1, '.', '');

$output->writeln(sprintf(
' <fg=white;options=bold>%s</> <fg=#6C7280>%s</> %s <fg=#6C7280>%%</>',
$rawName,
str_repeat('.', max($dottedLineLength - $takenSize, 1)),
number_format($totalCoverage->asFloat(), 1, '.', '')
));
renderUsing($output);
render(<<<HTML
<div class="mx-2">
<hr class="text-gray" />
<div class="w-full text-right">
<span class="ml-1 font-bold">Total: {$totalCoverageAsString} %</span>
</div>
</div>
HTML);

return $totalCoverage->asFloat();
}
Expand All @@ -170,7 +156,7 @@ public static function getMissingCoverage($file): array
$shouldBeNewLine = true;

$eachLine = function (array $array, array $tests, int $line) use (&$shouldBeNewLine): array {
if (count($tests) > 0) {
if ($tests !== []) {
$shouldBeNewLine = true;

return $array;
Expand All @@ -185,8 +171,8 @@ public static function getMissingCoverage($file): array

$lastKey = count($array) - 1;

if (array_key_exists($lastKey, $array) && str_contains($array[$lastKey], '..')) {
[$from] = explode('..', $array[$lastKey]);
if (array_key_exists($lastKey, $array) && str_contains((string) $array[$lastKey], '..')) {
[$from] = explode('..', (string) $array[$lastKey]);
$array[$lastKey] = $line > $from ? sprintf('%s..%s', $from, $line) : sprintf('%s..%s', $line, $from);

return $array;
Expand Down

0 comments on commit 2845876

Please sign in to comment.