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
40 changes: 38 additions & 2 deletions src/Plugins/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pest\Contracts\Plugins\AddsOutput;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Support\Str;
use Pest\TestSuite;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -25,6 +26,8 @@ final class Coverage implements AddsOutput, HandlesArguments

private const string ONLY_COVERED_OPTION = 'only-covered';

private const string ONLY_CHANGED_OPTION = 'only-changed';

/**
* Whether it should show the coverage or not.
*/
Expand All @@ -50,6 +53,11 @@ final class Coverage implements AddsOutput, HandlesArguments
*/
public bool $showOnlyCovered = false;

/**
* Whether coverage should be limited to files changed on the current branch.
*/
public bool $onlyChanged = false;

/**
* Creates a new Plugin instance.
*/
Expand All @@ -64,7 +72,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, self::ONLY_COVERED_OPTION] as $option) {
foreach ([self::COVERAGE_OPTION, self::MIN_OPTION, self::EXACTLY_OPTION, self::ONLY_COVERED_OPTION, self::ONLY_CHANGED_OPTION] as $option) {
if ($original === sprintf('--%s', $option)) {
return true;
}
Expand All @@ -88,6 +96,7 @@ public function handleArguments(array $originals): array
$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);
$inputs[] = new InputOption(self::ONLY_CHANGED_OPTION, null, InputOption::VALUE_NONE);

$input = new ArgvInput($arguments, new InputDefinition($inputs));
if ((bool) $input->getOption(self::COVERAGE_OPTION)) {
Expand Down Expand Up @@ -132,6 +141,10 @@ public function handleArguments(array $originals): array
$this->showOnlyCovered = true;
}

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

if ($_SERVER['COLLISION_PRINTER_COMPACT'] ?? false) {
$this->compact = true;
}
Expand All @@ -156,7 +169,30 @@ public function addOutput(int $exitCode): int
exit(1);
}

$coverage = \Pest\Support\Coverage::report($this->output, $this->compact, $this->showOnlyCovered);
$onlyChangedPaths = null;
$onlyChangedLineSetsByNormalizedPath = null;

if ($this->onlyChanged) {
$changed = \Pest\Support\Coverage::resolveBranchChangedPhpPaths(TestSuite::getInstance()->rootPath);

if (! $changed['ok']) {
if (file_exists($path = \Pest\Support\Coverage::getPath())) {
@unlink($path);
}

$this->output->writeln([
'',
' <fg=default;bg=red;options=bold> ERROR </> '.($changed['errorMessage'] ?? 'Branch-scoped coverage failed.'),
'',
]);
exit(1);
}

$onlyChangedPaths = $changed['absolutePhpPaths'];
$onlyChangedLineSetsByNormalizedPath = $changed['lineSetsByNormalizedPath'];
}

$coverage = \Pest\Support\Coverage::report($this->output, $this->compact, $this->showOnlyCovered, $onlyChangedPaths, $onlyChangedLineSetsByNormalizedPath);
$exitCode = (int) ($coverage < $this->coverageMin);

if ($exitCode === 0 && $this->coverageExactly !== null) {
Expand Down
3 changes: 3 additions & 0 deletions src/Plugins/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ private function getContent(): array
], [
'arg' => '--coverage --only-covered',
'desc' => 'Hide files with 0% coverage from the code coverage report',
], [
'arg' => '--coverage --only-changed',
'desc' => 'Generate code coverage report and output to standard output for changed PHP files',
], ...$content['Code Coverage']];

$content['Mutation Testing'] = [[
Expand Down
Loading
Loading