From 557aca75a0ce19ec40645937c77bd211bffed6cc Mon Sep 17 00:00:00 2001 From: Youssef Mansour Date: Thu, 3 Dec 2026 16:00:33 +0200 Subject: [PATCH] fix: accept space separated values for --min and --exactly (#1921) `--min` and `--exactly` only accepted the `--min=50` notation. When the space separated notation was used, the plugin kept the `--min` token but left the `50` token behind, so `ArgvInput` received an option without a value and threw: $ pest --coverage --min 50 Symfony\Component\Console\Exception\RuntimeException The "--min" option requires a value. The run aborted with an unhandled exception and a stack trace, even though every other valued option Pest forwards to PHPUnit accepts both notations (`pest --filter add` works). The orphaned value token was also forwarded to PHPUnit as a positional argument. `handleArguments()` now walks the arguments positionally instead of filtering them, so an option that takes a value also claims the token that follows it. Value-less options (`--coverage`, `--only-covered`) keep their current behaviour and never consume the next token, and a bare `--min` with no value still produces the regular "requires a value" error rather than swallowing the option that follows it. --- src/Plugins/Coverage.php | 71 +++++++++++++++++++++++++++++--------- tests/Plugins/Coverage.php | 33 ++++++++++++++++++ 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/src/Plugins/Coverage.php b/src/Plugins/Coverage.php index 3217dc6f..cd3b3759 100644 --- a/src/Plugins/Coverage.php +++ b/src/Plugins/Coverage.php @@ -27,6 +27,16 @@ final class Coverage implements AddsOutput, HandlesArguments private const string ONLY_COVERED_OPTION = 'only-covered'; + /** + * @var array + */ + private const array OPTIONS = [ + self::COVERAGE_OPTION => false, + self::MIN_OPTION => true, + self::EXACTLY_OPTION => true, + self::ONLY_COVERED_OPTION => false, + ]; + public bool $coverage = false; public bool $compact = false; @@ -47,23 +57,7 @@ final class Coverage implements AddsOutput, HandlesArguments */ 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) { - if ($original === sprintf('--%s', $option)) { - return true; - } - - if (Str::startsWith($original, sprintf('--%s=', $option))) { - return true; - } - } - - return false; - }))]; - - foreach ($arguments as $argument) { - $originals = $this->popArgument($argument, $originals); - } + [$arguments, $originals] = $this->extractOwnArguments($originals); $inputs = []; $inputs[] = new InputOption(self::COVERAGE_OPTION, null, InputOption::VALUE_NONE); @@ -121,6 +115,49 @@ final class Coverage implements AddsOutput, HandlesArguments return $originals; } + /** + * @param array $originals + * @return array{0: list, 1: list} + */ + private function extractOwnArguments(array $originals): array + { + $originals = array_values($originals); + + $owned = []; + $forwarded = []; + + for ($index = 0, $total = count($originals); $index < $total; $index++) { + $original = $originals[$index]; + + foreach (self::OPTIONS as $option => $requiresValue) { + if (Str::startsWith($original, sprintf('--%s=', $option))) { + $owned[] = $original; + + continue 2; + } + + if ($original !== sprintf('--%s', $option)) { + continue; + } + + $owned[] = $original; + + $value = $originals[$index + 1] ?? null; + + if ($requiresValue && $value !== null && ! Str::startsWith($value, '-')) { + $owned[] = $value; + $index++; + } + + continue 2; + } + + $forwarded[] = $original; + } + + return [['', ...$owned], $forwarded]; + } + /** * {@inheritdoc} */ diff --git a/tests/Plugins/Coverage.php b/tests/Plugins/Coverage.php index a9c581ef..fab82445 100644 --- a/tests/Plugins/Coverage.php +++ b/tests/Plugins/Coverage.php @@ -21,3 +21,36 @@ test('compute comparable coverage', function (float $givenValue, float $expected [32.57777771232132, 32.5], [100.0, 100.0], ]); + +test('coverage options accept a space separated value', function (array $arguments, float $min, ?float $exactly): void { + $plugin = new Coverage(new NullOutput); + + $plugin->handleArguments($arguments); + + expect($plugin->coverageMin)->toBe($min) + ->and($plugin->coverageExactly)->toBe($exactly); +})->with([ + 'min with equals sign' => [['--min=50'], 50.0, null], + 'min with space' => [['--min', '50'], 50.0, null], + 'exactly with equals sign' => [['--exactly=100'], 0.0, 100.0], + 'exactly with space' => [['--exactly', '100'], 0.0, 100.0], + 'both with space' => [['--min', '50', '--exactly', '100'], 50.0, 100.0], +]); + +test('coverage options do not consume arguments owned by phpunit', function (): void { + $plugin = new Coverage(new NullOutput); + + $arguments = $plugin->handleArguments(['--min', '50', '--filter', 'add', 'tests/Unit']); + + expect($plugin->coverageMin)->toBe(50.0) + ->and($arguments)->toBe(['--filter', 'add', 'tests/Unit']); +}); + +test('value less coverage options do not consume the next argument', function (): void { + $plugin = new Coverage(new NullOutput); + + $arguments = $plugin->handleArguments(['--only-covered', '--filter', 'add']); + + expect($plugin->showOnlyCovered)->toBeTrue() + ->and($arguments)->toBe(['--filter', 'add']); +});