Skip to content

Commit

Permalink
feat: option to coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Dec 1, 2024
1 parent 982353f commit 5d32dd0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
44 changes: 41 additions & 3 deletions src/Plugins/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ final class Coverage implements AddsOutput, HandlesArguments
*/
private const MIN_OPTION = 'min';

/**
* @var string
*/
private const EXACTLY_OPTION = 'exactly';

/**
* Whether it should show the coverage or not.
*/
Expand All @@ -37,6 +42,11 @@ final class Coverage implements AddsOutput, HandlesArguments
*/
public float $coverageMin = 0.0;

/**
* The exactly coverage.
*/
public ?float $coverageExactly = null;

/**
* Creates a new Plugin instance.
*/
Expand All @@ -51,7 +61,7 @@ public function __construct(private readonly OutputInterface $output)
public function handleArguments(array $originals): array
{
$arguments = [...[''], ...array_values(array_filter($originals, function (string $original): bool {
foreach ([self::COVERAGE_OPTION, self::MIN_OPTION] as $option) {
foreach ([self::COVERAGE_OPTION, self::MIN_OPTION, self::EXACTLY_OPTION] as $option) {
if ($original === sprintf('--%s', $option)) {
return true;
}
Expand All @@ -73,6 +83,7 @@ public function handleArguments(array $originals): array
$inputs = [];
$inputs[] = new InputOption(self::COVERAGE_OPTION, null, InputOption::VALUE_NONE);
$inputs[] = new InputOption(self::MIN_OPTION, null, InputOption::VALUE_REQUIRED);
$inputs[] = new InputOption(self::EXACTLY_OPTION, null, InputOption::VALUE_REQUIRED);

$input = new ArgvInput($arguments, new InputDefinition($inputs));
if ((bool) $input->getOption(self::COVERAGE_OPTION)) {
Expand Down Expand Up @@ -106,6 +117,13 @@ public function handleArguments(array $originals): array
$this->coverageMin = (float) $minOption;
}

if ($input->getOption(self::EXACTLY_OPTION) !== null) {
/** @var int|float $exactlyOption */
$exactlyOption = $input->getOption(self::EXACTLY_OPTION);

$this->coverageExactly = (float) $exactlyOption;
}

return $originals;
}

Expand All @@ -127,10 +145,22 @@ public function addOutput(int $exitCode): int
}

$coverage = \Pest\Support\Coverage::report($this->output);

$exitCode = (int) ($coverage < $this->coverageMin);

if ($exitCode === 1) {
if ($exitCode === 0 && $this->coverageExactly !== null) {
$comparableCoverage = $this->computeComparableCoverage($coverage);
$comparableCoverageExactly = $this->computeComparableCoverage($this->coverageExactly);

$exitCode = $comparableCoverage === $comparableCoverageExactly ? 0 : 1;

if ($exitCode === 1) {
$this->output->writeln(sprintf(
"\n <fg=white;bg=red;options=bold> FAIL </> Code coverage not exactly <fg=white;options=bold> %s %%</>, currently <fg=red;options=bold> %s %%</>.",
number_format($this->coverageExactly, 1),
number_format(floor($coverage * 10) / 10, 1),
));
}
} elseif ($exitCode === 1) {
$this->output->writeln(sprintf(
"\n <fg=white;bg=red;options=bold> FAIL </> Code coverage below expected <fg=white;options=bold> %s %%</>, currently <fg=red;options=bold> %s %%</>.",
number_format($this->coverageMin, 1),
Expand All @@ -143,4 +173,12 @@ public function addOutput(int $exitCode): int

return $exitCode;
}

/**
* Computes the comparable coverage to a percentage with one decimal.
*/
private function computeComparableCoverage(float $coverage): float
{
return floor($coverage * 10) / 10;
}
}
2 changes: 1 addition & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1698,4 +1698,4 @@
WARN Tests\Visual\Version
- visual snapshot of help command output

Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 33 skipped, 1144 passed (2736 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 24 skipped, 1142 passed (2720 assertions)
23 changes: 23 additions & 0 deletions tests/Plugins/Coverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Pest\Plugins\Coverage;
use Symfony\Component\Console\Output\NullOutput;

test('compute comparable coverage', function (float $givenValue, float $expectedValue) {
$output = new NullOutput();

$plugin = new Coverage($output);

$comparableCoverage = (fn () => $this->computeComparableCoverage($givenValue))->call($plugin);

expect($comparableCoverage)->toBe($expectedValue);
})->with([
[0, 0],
[0.5, 0.5],
[1.0, 1.0],
[32.51, 32.5],
[32.12312321312312, 32.1],
[32.53333333333333, 32.5],
[32.57777771232132, 32.5],
[100.0, 100.0],
]);
2 changes: 1 addition & 1 deletion tests/Visual/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

test('parallel', function () use ($run) {
expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 24 skipped, 1134 passed (2712 assertions)')
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 24 skipped, 1142 passed (2720 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();

Expand Down

0 comments on commit 5d32dd0

Please sign in to comment.