Skip to content
Merged
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

## [Unreleased]

### Fixed

- **No mutation score is given when the tests do not pass on unchanged code run as a mutant.** Pest counts a mutant's process that fails as a caught mutant, and one that loops as a timeout, caught too, so a suite that breaks in that process whatever the file contains scored mutants nothing had tested (on `pollora/media`, one file read 100 % with Patchwork 2.2.5 and 77.92 % with its unreleased fix). Before Pest runs, the harness now runs the suite alone, then the tests covering the code with `PEST_MUTATION_TESTING` set to the source file and `PEST_MUTATION_FILE` to an exact copy of it; selected by name as Pest selects them, never passing `--parallel`; if they fail (twice: one flaky failure is not a verdict), or are still running after five times the duration of the suite (never less than 120 s), Pest is not started and the gate fails with the failing tests. When they only fail with the variables of a parallel run, the mutants are run in a single process. **A repository whose score the gate accepted may now be refused**: Patchwork (loaded by Brain Monkey) under tests that read a directory, and Pest's own stream wrapper, which takes every `flock()` as shared and reports an unreadable file as absent, are two causes. The README says what to do.

- A run of the canary that ran out of memory says so: a `php -d memory_limit=...` on the command does not reach the processes of the tests, as it does not reach those of Pest's mutants.
- The advice to give `file` back to a bootstrap now carries its condition, only when Pest's wrapper is on it: unconditionally, `StreamWrapper::disable()` fails every test PHPUnit isolates in a process of its own. The README has the code.

### Added

- `check --mutation-canary[=FILES]` asks only whether a mutation score could be believed, without a mutation pass or any other gate, and exits with a failure when it could not, in any mode.

### Documentation

