[Bug]: [mutate] A --mutate run given --filter scores every mutant as tested
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
The fix lives in pestphp/pest-plugin-mutate, not pest: in src/MutationTest.php, start() builds $filteredArguments from the run's arguments before appending the mutant's own --filter, and hasFinished()/isSuccessful() treats a non-zero child exit as a kill. Reproduce with the four given files and run vendor/bin/pest --mutate --path=src/Calc.php tests/CalcTest.php --filter=triples; stock prints 3 tested, 50.00%. Done when that prints 3 untested, 0 tested, 0.00% while --filter=doubles still prints 3 tested, 50.00%.
Written by the indexing model from the issue text.
Description
What Happened
A --mutate run that is given --filter scores every mutant of the tests it selected as tested (killed), whether or not any test kills it. Expected: the score the same run gives without the duplicate option.
MutationTest::start() builds each mutant's child command from the run's own arguments ($filteredArguments, only --coverage* removed) and then appends its own --filter="...". With the run's --filter among those arguments the child gets two. PHPUnit's CLI builder emits Option --filter cannot be used more than once when an option is seen a second time (TextUI/Configuration/Cli/Builder.php, markProcessed()) and the child exits 1 even though its test passes. MutationTest::hasFinished() then reads the non-zero exit as a kill (isSuccessful() is the only path to Untested), so the mutant is scored tested.
In the project below, triples calls Calc::triple() and asserts nothing about the result, so its 3 mutants must be untested. With --filter=triples they are all scored tested.
| Run | Mutations | Score | Mutant child exits |
|---|---|---|---|
--filter=triples, stock |
3 uncovered, 3 tested | 50.00% | 1, 1, 1, each with the option warning |
--filter=triples, with the change below |
3 untested, 3 uncovered, 0 tested | 0.00% | 0, 0, 0 |
--filter=doubles, stock |
3 uncovered, 3 tested | 50.00% | 1, 1, 1, each with the option warning |
--filter=doubles, with the change below |
3 uncovered, 3 tested | 50.00% | 1, 1, 1 (genuine failures) |
no --filter, stock |
3 untested, 3 tested | 50.00% | 0, 0, 0 and 255, 255, 255 (no warning) |
The doubles rows are the control that the change does not turn kills into escapes: those three mutants are killed by doubles and stay tested.
How to Reproduce
composer.json
{
"require-dev": {
"pestphp/pest": "5.3.7",
"pestphp/pest-plugin-mutate": "5.0.2"
},
"autoload": { "psr-4": { "Repro\\": "src/" } },
"config": { "allow-plugins": { "pestphp/pest-plugin": true } }
}
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites><testsuite name="Unit"><directory>tests</directory></testsuite></testsuites>
<source><include><directory>src</directory></include></source>
</phpunit>
src/Calc.php
<?php
declare(strict_types=1);
namespace Repro;
final class Calc
{
public static function double(int $n): int
{
return $n * 2;
}
public static function triple(int $n): int|float
{
return $n * 3;
}
}
tests/CalcTest.php
<?php
declare(strict_types=1);
use Repro\Calc;
it('doubles', function (): void {
expect(Calc::double(2))->toBe(4);
});
it('triples without checking the result', function (): void {
Calc::triple(2);
expect(true)->toBeTrue();
});
it('quadruples through double', function (): void {
expect(Calc::double(Calc::double(1)))->toBe(4);
});
Then, in that directory (on Windows use vendor\bin\pest.bat, which the mutant child needs):
composer install
vendor/bin/pest --mutate --path=src/Calc.php tests/CalcTest.php --filter=triples
Printed: Mutations: 3 uncovered, 3 tested and Score: 50.00%. Expected: Mutations: 3 untested, 3 uncovered, 0 tested and Score: 0.00%.
The option error on its own, without --mutate:
vendor/bin/pest tests/CalcTest.php --filter=triples --filter=triples
echo $?
Printed: the warning Option --filter cannot be used more than once, Tests: 1 passed (1 assertions), and exit status 1. With one --filter the status is 0.
Proposed fix
Drop the run's --filter (both the --filter=<value> and the two-argument form) from the child's arguments. The covering tests were collected from the filtered run, so the mutant's own --filter already selects only tests inside the run's selection: in the project above, --filter=doubles makes the child filter CalcTest::(.*)it.doubles, while the unfiltered run's child filter for the same line is CalcTest::(.*)it.doubles|CalcTest::(.*)it.quadruples.through.double.
--- a/src/MutationTest.php
+++ b/src/MutationTest.php
@@ -87,6 +87,33 @@
// remove coverage arguments from the original arguments
$filteredArguments = array_filter($originalArguments, fn (string $argument): bool => ! str_starts_with($argument, '--coverage'));
+ // The mutant gets its own --filter below. PHPUnit warns "Option --filter cannot be used more
+ // than once" and exits 1 when it is given two, which hasFinished() reads as a killed mutant.
+ // The covering tests were collected from the filtered run, so they are already inside the
+ // run's selection.
+ $withoutRunFilter = [];
+ $skipValue = false;
+ foreach ($filteredArguments as $argument) {
+ if ($skipValue) {
+ $skipValue = false;
+
+ continue;
+ }
+
+ if ($argument === '--filter') {
+ $skipValue = true;
+
+ continue;
+ }
+
+ if (str_starts_with($argument, '--filter=')) {
+ continue;
+ }
+
+ $withoutRunFilter[] = $argument;
+ }
+ $filteredArguments = $withoutRunFilter;
+
// TODO: filter arguments to remove unnecessary stuff (Teamcity, Coverage, etc.)
$process = new Process(
command: [
I can open this as a pull request against pestphp/pest-plugin-mutate if that is useful. pestphp/pest-plugin-mutate#43 (FilterArgument) and #45 (MutantArguments) change neighbouring lines of the same method and neither removes the run's --filter.
Sample Repository
The four files above.
Pest Version
5.3.7, with pestphp/pest-plugin-mutate 5.0.2 (tag v5.0.2, commit f09af7e81bef6ec30cfabed6d36a472e05a8f317), PHPUnit 13.4.7, ParaTest 7.29.0.
PHP Version
8.4.27
Operation System
Windows (Windows 11, run through Git Bash with vendor\bin\pest.bat).
Notes
- Read from source, not run: the option check is PHPUnit's and does not depend on the platform; the exit status of a child that only warns was measured on Windows only. Linux and macOS were not run.
- Related, different defects:
pestphp/pest#1927(the child's own quoted--filteron Windows),pestphp/pest#1771andpestphp/pest#2018(filter length),pestphp/pest#1937(ParaTest options inherited by the child). The same shape, an inherited argument making the child exit non-zero before it runs a test, is the rootpestphp/pest#2018names. - Checked before filing: no report of a duplicated
--filteramong the issues and pull requests ofpestphp/pestandpestphp/pest-plugin-mutatematchingfilter twice,cannot be used more than onceandduplicate filter mutate; the items those searches return were read and are about other defects.
- Dominant language
- PHP
- Stars
- 11.8k
- Forks
- 563
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 39
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pestphp/pest
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 2 days
-
[Bug]: [mutate] Windows: StreamWrapper::stream_lock() always takes LOCK_SH, so LOCK_EX writes fail in a mutant child and the mutant counts as killedPossibly taken @joshdaugherty claimed this 2 days ago. Open
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
[Bug]: --mutate hides unreadable files, so mutants behind a failed read can never be killedMay be free again A pull request for this issue was closed without being merged. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
[Bug]: [mutate] Windows: @pest-mutate-ignore comments land on line 1 in files with LF line endingsPossibly taken @webdevsamran claimed this 64 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
Similar issues
-
bug needs grooming
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
phpstan/phpstan-phpunit#361 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
HE-Arc/le-panier-du-coin#49 ·
Maintainers usually reply within 1 day
-
15.0
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
woocommerce/woocommerce-subscriptions-importer-exporter#298 ·