Skip to content

fix: accept space separated values for --min and --exactly - #1921

Open
YoussefMansour9 wants to merge 1 commit into
pestphp:5.xfrom
YoussefMansour9:fix/coverage-space-separated-value
Open

YoussefMansour9 wants to merge 1 commit into
pestphp:5.xfrom
YoussefMansour9:fix/coverage-space-separated-value

Conversation

@YoussefMansour9

Copy link
Copy Markdown

What

--min and --exactly only accept the --min=50 notation. With the space separated
notation the run dies with an unhandled exception:

$ pest --coverage --min 50

  Symfony\Component\Console\Exception\RuntimeException

  The "--min" option requires a value.

  at vendor/symfony/console/Input/ArgvInput.php:257
  ...
  7  src/Plugins/Coverage.php:74
     Symfony\Component\Console\Input\ArgvInput::__construct(...)
  8  src/Plugins/Actions/CallsHandleArguments.php:25
     Pest\Plugins\Coverage::handleArguments()

The same applies to --exactly 100.

Why it happens

Coverage::handleArguments() picks its own options out of the argument list with
array_filter(), matching only --min and --min=…. With --coverage --min 50
the --min token is collected but the separate 50 token is not, so ArgvInput
gets a VALUE_REQUIRED option with nothing after it and throws. As a side effect the
orphaned 50 is left in the forwarded arguments and reaches PHPUnit as a positional
argument.

This is inconsistent with the rest of the CLI — valued options that Pest forwards to
PHPUnit accept both notations, so pest --filter add works fine.

The fix

handleArguments() now walks the arguments positionally rather than filtering them,
so an option that takes a value also claims the token that follows it. Deliberately
preserved behaviour:

  • --coverage and --only-covered take no value and never consume the next token,
    so pest --only-covered --filter add still forwards --filter add untouched.
  • A bare --min with no value still produces the normal "requires a value" error
    instead of swallowing the option that follows it.
  • Positional arguments and PHPUnit options are forwarded unchanged.

Tests

Added to tests/Plugins/Coverage.php, covering both notations for --min and
--exactly, that PHPUnit-owned arguments are not consumed, and that value-less
options do not eat the following token. The four new cases fail on 5.x and pass
with this change.

Full suite is green locally (1588 passed, both serial and --parallel), plus
pint, rector --dry-run and phpstan.

`--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.

@MrPunyapal MrPunyapal left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I reproduced the crash on 5.x, so the fix is welcome. The extraction logic is correct. One test case is missing.

Comment thread src/Plugins/Coverage.php

$value = $originals[$index + 1] ?? null;

if ($requiresValue && $value !== null && ! Str::startsWith($value, '-')) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing tests this guard. Your description says a bare --min still raises the requires-a-value error instead of taking the next option, but the only new cases exercise a value that is present. Add a case where handleArguments(['--min', '--filter', 'add']) throws Symfony\Component\Console\Exception\RuntimeException.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants