Merge pull request #1626 from SimonBroekaert/feat/add_only-covered_option

feat: add '--only-covered' option to '--coverage'
This commit is contained in:
nuno maduro
2026-04-10 12:53:17 +01:00
committed by GitHub
3 changed files with 25 additions and 3 deletions

View File

@ -23,6 +23,8 @@ final class Coverage implements AddsOutput, HandlesArguments
private const string EXACTLY_OPTION = 'exactly'; private const string EXACTLY_OPTION = 'exactly';
private const string ONLY_COVERED_OPTION = 'only-covered';
/** /**
* Whether it should show the coverage or not. * Whether it should show the coverage or not.
*/ */
@ -43,6 +45,11 @@ final class Coverage implements AddsOutput, HandlesArguments
*/ */
public ?float $coverageExactly = null; public ?float $coverageExactly = null;
/**
* Whether it should show only covered files.
*/
public bool $showOnlyCovered = false;
/** /**
* Creates a new Plugin instance. * Creates a new Plugin instance.
*/ */
@ -57,7 +64,7 @@ final class Coverage implements AddsOutput, HandlesArguments
public function handleArguments(array $originals): array public function handleArguments(array $originals): array
{ {
$arguments = [...[''], ...array_values(array_filter($originals, function (string $original): bool { $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::ONLY_COVERED_OPTION] as $option) {
if ($original === sprintf('--%s', $option)) { if ($original === sprintf('--%s', $option)) {
return true; return true;
} }
@ -80,6 +87,7 @@ final class Coverage implements AddsOutput, HandlesArguments
$inputs[] = new InputOption(self::COVERAGE_OPTION, null, InputOption::VALUE_NONE); $inputs[] = new InputOption(self::COVERAGE_OPTION, null, InputOption::VALUE_NONE);
$inputs[] = new InputOption(self::MIN_OPTION, null, InputOption::VALUE_REQUIRED); $inputs[] = new InputOption(self::MIN_OPTION, null, InputOption::VALUE_REQUIRED);
$inputs[] = new InputOption(self::EXACTLY_OPTION, null, InputOption::VALUE_REQUIRED); $inputs[] = new InputOption(self::EXACTLY_OPTION, null, InputOption::VALUE_REQUIRED);
$inputs[] = new InputOption(self::ONLY_COVERED_OPTION, null, InputOption::VALUE_NONE);
$input = new ArgvInput($arguments, new InputDefinition($inputs)); $input = new ArgvInput($arguments, new InputDefinition($inputs));
if ((bool) $input->getOption(self::COVERAGE_OPTION)) { if ((bool) $input->getOption(self::COVERAGE_OPTION)) {
@ -120,6 +128,10 @@ final class Coverage implements AddsOutput, HandlesArguments
$this->coverageExactly = (float) $exactlyOption; $this->coverageExactly = (float) $exactlyOption;
} }
if ((bool) $input->getOption(self::ONLY_COVERED_OPTION)) {
$this->showOnlyCovered = true;
}
if ($_SERVER['COLLISION_PRINTER_COMPACT'] ?? false) { if ($_SERVER['COLLISION_PRINTER_COMPACT'] ?? false) {
$this->compact = true; $this->compact = true;
} }
@ -144,7 +156,7 @@ final class Coverage implements AddsOutput, HandlesArguments
exit(1); exit(1);
} }
$coverage = \Pest\Support\Coverage::report($this->output, $this->compact); $coverage = \Pest\Support\Coverage::report($this->output, $this->compact, $this->showOnlyCovered);
$exitCode = (int) ($coverage < $this->coverageMin); $exitCode = (int) ($coverage < $this->coverageMin);
if ($exitCode === 0 && $this->coverageExactly !== null) { if ($exitCode === 0 && $this->coverageExactly !== null) {

View File

@ -167,6 +167,12 @@ final readonly class Help implements HandlesArguments
], [ ], [
'arg' => '--coverage --min', 'arg' => '--coverage --min',
'desc' => 'Set the minimum required coverage percentage, and fail if not met', 'desc' => 'Set the minimum required coverage percentage, and fail if not met',
], [
'arg' => '--coverage --exactly',
'desc' => 'Set the exact required coverage percentage, and fail if not met',
], [
'arg' => '--coverage --only-covered',
'desc' => 'Hide files with 0% coverage from the code coverage report',
], ...$content['Code Coverage']]; ], ...$content['Code Coverage']];
$content['Mutation Testing'] = [[ $content['Mutation Testing'] = [[

View File

@ -74,7 +74,7 @@ final class Coverage
* Reports the code coverage report to the * Reports the code coverage report to the
* console and returns the result in float. * 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 $showOnlyCovered = false): float
{ {
if (! file_exists($reportPath = self::getPath())) { if (! file_exists($reportPath = self::getPath())) {
if (self::usingXdebug()) { if (self::usingXdebug()) {
@ -109,6 +109,10 @@ final class Coverage
$basename, $basename,
]); ]);
if ($showOnlyCovered && $file->percentageOfExecutedLines()->asFloat() === 0.0) {
continue;
}
$percentage = $file->numberOfExecutableLines() === 0 $percentage = $file->numberOfExecutableLines() === 0
? '100.0' ? '100.0'
: number_format($file->percentageOfExecutedLines()->asFloat(), 1, '.', ''); : number_format($file->percentageOfExecutedLines()->asFloat(), 1, '.', '');