- The README gives the security patterns a template runs (SEC-01 to SEC-07 and CONV-10) and says `mutation.min` has no default for `changed-relative`; the headers of the reusable workflows call a current tag.
Expand Down
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ The **Dependency analysis** gate uses the repository's `composer-dependency-anal
| `pollora-qa check --strict` | Also fails when a gate could not run because a tool is missing (Semgrep, a `vendor/bin/` tool) |
| `pollora-qa check --coverage[=FILE]` | Runs the tests with coverage and compares the Clover report with the ratchet (default `.harness/coverage.xml`) |
| `pollora-qa check --mutation` | Also runs Pest's mutation testing (`pest --mutate --everything --covered-only`) and compares its score with the ratchet's `msi`; reported, never blocking by itself |
| `pollora-qa check --mutation-canary[=FILES]` | Runs no gate: only checks that a mutation score could be believed (mutants load, tests pass on unchanged code run as a mutant) |
| `pollora-qa check --mutation --mutation-base=REF` | With a limited `mutation.scope` in `.harness.json`, the commit the changed files are counted from (and, for `changed-relative`, the base each file is measured on) (default `HEAD^1`) |
| `pollora-qa promote-report [--json] [<path>... \| --root=<dir> [--only=a,b]]` | For each observed rule, what would block here if it became enforced (read only, see [Promoting a rule](#promoting-a-rule)); with paths or `--root`, one rule x repository matrix |
| `pollora-qa check --mutation --mutation-scope=all\|changed\|added\|changed-relative` | What `--mutation` mutates in this run, over the `mutation.scope` of `.harness.json`. A limited scope is held to `mutation.min`, or to 50 % when the file sets none; an unknown value fails before any gate runs |
Expand Down Expand Up @@ -229,14 +230,55 @@ The run is parallel. A suite that fails that way (a global function a helper dec

**No cache.** Pest keeps the mutants it generated in `.temp/pest-mutate-cache` and does not check that their files exist: after a run that could not write them, the next one reads 100 % with no warning. Every run of the harness passes `--no-cache`, so mutants are generated again each time and the cache of a developer is neither read nor written. A `pest --mutate` typed by hand still uses it: `--clear-cache` empties it.

#### A score needs proof that mutants load
#### A score needs proof that mutants load, and that the tests pass on unchanged code

Pest hands a mutant to its process through `PEST_MUTATION_TESTING` and `PEST_MUTATION_FILE` and serves it through a stream wrapper on `file`. If something else serves the original (Patchwork, which Brain Monkey loads, or a bootstrap), mutants survive unseen or their processes die and count as killed. After every run that has a score, in every scope, the harness therefore starts Pest as one of those processes with a "mutant" that only records that it was executed, and stops it. No record, no score: the gate fails, saying why (on the base of `changed-relative`, `base unmeasured`). A real 100 % passes, because what is judged is the record, not the value.

- **With files** (`changed`, `added`, `changed-relative`), each file is probed with the real suite. A file included without its mutant refuses the score. A file no test includes has no covered line, hence no mutant, and says nothing; but at least one file of the set must have run its mutant (the suite is tried once more with `--parallel` before concluding that none did).
- **Whole repository**, there is no single file: the harness writes `src/PolloraCanary<random>.php` and a test next to the others that loads Patchwork (when installed) and requires it, runs that test and removes both.
- **Cost.** Nothing more when a bootstrap loads the mutant at once (the framework: 0.1 s) or when a test includes the file early; otherwise as long as the suite takes to reach the file. A refusal costs a whole run of the suite.
- **What it cannot see.** That the processes of the mutants fail for a reason of their own (a test that only fails under `--filter`, a flaky one): they count as killed. A wrapper replaced only by some tests, which the probe does not run before the file is read. For the whole repository, a bootstrap that loads some files and not others. A `pest --mutate` run outside `pollora-qa`.
**The tests must also pass on code that did not change.** A mutant's process that fails is a caught mutant for Pest, one that loops is a timeout, counted as caught too: a suite that breaks there whatever the file contains scores mutants nothing tested. So, before Pest runs:

1. **The suite runs alone** (`pest`, then `pest --parallel` if that fails, with coverage first, then without). It gives the duration and, with a coverage driver (pcov, Xdebug), which tests cover which file. When it does not pass, there is nothing to compare with.
2. **The tests run as a mutant of an unchanged file**: `PEST_MUTATION_TESTING` is the source file, `PEST_MUTATION_FILE` an exact copy of it. As Pest does, only the tests that cover the file are selected, by name (`--filter`, in several runs when too long for PCRE; for the whole repository, the first source file stands for the others, with every test that covers a source file), and `--parallel` is never passed: the variables of a ParaTest worker are set instead. With pest-plugin-mutate 3, whose mutants collect coverage, so does this run. Without coverage data every test runs, which can refuse a score for a test no mutant of the file would run.
3. **They must exit 0** (a run that fails is tried once more) within five times the duration of step 1, and never less than 120 seconds. Otherwise Pest is not started and the gate fails, listing the failing tests. If they only fail with the worker variables, the mutants are run in a single process instead.

Each of these runs has a temporary directory of its own (`TMPDIR`), removed afterwards, so that what a failing run leaves behind does not fail the next one. A test that starts further processes in a mutant's process (PHPUnit's `RunTestsInSeparateProcesses`) has them inherit `PEST_MUTATION_TESTING` without Pest's wrapper: a bootstrap must not assume the wrapper is there (`StreamWrapper::disable()` then raises a notice, `file:// was never changed`, which fails the test).

Two causes have been seen. **Patchwork** (Brain Monkey loads it) stacked on Pest's stream wrapper breaks directory reading ([antecedent/patchwork#235](https://github.com/antecedent/patchwork/pull/235)). **Pest's wrapper itself** takes every `flock()` as a shared lock and reports a file nobody may read as absent, so a test of locking or permissions fails whatever Patchwork does. Both go away when the test bootstrap, if `PEST_MUTATION_TESTING` is set, gives `file` back and loads `PEST_MUTATION_FILE` itself. It gives it back **only when Pest's wrapper is on it**: a process PHPUnit isolates a test in has the variables but not the wrapper. At the top of the bootstrap, before Patchwork is loaded (it puts its own wrapper on `file`):

```php
$mutating = is_string(getenv('PEST_MUTATION_TESTING')) && class_exists(\Pest\Mutate\Support\StreamWrapper::class);

if ($mutating) {
$handle = fopen(__FILE__, 'r');
$wrapped = (stream_get_meta_data($handle)['wrapper_data'] ?? null) instanceof \Pest\Mutate\Support\StreamWrapper;
fclose($handle);

if ($wrapped) {
\Pest\Mutate\Support\StreamWrapper::disable();
}
}

// Patchwork, Composer's autoloader and the helpers of the tests are loaded here

if ($mutating) {
require (string) getenv('PEST_MUTATION_FILE');
}
```

That `require` is enough for a file that can be included on its own. For `__DIR__` and `__FILE__` in the mutated file, see the framework's `tests/bootstrap.php`.

**To check a repository without a mutation pass** (a few runs of the suite, no gate, no report):

```bash
vendor/bin/pollora-qa check --mutation-canary # the whole repository
vendor/bin/pollora-qa check --mutation-canary=src/A.php,src/B.php # some files, as the limited scopes do
```

It exits 1 when no score could be believed, whatever the mode of the repository, and when `pest-plugin-mutate` is not installed. Give it a coverage driver, as the mutation run has.

- **Cost.** Two runs of the suite or of the covering tests (the control, then one per file; more when a run fails, which is tried again, and without the worker variables), then the canary: nothing more when a bootstrap loads the mutant at once (the framework: 0.1 s) or when a test includes the file early; otherwise as long as the suite takes to reach the file. A refusal costs a whole run of the suite.
- **What it cannot see.** Pest selects tests line by line: a test that only fails in the smaller selection of one line is not seen, nor a flaky one: they count as killed. A test that fails only now and then as a mutant: a failing run is tried once more, Pest tries each mutant once. A test that is slower as a mutant than Pest's own limit (the duration of its first run, plus 5 s or 20 %) yet within the limit above. A wrapper replaced only by some tests, which the probe does not run before the file is read. For the whole repository, a bootstrap that loads some files and not others. A `pest --mutate` run outside `pollora-qa`.

**Two traps of Pest's mutation runner.**
- Pest builds its `--filter` as regex alternatives, one per test that covers the mutated line. Past the PCRE compilation limit (about 64 KB) the filter is invalid, no test runs and the mutant is counted as surviving. The lines covered by almost the whole suite are the ones concerned, and adding tests can lower the score there: fewer, broader tests (datasets folded into loops) beat more of them.
Expand Down Expand Up @@ -280,7 +322,7 @@ Only `scope` is needed; the other keys are optional and shown with their default
- **New files** (absent from the base) are held to `min`, as `added` does; a renamed file is compared with its old path. A file with no mutant on the change (an interface) is listed, not failed; with no mutant on the base, it is held to `min`.
- **Base unmeasured.** When the base cannot be measured (its tests fail there, a guard below refuses its score, its suite needs a file git ignores), a regression cannot be proved: the file is held to `min`, and the line says `base unmeasured` with the reason. The change must reach the fixed floor, so that a broken base never makes a file pass for free.
- **Nothing is measured twice for nothing.** The change is measured first. A file at or above `100 - tolerance` cannot have dropped beyond the tolerance, so its base is not measured (the line says so).
- **A score is refused rather than believed**, on the change and on the base, in the cases that already gave a false figure: Pest could not write its temporary files (every mutant process fails and counts as killed: 100 %; the gate fails and names the directory); the test bootstrap loads **Patchwork** (Brain Monkey does) and Patchwork's stream wrapper replaced Pest's, so the mutants never load (every one survives) or the process dies (every one counts as killed, 100 % that no test earned). Pest's compact report shows neither, and the line `\Patchwork\CodeManipulation\Stream::reinstateWrapper();` also appears in the output of a healthy run (the framework's), so the gate does not read the output for it. It refuses on what it can see: a repository that has `antecedent/patchwork` and no test bootstrap (`tests/*.php`, `tests/*/*.php`, `phpunit.xml*`) that reads `PEST_MUTATION_TESTING` fails before any run, naming the fix (the framework's `tests/bootstrap.php` gives the wrapper back to Pest); and a run in which **at least 5 mutants all survive** (`859 untested, 0 tested`, the first failure above) is `unmeasured`. The check on the repository only proves the bootstrap *mentions* the variable: whether it works is checked by running it (see [A score needs proof that mutants load](#a-score-needs-proof-that-mutants-load)). A genuine 0 % on 4 mutants is judged like any other score; a genuine 100 % is believed.
- **A score is refused rather than believed**, on the change and on the base, in the cases that already gave a false figure: the tests fail on unchanged code run as a mutant (above); Pest could not write its temporary files (every mutant process fails and counts as killed: 100 %; the gate fails and names the directory); the test bootstrap loads **Patchwork** (Brain Monkey does) and Patchwork's stream wrapper replaced Pest's, so the mutants never load (every one survives) or the process dies (every one counts as killed, 100 % that no test earned). Pest's compact report shows neither, and the line `\Patchwork\CodeManipulation\Stream::reinstateWrapper();` also appears in the output of a healthy run (the framework's), so the gate does not read the output for it. It refuses on what it can see: a repository that has `antecedent/patchwork` and no test bootstrap (`tests/*.php`, `tests/*/*.php`, `phpunit.xml*`) that reads `PEST_MUTATION_TESTING` fails before any run, naming the fix (the framework's `tests/bootstrap.php` gives the wrapper back to Pest); and a run in which **at least 5 mutants all survive** (`859 untested, 0 tested`, the first failure above) is `unmeasured`. The check on the repository only proves the bootstrap *mentions* the variable: whether it works is checked by running it (see [A score needs proof that mutants load](#a-score-needs-proof-that-mutants-load)). A genuine 0 % on 4 mutants is judged like any other score; a genuine 100 % is believed.
- **The cost is capped, and the cap fails.** Two passes per file: measured on the framework, a small file of 49 mutants takes about three minutes a pass (see the CHANGELOG), so the defaults are `max_files: 3` and `max_minutes: 15`. A change that touches more files than `max_files` fails **before anything runs**, naming the files and what to do (raise the key, split the change, measure by hand): measuring only some would let the others through. A release pull request does not reach this limit in the reusable workflow, which does not run the gate there (above); run by hand, `check` applies it as to any other change. During the run, a pass that the passes so far say would run past `max_minutes` is not started; the gate then fails and lists each file it did not reach (`not measured: the time budget ran out`), never green. A pass already started is not interrupted, so the job can overrun by one pass; the `timeout-minutes: 30` of the quality job stays the hard limit.
- **What it does not see.** A change that only edits tests touches no source file and measures nothing (as with the other scopes). A regression spread over files the change does not touch is invisible. A renamed file is found by git's similarity detection (`-M`); a copied file counts as new. A suite whose result depends on files git ignores, or on a classmap-authoritative autoloader of the change, may read `base unmeasured`. The parallel and the single-process runs do not give exactly the same counts (above), and the gate does not know which of the two measured each side: that is what the tolerance is for.

Expand Down
Loading
Loading