Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Plugins/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ final class Coverage implements AddsOutput, HandlesArguments

private const string EXACTLY_OPTION = 'exactly';

private const string ONLY_COVERED_OPTION = 'only-covered';

/**
* Whether it should show the coverage or not.
*/
Expand All @@ -43,6 +45,11 @@ final class Coverage implements AddsOutput, HandlesArguments
*/
public ?float $coverageExactly = null;

/**
* Whether it should show only covered files.
*/
public bool $showOnlyCovered = false;

/**
* Creates a new Plugin instance.
*/
Expand All @@ -57,7 +64,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, 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)) {
return true;
}
Expand All @@ -80,6 +87,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::ONLY_COVERED_OPTION, null, InputOption::VALUE_NONE);

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

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

if ($_SERVER['COLLISION_PRINTER_COMPACT'] ?? false) {
$this->compact = true;
}
Expand All @@ -144,7 +156,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->showOnlyCovered);
$exitCode = (int) ($coverage < $this->coverageMin);

if ($exitCode === 0 && $this->coverageExactly !== null) {
Expand Down
6 changes: 6 additions & 0 deletions src/Plugins/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ private function getContent(): array
], [
'arg' => '--coverage --min',
'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['Mutation Testing'] = [[
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, bool $compact = false): float
public static function report(OutputInterface $output, bool $compact = false, bool $showOnlyCovered = false): float
{
if (! file_exists($reportPath = self::getPath())) {
if (self::usingXdebug()) {
Expand Down Expand Up @@ -109,6 +109,10 @@ public static function report(OutputInterface $output, bool $compact = false): f
$basename,
]);

if ($showOnlyCovered && $file->percentageOfExecutedLines()->asFloat() === 0.0) {
continue;
}

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