Skip to content

Option to suppress code coverage target validation #6472

@AegirLeet

Description

@AegirLeet

References: infection/infection#2440 and infection/infection#2848

Quick summary:

Infection is a mutation testing tool. It makes small changes to a project's source code (such as inverting the condition inside an if), runs the test suite and checks whether the tests still pass.
Before doing any of this, Infection needs to do an initial run of the test suite to gather coverage information. It does this by generating a new, temporary PHPUnit config (based on the project's config) and invoking PHPUnit.

Infection supports a --filter option that allows users to specifcy a list of files or directories to consider. It will only generate mutations for those files.
If this option is used, the generated PHPUnit config is modified to only list the relevant files or directories in <source><include>.

Problem:

If a project uses #[CoversClass()]/#[UsesClass()], PHPUnit (since version 12 I believe) will run some validation on those attributes in

if ($covers instanceof TargetCollection) {
$result = $this->codeCoverage->validate($covers);
if ($result->isFailure()) {
assert($result instanceof ValidationFailure);
EventFacade::emitter()->testTriggeredPhpunitWarning(
$this->test->valueObjectForEvents(),
$result->message(),
);
$append = false;
}
}
if ($uses instanceof TargetCollection) {
$result = $this->codeCoverage->validate($uses);
if ($result->isFailure()) {
assert($result instanceof ValidationFailure);
EventFacade::emitter()->testTriggeredPhpunitWarning(
$this->test->valueObjectForEvents(),
$result->message(),
);
$append = false;
}
}

Because the full test suite is being run but the config <source> includes only a small subset of source files, an exception is thrown in https://github.com/sebastianbergmann/php-code-coverage/blob/3c97001bca11db4a3e6eee52f6e6924617364c3a/src/Target/Mapper.php#L82 and a "X is not a valid target for code coverage" warning is emitted.
This, combined with failOnPhpunitWarning="true" (the default and probably generally advisable setting), leads to a failed initial test run, which in turn leads Infection to abort its run.

Possible solution:

A number of options were explored in the issues I linked above. The TLDR is that this is difficult to properly handle on Infection's end.
The most straightforward solution would be to add a "don't validate code coverage targets" config option to PHPUnit.
Is that something PHPUnit would consider adding?

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature/code-coverageIssues related to code coverage (but not php-code-coverage)type/enhancementA new idea that should be implemented

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions