Skip to content

Commit

Permalink
Merge pull request #919 from WendellAdriel/feature/coverage-errors-on…
Browse files Browse the repository at this point in the history
…ly-flag-2

[2.x] Print only files below the min coverage
  • Loading branch information
nunomaduro authored Dec 17, 2023
2 parents 4522cb5 + 8ea7b2b commit 1e2ca40
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/Plugins/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ final class Coverage implements AddsOutput, HandlesArguments
*/
private const MIN_OPTION = 'min';

/**
* @var string
*/
private const ERRORS_ONLY_OPTION = 'errors-only';

/**
* @var string[]
*/
private const ALLOWED_OPTIONS = [
self::COVERAGE_OPTION,
self::MIN_OPTION,
self::ERRORS_ONLY_OPTION,
];

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

/**
* Whether it should show only errors or not.
*/
public bool $errorsOnly = false;

/**
* Creates a new Plugin instance.
*/
Expand All @@ -51,7 +70,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::ALLOWED_OPTIONS as $option) {
if ($original === sprintf('--%s', $option)) {
return true;
}
Expand All @@ -73,6 +92,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::ERRORS_ONLY_OPTION, null, InputOption::VALUE_NONE);

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

if ((bool) $input->getOption(self::ERRORS_ONLY_OPTION)) {
$this->errorsOnly = true;
}

return $originals;
}

Expand All @@ -122,7 +146,7 @@ public function addOutput(int $exitCode): int
exit(1);
}

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

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

Expand Down
6 changes: 5 additions & 1 deletion src/Support/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function usingXdebug(): bool
* Reports the code coverage report to the
* console and returns the result in float.
*/
public static function report(OutputInterface $output): float
public static function report(OutputInterface $output, float $coverageMin, bool $showErrorsOnly): float
{
if (! file_exists($reportPath = self::getPath())) {
if (self::usingXdebug()) {
Expand Down Expand Up @@ -126,6 +126,10 @@ public static function report(OutputInterface $output): float

$truncateAt = max(1, terminal()->width() - 12);

if ($showErrorsOnly && (float) $percentage >= $coverageMin) {
continue;
}

renderUsing($output);
render(<<<HTML
<div class="flex mx-2">
Expand Down
12 changes: 12 additions & 0 deletions tests/Features/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
expect($plugin->coverageMin)->toEqual(2.4);
});

it('adds coverage if --errors-only exist', function () {
$plugin = new CoveragePlugin(new ConsoleOutput());
expect($plugin->errorsOnly)->toBeFalse()
->and($plugin->coverage)->toBeFalse();

$plugin->handleArguments([]);
expect($plugin->errorsOnly)->toBeFalse();

$plugin->handleArguments(['--errors-only']);
expect($plugin->errorsOnly)->toBeTrue();
});

it('generates coverage based on file input', function () {
expect(Coverage::getMissingCoverage(new class()
{
Expand Down

0 comments on commit 1e2ca40

Please sign in to comment.