diff --git a/src/Plugins/Coverage.php b/src/Plugins/Coverage.php index ed7b1214..0da42d2c 100644 --- a/src/Plugins/Coverage.php +++ b/src/Plugins/Coverage.php @@ -50,12 +50,14 @@ final class Coverage implements AddsOutput, HandlesArguments */ public bool $showOnlyCovered = false; + private string $coveragePath; + /** * Creates a new Plugin instance. */ public function __construct(private readonly OutputInterface $output) { - // .. + $this->coveragePath = \Pest\Support\Coverage::getPath(); } /** @@ -77,12 +79,6 @@ final class Coverage implements AddsOutput, HandlesArguments return false; }))]; - $originals = array_flip($originals); - foreach ($arguments as $argument) { - unset($originals[$argument]); - } - $originals = array_flip($originals); - $inputs = []; $inputs[] = new InputOption(self::COVERAGE_OPTION, null, InputOption::VALUE_NONE); $inputs[] = new InputOption(self::MIN_OPTION, null, InputOption::VALUE_REQUIRED); @@ -90,10 +86,26 @@ final class Coverage implements AddsOutput, HandlesArguments $inputs[] = new InputOption(self::ONLY_COVERED_OPTION, null, InputOption::VALUE_NONE); $input = new ArgvInput($arguments, new InputDefinition($inputs)); + $coveragePathProvided = false; + if ((bool) $input->getOption(self::COVERAGE_OPTION)) { $this->coverage = true; - $originals[] = '--coverage-php'; - $originals[] = \Pest\Support\Coverage::getPath(); + + foreach ($originals as $index => $original) { + if (Str::startsWith($original, '--coverage-php=')) { + $this->coveragePath = Str::after($original, '--coverage-php='); + $coveragePathProvided = true; + + break; + } + + if ($original === '--coverage-php' && isset($originals[$index + 1])) { + $this->coveragePath = $originals[$index + 1]; + $coveragePathProvided = true; + + break; + } + } if (! \Pest\Support\Coverage::isAvailable()) { if (\Pest\Support\Coverage::usingXdebug()) { @@ -114,6 +126,17 @@ final class Coverage implements AddsOutput, HandlesArguments } } + $originals = array_flip($originals); + foreach ($arguments as $argument) { + unset($originals[$argument]); + } + $originals = array_flip($originals); + + if ((bool) $input->getOption(self::COVERAGE_OPTION) && ! $coveragePathProvided) { + $originals[] = '--coverage-php'; + $originals[] = $this->coveragePath; + } + if ($input->getOption(self::MIN_OPTION) !== null) { /** @var int|float $minOption */ $minOption = $input->getOption(self::MIN_OPTION); @@ -156,7 +179,12 @@ final class Coverage implements AddsOutput, HandlesArguments exit(1); } - $coverage = \Pest\Support\Coverage::report($this->output, $this->compact, $this->showOnlyCovered); + $coverage = \Pest\Support\Coverage::report($this->output, $this->compact, $this->showOnlyCovered, $this->coveragePath); + + if ($this->coveragePath === \Pest\Support\Coverage::getPath()) { + unlink($this->coveragePath); + } + $exitCode = (int) ($coverage < $this->coverageMin); if ($exitCode === 0 && $this->coverageExactly !== null) { diff --git a/src/Support/Coverage.php b/src/Support/Coverage.php index a36dd8ed..5b27ebca 100644 --- a/src/Support/Coverage.php +++ b/src/Support/Coverage.php @@ -74,9 +74,9 @@ final class Coverage * Reports the code coverage report to the * console and returns the result in float. */ - public static function report(OutputInterface $output, bool $compact = false, bool $showOnlyCovered = false): float + public static function report(OutputInterface $output, bool $compact, bool $showOnlyCovered, string $reportPath): float { - if (! file_exists($reportPath = self::getPath())) { + if (! file_exists($reportPath)) { if (self::usingXdebug()) { $output->writeln( " WARN Unable to get coverage using Xdebug. Did you set Xdebug's coverage mode?", @@ -90,7 +90,6 @@ final class Coverage /** @var CodeCoverage $codeCoverage */ $codeCoverage = require $reportPath; - unlink($reportPath); $totalCoverage = $codeCoverage->getReport()->percentageOfExecutedLines(); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index c0d2a14c..8e43580f 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -76,6 +76,7 @@ WARN Tests\Features\Coverage ✓ it has plugin - it adds coverage if --coverage exist → Coverage is not available + - it keeps the explicit coverage path when --coverage exists → Coverage is not available ✓ it adds coverage if --min exist ✓ it generates coverage based on file input @@ -1905,5 +1906,4 @@ PASS Testsexternal\Features\Expect\toMatchSnapshot ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1297 passed (2980 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 36 skipped, 1297 passed (2980 assertions) diff --git a/tests/Features/Coverage.php b/tests/Features/Coverage.php index 55ce4164..ce69d5dc 100644 --- a/tests/Features/Coverage.php +++ b/tests/Features/Coverage.php @@ -19,6 +19,26 @@ it('adds coverage if --coverage exist', function () { ->and($plugin->coverage)->toBeTrue(); })->skip(! Coverage::isAvailable() || ! function_exists('xdebug_info') || ! in_array('coverage', xdebug_info('mode'), true), 'Coverage is not available'); +it('keeps the explicit coverage path when --coverage exists', function () { + foreach ([ + ['--coverage-php=test.cov'], + ['--coverage-php', 'test.cov'], + ] as $coverageArguments) { + $plugin = new CoveragePlugin(new ConsoleOutput); + + $arguments = $plugin->handleArguments([ + '--coverage', + '--coverage-text', + ...$coverageArguments, + ]); + + expect(array_values($arguments))->toEqual([ + '--coverage-text', + ...$coverageArguments, + ]); + } +})->skip(! Coverage::isAvailable() || ! function_exists('xdebug_info') || ! in_array('coverage', xdebug_info('mode'), true), 'Coverage is not available'); + it('adds coverage if --min exist', function () { $plugin = new CoveragePlugin(new ConsoleOutput); expect($plugin->coverageMin)->toEqual(0.0)