From 1e20307c6e3d7cdf2a05e86ddf9baa3722f21ad6 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Sat, 25 Jan 2025 11:54:42 +0100 Subject: [PATCH] Add quiet-coverage option --- src/Plugins/Coverage.php | 24 ++++++++++++++++++++++-- src/Plugins/Help.php | 5 ++++- src/Support/Coverage.php | 9 ++++++--- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Plugins/Coverage.php b/src/Plugins/Coverage.php index a5061d258..11bfa47bb 100644 --- a/src/Plugins/Coverage.php +++ b/src/Plugins/Coverage.php @@ -32,6 +32,11 @@ final class Coverage implements AddsOutput, HandlesArguments */ private const EXACTLY_OPTION = 'exactly'; + /** + * @var string + */ + private const QUIET_COVERAGE_OPTION = 'quiet-coverage'; + /** * Whether it should show the coverage or not. */ @@ -52,6 +57,11 @@ final class Coverage implements AddsOutput, HandlesArguments */ public ?float $coverageExactly = null; + /** + * Whether it should hide files where coverage is 100% or not + */ + public bool $quietCoverage = false; + /** * Creates a new Plugin instance. */ @@ -66,7 +76,12 @@ 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, self::EXACTLY_OPTION] as $option) { + foreach ([ + self::COVERAGE_OPTION, + self::MIN_OPTION, + self::EXACTLY_OPTION, + self::QUIET_COVERAGE_OPTION + ] as $option) { if ($original === sprintf('--%s', $option)) { return true; } @@ -89,6 +104,7 @@ public function handleArguments(array $originals): array $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); + $inputs[] = new InputOption(self::QUIET_COVERAGE_OPTION, null, InputOption::VALUE_NONE); $input = new ArgvInput($arguments, new InputDefinition($inputs)); if ((bool) $input->getOption(self::COVERAGE_OPTION)) { @@ -129,6 +145,10 @@ public function handleArguments(array $originals): array $this->coverageExactly = (float) $exactlyOption; } + if ((bool) $input->getOption(self::COVERAGE_OPTION)) { + $this->quietCoverage = true; + } + if ($_SERVER['COLLISION_PRINTER_COMPACT'] ?? false) { $this->compact = true; } @@ -153,7 +173,7 @@ public function addOutput(int $exitCode): int exit(1); } - $coverage = \Pest\Support\Coverage::report($this->output, $this->compact); + $coverage = \Pest\Support\Coverage::report($this->output, $this->compact, $this->quietCoverage); $exitCode = (int) ($coverage < $this->coverageMin); if ($exitCode === 0 && $this->coverageExactly !== null) { diff --git a/src/Plugins/Help.php b/src/Plugins/Help.php index 89a47b66e..e5ff4660d 100644 --- a/src/Plugins/Help.php +++ b/src/Plugins/Help.php @@ -156,7 +156,10 @@ private function getContent(): array ], [ 'arg' => '--coverage --min', 'desc' => 'Set the minimum required coverage percentage, and fail if not met', - ], ...$content['Code Coverage']]; + ], ...$content['Code Coverage'], [ + 'arg' => '--quiet-coverage ', + 'desc' => 'Do not report any files where code coverage is 100%', + ]]; $content['Mutation Testing'] = [[ 'arg' => '--mutate ', diff --git a/src/Support/Coverage.php b/src/Support/Coverage.php index 955bbfc46..29807e50b 100644 --- a/src/Support/Coverage.php +++ b/src/Support/Coverage.php @@ -74,8 +74,11 @@ 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, bool $compact = false): float - { + public static function report( + OutputInterface $output, + bool $compact = false, + bool $quietCoverage = false, + ): float { if (! file_exists($reportPath = self::getPath())) { if (self::usingXdebug()) { $output->writeln( @@ -113,7 +116,7 @@ public static function report(OutputInterface $output, bool $compact = false): f ? '100.0' : number_format($file->percentageOfExecutedLines()->asFloat(), 1, '.', ''); - if ($percentage === '100.0' && $compact) { + if ($percentage === '100.0' && ($compact || $quietCoverage)) { continue; }