From dda6f2975f1362a7a6107a9c15e42058d23b8110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Krzy=C5=BCanowski?= Date: Wed, 28 Oct 2026 18:32:28 +0100 Subject: [PATCH 1/3] feat: mutate only what changed since a revision Narrows inside the runner rather than through --class, which lifts the __pest_mutate_only group and so runs different tests than a full run. --- src/Contracts/Printer.php | 3 + src/Options/ChangedOption.php | 28 ++ src/Repositories/ConfigurationRepository.php | 1 + src/Support/ChangedScope.php | 322 ++++++++++++++++++ .../Configuration/AbstractConfiguration.php | 15 +- .../Configuration/CliConfiguration.php | 6 + src/Support/Configuration/Configuration.php | 1 + src/Support/Printers/DefaultPrinter.php | 24 ++ src/Tester/MutationTestRunner.php | 22 +- .../HandlesCliProfileConfigurationTest.php | 14 + tests/Unit/Support/ChangedScopeTest.php | 158 +++++++++ .../Support/Printers/DefaultPrinterTest.php | 24 ++ 12 files changed, 616 insertions(+), 2 deletions(-) create mode 100644 src/Options/ChangedOption.php create mode 100644 src/Support/ChangedScope.php create mode 100644 tests/Unit/Support/ChangedScopeTest.php diff --git a/src/Contracts/Printer.php b/src/Contracts/Printer.php index 397e541..1625cab 100644 --- a/src/Contracts/Printer.php +++ b/src/Contracts/Printer.php @@ -7,6 +7,7 @@ use Pest\Mutate\MutationSuite; use Pest\Mutate\MutationTest; use Pest\Mutate\MutationTestCollection; +use Pest\Mutate\Support\ChangedScope; interface Printer { @@ -26,6 +27,8 @@ public function reportScoreNotReached(float $scoreReached, float $scoreRequired) public function printFilename(MutationTestCollection $testCollection): void; + public function reportChangedScope(ChangedScope $scope): void; + public function reportMutationGenerationStarted(MutationSuite $mutationSuite): void; public function reportMutationGenerationFinished(MutationSuite $mutationSuite): void; diff --git a/src/Options/ChangedOption.php b/src/Options/ChangedOption.php new file mode 100644 index 0000000..1082e6d --- /dev/null +++ b/src/Options/ChangedOption.php @@ -0,0 +1,28 @@ + + */ + private static array $testFilesByClass = []; + + /** + * @param array $files keyed by real path + */ + public function __construct( + public readonly string $revision, + public readonly string $base, + public readonly ?string $everythingBecause, + private readonly array $files = [], + ) {} + + /** + * @param iterable $sourceFiles + * @param array $sourcePaths + * @param array $testFiles + * @param array>> $coveredLines + */ + public static function since( + string $revision, + string $root, + iterable $sourceFiles, + array $sourcePaths, + string $testPath, + array $testFiles, + array $coveredLines, + ): self { + $base = self::git($root, ['merge-base', $revision, 'HEAD']) + ?? throw new InvalidOption("Mutating changes requires git and a common ancestor of [{$revision}] and HEAD. A shallow clone may need more history."); + $base = trim($base); + + $changed = [ + ...self::split(self::git($root, ['diff', '--name-only', '--no-renames', '--relative', '-z', $base, '--']) ?? ''), + ...self::split(self::git($root, ['ls-files', '--others', '--exclude-standard', '-z']) ?? ''), + ]; + + $sources = []; + + foreach ($sourceFiles as $file) { + $sources[$file->getRealPath()] = $file; + } + + $sourceRoots = array_map(fn (string $path): string => self::absolute($root, $path), $sourcePaths); + $testRoot = self::absolute($root, $testPath); + $testFiles = array_flip(array_map(fn (string $file): string => realpath($file) ?: $file, $testFiles)); + + $files = []; + $targets = []; + $changedTests = []; + + foreach (array_unique($changed) as $path) { + $absolute = $root.DIRECTORY_SEPARATOR.$path; + $real = realpath($absolute) ?: $absolute; + + if (isset($sources[$real])) { + $files[$real] = true; + + continue; + } + + if (self::within($real, $sourceRoots)) { + // A deleted or ignored source file has no mutants of its own. + if (str_ends_with($path, '.php')) { + continue; + } + + return new self($revision, $base, "{$path} changed"); + } + + if (self::within($real, [$testRoot])) { + $isDeletedTest = ! file_exists($absolute) && str_ends_with($path, '.php'); + + if (! isset($testFiles[$real]) && ! $isDeletedTest) { + return new self($revision, $base, "{$path} changed"); + } + + // The base side counts too: a target dropped from covers() loses the tests that killed its mutants. + foreach ([self::git($root, ['show', "{$base}:./{$path}"]), $isDeletedTest ? null : file_get_contents($absolute)] as $code) { + if (! is_string($code)) { + continue; + } + + $covered = self::coveredBy($code); + + if ($covered === null) { + return new self($revision, $base, "{$path} covers something other than class or namespace names"); + } + + if ($covered === [] && $isDeletedTest) { + return new self($revision, $base, "{$path} was deleted"); + } + + $targets = [...$targets, ...$covered]; + } + + $changedTests[$real] = true; + + continue; + } + + if (str_ends_with($path, '.php') || in_array($path, self::PROJECT_FILES, true)) { + return new self($revision, $base, "{$path} changed"); + } + } + + foreach (array_unique($targets) as $target) { + if (class_exists($target) || interface_exists($target) || trait_exists($target)) { + $file = (string) new ReflectionClass($target)->getFileName(); + $files[realpath($file) ?: $file] = true; + + continue; + } + + // mutates() also takes namespaces. + foreach ($sources as $real => $file) { + if (preg_match('/namespace\s+'.preg_quote($target, '/').'[\\\\\s;{]/', $file->getContents()) === 1) { + $files[$real] = true; + } + } + } + + // Mutants the changed tests used to kill may live in any file they run, not only in what they cover. + foreach ($coveredLines as $file => $lines) { + if (isset($files[$file])) { + continue; + } + + foreach ($lines as $tests) { + foreach ($tests as $test) { + if (isset($changedTests[self::testFile($test) ?? ''])) { + $files[$file] = true; + + continue 3; + } + } + } + } + + return new self($revision, $base, null, array_intersect_key($files, $sources)); + } + + public function includes(SplFileInfo $file): bool + { + return $this->everythingBecause !== null || isset($this->files[$file->getRealPath()]); + } + + /** + * @return array + */ + public function files(): array + { + return array_keys($this->files); + } + + /** + * @param array $arguments + */ + private static function git(string $root, array $arguments): ?string + { + $process = new Process(['git', ...$arguments], $root); + $process->run(); + + return $process->isSuccessful() ? $process->getOutput() : null; + } + + /** + * @return array + */ + private static function split(string $output): array + { + return array_values(array_filter(explode("\0", $output), fn (string $path): bool => $path !== '')); + } + + private static function absolute(string $root, string $path): string + { + $path = str_starts_with($path, DIRECTORY_SEPARATOR) ? $path : $root.DIRECTORY_SEPARATOR.$path; + + return rtrim(realpath($path) ?: $path, DIRECTORY_SEPARATOR); + } + + /** + * @param array $roots + */ + private static function within(string $path, array $roots): bool + { + return array_any($roots, fn (string $root): bool => $path === $root || str_starts_with($path, $root.DIRECTORY_SEPARATOR)); + } + + /** + * The classes and namespaces a test file passes to covers() or mutates(), or null when an argument isn't a literal name. + * + * @return array|null + */ + private static function coveredBy(string $code): ?array + { + try { + $statements = PhpParserFactory::make()->parse($code) ?? []; + } catch (Error) { + return null; + } + + $statements = new NodeTraverser(new NameResolver)->traverse($statements); + + $calls = (new NodeFinder)->find($statements, fn (Node $node): bool => match (true) { + $node instanceof FuncCall && $node->name instanceof Name => in_array(strtolower($node->name->getLast()), self::COVERING_CALLS, true), + $node instanceof MethodCall && $node->name instanceof Identifier => in_array($node->name->toLowerString(), self::COVERING_CALLS, true), + default => false, + }); + + $targets = []; + + foreach ($calls as $call) { + assert($call instanceof FuncCall || $call instanceof MethodCall); + + if ($call->isFirstClassCallable()) { + return null; + } + + foreach ($call->getArgs() as $argument) { + $names = $argument->unpack ? null : self::names($argument->value); + + if ($names === null) { + return null; + } + + $targets = [...$targets, ...$names]; + } + } + + return $targets; + } + + /** + * @return array|null + */ + private static function names(Expr $expression): ?array + { + if ($expression instanceof String_) { + return [ltrim($expression->value, '\\')]; + } + + if ($expression instanceof ClassConstFetch && $expression->class instanceof Name && ! $expression->class->isSpecialClassName() + && $expression->name instanceof Identifier && $expression->name->toLowerString() === 'class') { + return [$expression->class->toString()]; + } + + if (! $expression instanceof Array_) { + return null; + } + + $names = []; + + foreach ($expression->items as $item) { + $itemNames = $item->unpack ? null : self::names($item->value); + + if ($itemNames === null) { + return null; + } + + $names = [...$names, ...$itemNames]; + } + + return $names; + } + + /** + * The test file a coverage test id belongs to, when its generated test case is loaded. + */ + private static function testFile(string $testId): ?string + { + $class = explode('::', $testId)[0]; + + if (! array_key_exists($class, self::$testFilesByClass)) { + $filename = class_exists($class, false) && property_exists($class, '__filename') + ? new ReflectionClass($class)->getStaticPropertyValue('__filename') + : null; + + self::$testFilesByClass[$class] = is_string($filename) ? (realpath($filename) ?: $filename) : null; + } + + return self::$testFilesByClass[$class]; + } +} diff --git a/src/Support/Configuration/AbstractConfiguration.php b/src/Support/Configuration/AbstractConfiguration.php index e174b59..5997f85 100644 --- a/src/Support/Configuration/AbstractConfiguration.php +++ b/src/Support/Configuration/AbstractConfiguration.php @@ -58,6 +58,8 @@ abstract class AbstractConfiguration implements ConfigurationContract private ?bool $everything = null; + private ?string $changed = null; + /** * {@inheritDoc} */ @@ -194,7 +196,17 @@ public function everything(): self } /** - * @return array{covered_only?: bool, paths?: string[], paths_to_ignore?: string[], mutators?: class-string[], excluded_mutators?: class-string[], classes?: string[], parallel?: bool, processes?: int, profile?: bool, min_score?: float, ignore_min_score_on_zero_mutations?: bool, covered_only?: bool, stop_on_untested?: bool, stop_on_uncovered?: bool, mutation_id?: string, retry?: bool, everything?: bool} + * Only mutates what changed since the given git revision forked from HEAD. + */ + public function changed(string $revision = 'HEAD'): self + { + $this->changed = $revision; + + return $this; + } + + /** + * @return array{covered_only?: bool, paths?: string[], paths_to_ignore?: string[], mutators?: class-string[], excluded_mutators?: class-string[], classes?: string[], parallel?: bool, processes?: int, profile?: bool, min_score?: float, ignore_min_score_on_zero_mutations?: bool, covered_only?: bool, stop_on_untested?: bool, stop_on_uncovered?: bool, mutation_id?: string, retry?: bool, everything?: bool, changed?: string} */ public function toArray(): array { @@ -215,6 +227,7 @@ public function toArray(): array 'mutation_id' => $this->mutationId, 'retry' => $this->retry, 'everything' => $this->everything, + 'changed' => $this->changed, ], fn (mixed $value): bool => ! is_null($value)); } diff --git a/src/Support/Configuration/CliConfiguration.php b/src/Support/Configuration/CliConfiguration.php index ef7052e..74411f9 100644 --- a/src/Support/Configuration/CliConfiguration.php +++ b/src/Support/Configuration/CliConfiguration.php @@ -6,6 +6,7 @@ use Pest\Mutate\Cache\NullStore; use Pest\Mutate\Options\BailOption; +use Pest\Mutate\Options\ChangedOption; use Pest\Mutate\Options\ClassOption; use Pest\Mutate\Options\ClearCacheOption; use Pest\Mutate\Options\CoveredOnlyOption; @@ -53,6 +54,7 @@ class CliConfiguration extends AbstractConfiguration ClearCacheOption::class, EverythingOption::class, CoveredOnlyOption::class, + ChangedOption::class, ]; /** @@ -160,6 +162,10 @@ public function fromArguments(array $arguments): array $this->everything(); } + if ($input->hasOption(ChangedOption::ARGUMENT)) { + $this->changed((string) ($input->getOption(ChangedOption::ARGUMENT) ?? 'HEAD')); // @phpstan-ignore-line + } + return $arguments; } } diff --git a/src/Support/Configuration/Configuration.php b/src/Support/Configuration/Configuration.php index 9e16b4f..e8ebf9f 100644 --- a/src/Support/Configuration/Configuration.php +++ b/src/Support/Configuration/Configuration.php @@ -30,5 +30,6 @@ public function __construct( public readonly ?string $mutationId, public readonly bool $retry, public readonly bool $everything, + public readonly ?string $changed = null, ) {} } diff --git a/src/Support/Printers/DefaultPrinter.php b/src/Support/Printers/DefaultPrinter.php index 2e22cda..f5a5755 100644 --- a/src/Support/Printers/DefaultPrinter.php +++ b/src/Support/Printers/DefaultPrinter.php @@ -9,6 +9,7 @@ use Pest\Mutate\MutationTest; use Pest\Mutate\MutationTestCollection; use Pest\Mutate\Repositories\ConfigurationRepository; +use Pest\Mutate\Support\ChangedScope; use Pest\Mutate\Support\Configuration\Configuration; use Pest\Mutate\Support\MutationTestResult; use Pest\Support\Container; @@ -100,6 +101,29 @@ public function reportScoreNotReached(float $scoreReached, float $scoreRequired) ]); } + public function reportChangedScope(ChangedScope $scope): void + { + $since = 'since '.substr($scope->base, 0, 7).($scope->revision === 'HEAD' ? '' : ' ('.$scope->revision.')'); + + if ($scope->everythingBecause !== null) { + $this->output->writeln(' Mutating everything, as '.$scope->everythingBecause.' '.$since.''); + + return; + } + + if ($scope->files() === []) { + $this->output->writeln(' Nothing to mutate, as no change '.$since.' affects a mutant'); + + return; + } + + $this->output->writeln(' Only mutating what changed '.$since.':'); + + foreach ($scope->files() as $file) { + $this->output->writeln(' '.str_replace(getcwd().DIRECTORY_SEPARATOR, '', $file).''); + } + } + public function reportMutationGenerationStarted(MutationSuite $mutationSuite): void { $this->output->writeln(' Mutating application files...'); diff --git a/src/Tester/MutationTestRunner.php b/src/Tester/MutationTestRunner.php index b451574..38c79ec 100644 --- a/src/Tester/MutationTestRunner.php +++ b/src/Tester/MutationTestRunner.php @@ -13,11 +13,13 @@ use Pest\Mutate\Plugins\Mutate; use Pest\Mutate\Repositories\ConfigurationRepository; use Pest\Mutate\Repositories\TelemetryRepository; +use Pest\Mutate\Support\ChangedScope; use Pest\Mutate\Support\Configuration\Configuration; use Pest\Mutate\Support\FileFinder; use Pest\Mutate\Support\MutationGenerator; use Pest\Support\Container; use Pest\Support\Coverage; +use Pest\TestSuite; use Psr\SimpleCache\CacheInterface; use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\Data\ProcessedCodeCoverageData; @@ -134,10 +136,28 @@ public function run(): int $files = FileFinder::files($this->getConfiguration()->paths, $this->getConfiguration()->pathsToIgnore); + $scope = $this->getConfiguration()->changed === null ? null : ChangedScope::since( + revision: $this->getConfiguration()->changed, + root: TestSuite::getInstance()->rootPath, + sourceFiles: $files, + sourcePaths: $this->getConfiguration()->paths, + testPath: TestSuite::getInstance()->testPath, + testFiles: TestSuite::getInstance()->tests->getFilenames(), + coveredLines: $coveredLines, + ); + + if ($scope instanceof ChangedScope) { + Container::getInstance()->get(Printer::class)->reportChangedScope($scope); // @phpstan-ignore-line + } + /** @var MutationGenerator $generator */ $generator = Container::getInstance()->get(MutationGenerator::class); foreach ($files as $file) { + if ($scope instanceof ChangedScope && ! $scope->includes($file)) { + continue; + } + $linesToMutate = []; if ($this->getConfiguration()->coveredOnly) { @@ -237,7 +257,7 @@ private function isMinScoreIsReached(MutationSuite $mutationSuite): bool return true; } - if ($mutationSuite->repository->count() === 0 && $configuration->ignoreMinScoreOnZeroMutations) { + if ($mutationSuite->repository->count() === 0 && ($configuration->ignoreMinScoreOnZeroMutations || $configuration->changed !== null)) { return true; } diff --git a/tests/Features/Profiles/HandlesCliProfileConfigurationTest.php b/tests/Features/Profiles/HandlesCliProfileConfigurationTest.php index b1dfcc6..1b5249b 100644 --- a/tests/Features/Profiles/HandlesCliProfileConfigurationTest.php +++ b/tests/Features/Profiles/HandlesCliProfileConfigurationTest.php @@ -220,3 +220,17 @@ expect($this->configuration->toArray()) ->mutation_id->toBe('fa1234'); }); + +it('only mutates what changed since a revision if --changed argument is passed', function (): void { + $this->configuration->fromArguments(['--mutate='.ConfigurationRepository::FAKE]); + expect($this->configuration->toArray()) + ->changed->toBeNull(); + + $this->configuration->fromArguments(['--changed']); + expect($this->configuration->toArray()) + ->changed->toBe('HEAD'); + + $this->configuration->fromArguments(['--changed=origin/main']); + expect($this->configuration->toArray()) + ->changed->toBe('origin/main'); +}); diff --git a/tests/Unit/Support/ChangedScopeTest.php b/tests/Unit/Support/ChangedScopeTest.php new file mode 100644 index 0000000..954a6f3 --- /dev/null +++ b/tests/Unit/Support/ChangedScopeTest.php @@ -0,0 +1,158 @@ +namespace = 'App\\Changed'.bin2hex(random_bytes(4)); + $this->root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-mutate-changed-'.bin2hex(random_bytes(4)); + + mkdir($this->root); + $this->root = (string) realpath($this->root); + + $this->write = function (array $files): void { + foreach ($files as $path => $code) { + is_dir(dirname($this->root.'/'.$path)) || mkdir(dirname($this->root.'/'.$path), recursive: true); + file_put_contents($this->root.'/'.$path, str_replace('App\\Ns', $this->namespace, (string) $code)); + } + }; + + $this->git = function (string ...$arguments): string { + $process = new Process(['git', '-c', 'user.name=Pest', '-c', 'user.email=pest@example.com', '-c', 'commit.gpgsign=false', '-c', 'core.hooksPath=/dev/null', ...$arguments], $this->root); + $process->mustRun(); + + return $process->getOutput(); + }; + + $this->commit = function (array $files): void { + ($this->write)($files); + ($this->git)('add', '--all'); + ($this->git)('commit', '-q', '-m', 'commit'); + }; + + $this->scope = function (string $revision = 'HEAD', array $coveredLines = []): ChangedScope { + foreach (glob($this->root.'/src/*.php') ?: [] as $file) { + require_once $file; + } + + return ChangedScope::since( + revision: $revision, + root: $this->root, + sourceFiles: FileFinder::files([$this->root.'/src'], []), + sourcePaths: [$this->root.'/src'], + testPath: 'tests', + testFiles: glob($this->root.'/tests/*Test.php') ?: [], + coveredLines: $coveredLines, + ); + }; + + $this->files = function (ChangedScope $scope): array { + $files = array_map(fn (string $file): string => substr($file, strlen($this->root) + 1), $scope->files()); + sort($files); + + return $files; + }; + + ($this->git)('init', '-q', '-b', 'main'); + ($this->commit)([ + 'README.md' => '# Project', + 'src/Foo.php' => ' ' ' ' ' true);', + ]); +}); + +afterEach(function (): void { + (new Process(['rm', '-rf', $this->root]))->run(); +}); + +it('only scopes the source files that changed', function (): void { + ($this->write)(['src/Foo.php' => 'scope)(); + + expect($scope->everythingBecause)->toBeNull() + ->and(($this->files)($scope))->toBe(['src/Foo.php']); +}); + +it('scopes new source files that are not tracked yet', function (): void { + ($this->write)(['src/Qux.php' => 'files)(($this->scope)()))->toBe(['src/Qux.php']); +}); + +it('scopes nothing when only files without mutants changed', function (): void { + ($this->write)(['README.md' => '# Changed']); + + $scope = ($this->scope)(); + + expect($scope->everythingBecause)->toBeNull() + ->and(($this->files)($scope))->toBe([]); +}); + +it('scopes what a changed test file covers before and after the change', function (): void { + ($this->write)(['tests/FooTest.php' => ' true);']); + + expect(($this->files)(($this->scope)()))->toBe(['src/Bar.php', 'src/Foo.php']); +}); + +it('scopes what a deleted test file covered', function (): void { + unlink($this->root.'/tests/FooTest.php'); + + expect(($this->files)(($this->scope)()))->toBe(['src/Bar.php', 'src/Foo.php']); +}); + +it('scopes the namespaces a changed test file mutates', function (): void { + ($this->write)(['tests/SubTest.php' => ' true);']); + + expect(($this->files)(($this->scope)()))->toBe(['src/Sub/Baz.php']); +}); + +it('scopes the source files the tests of a changed test file run', function (): void { + ($this->write)(['tests/FooTest.php' => ' true);']); + + $testCase = 'P\\Tests\\'.str_replace('\\', '', $this->namespace).'FooTest'; + eval(sprintf('namespace %s; final class %s { public static $__filename = %s; }', substr($testCase, 0, (int) strrpos($testCase, '\\')), substr($testCase, (int) strrpos($testCase, '\\') + 1), var_export($this->root.'/tests/FooTest.php', true))); + + $scope = ($this->scope)('HEAD', [$this->root.'/src/Sub/Baz.php' => [1 => [$testCase.'::__pest_evaluable_it_works']]]); + + expect(($this->files)($scope))->toBe(['src/Bar.php', 'src/Foo.php', 'src/Sub/Baz.php']); +}); + +it('compares against where HEAD forked from the given revision', function (): void { + ($this->git)('switch', '-q', '-c', 'feature'); + ($this->commit)(['src/Foo.php' => 'git)('switch', '-q', 'main'); + ($this->commit)(['src/Bar.php' => 'git)('switch', '-q', 'feature'); + + expect(($this->files)(($this->scope)('main')))->toBe(['src/Foo.php']); +}); + +it('mutates everything when a non-test file in the tests path changed', function (): void { + ($this->write)(['tests/Pest.php' => 'extend(Tests\TestCase::class);']); + + expect(($this->scope)()->everythingBecause)->toBe('tests/Pest.php changed'); +}); + +it('mutates everything when the dependencies changed', function (): void { + ($this->write)(['composer.lock' => '{}']); + + expect(($this->scope)()->everythingBecause)->toBe('composer.lock changed'); +}); + +it('mutates everything when a changed test file covers something other than a name', function (): void { + ($this->write)(['tests/FooTest.php' => ' true);']); + + expect(($this->scope)()->everythingBecause)->toBe('tests/FooTest.php covers something other than class or namespace names'); +}); + +it('fails when the revision shares no history with HEAD', function (): void { + ($this->scope)('does-not-exist'); +})->throws(InvalidOption::class, 'Mutating changes requires git and a common ancestor of [does-not-exist] and HEAD.'); diff --git a/tests/Unit/Support/Printers/DefaultPrinterTest.php b/tests/Unit/Support/Printers/DefaultPrinterTest.php index fb55914..083db6c 100644 --- a/tests/Unit/Support/Printers/DefaultPrinterTest.php +++ b/tests/Unit/Support/Printers/DefaultPrinterTest.php @@ -7,6 +7,7 @@ use Pest\Mutate\MutationTest; use Pest\Mutate\MutationTestCollection; use Pest\Mutate\Mutators\Equality\EqualToIdentical; +use Pest\Mutate\Support\ChangedScope; use Pest\Mutate\Support\Printers\DefaultPrinter; use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Finder\SplFileInfo; @@ -217,3 +218,26 @@ ->toStartWith(PHP_EOL); }); }); + +describe('report changed scope', function (): void { + it('lists the files a change can affect', function (): void { + $this->printer->reportChangedScope(new ChangedScope('origin/main', str_repeat('a', 40), null, [getcwd().'/src/Foo.php' => true])); + + expect($this->out->fetch()) + ->toBe(' Only mutating what changed since aaaaaaa (origin/main):'.PHP_EOL.' src/Foo.php'.PHP_EOL); + }); + + it('says when no change affects a mutant', function (): void { + $this->printer->reportChangedScope(new ChangedScope('HEAD', str_repeat('a', 40), null)); + + expect($this->out->fetch()) + ->toBe(' Nothing to mutate, as no change since aaaaaaa affects a mutant'.PHP_EOL); + }); + + it('says why it mutates everything', function (): void { + $this->printer->reportChangedScope(new ChangedScope('origin/main', str_repeat('a', 40), 'tests/Pest.php changed')); + + expect($this->out->fetch()) + ->toBe(' Mutating everything, as tests/Pest.php changed since aaaaaaa (origin/main)'.PHP_EOL); + }); +}); From cb4b29df982707857c2a3cd57f5a97b4352c4b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Krzy=C5=BCanowski?= Date: Wed, 28 Oct 2026 18:32:34 +0100 Subject: [PATCH 2/3] feat: skip support files that only add functions No unchanged test can call a new function, and the tests that do are in the diff anyway. --- src/Support/ChangedScope.php | 44 +++++++++++++++++++++++++ tests/Unit/Support/ChangedScopeTest.php | 23 +++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/Support/ChangedScope.php b/src/Support/ChangedScope.php index 3e61f20..25543cb 100644 --- a/src/Support/ChangedScope.php +++ b/src/Support/ChangedScope.php @@ -15,9 +15,14 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; +use PhpParser\Node\Stmt\Function_; +use PhpParser\Node\Stmt\GroupUse; +use PhpParser\Node\Stmt\Nop; +use PhpParser\Node\Stmt\Use_; use PhpParser\NodeFinder; use PhpParser\NodeTraverser; use PhpParser\NodeVisitor\NameResolver; +use PhpParser\PrettyPrinter\Standard; use ReflectionClass; use Symfony\Component\Finder\SplFileInfo; use Symfony\Component\Process\Process; @@ -110,6 +115,10 @@ public static function since( $isDeletedTest = ! file_exists($absolute) && str_ends_with($path, '.php'); if (! isset($testFiles[$real]) && ! $isDeletedTest) { + if (self::onlyAddsFunctions(self::git($root, ['show', "{$base}:./{$path}"]), $absolute)) { + continue; + } + return new self($revision, $base, "{$path} changed"); } @@ -225,6 +234,41 @@ private static function within(string $path, array $roots): bool return array_any($roots, fn (string $root): bool => $path === $root || str_starts_with($path, $root.DIRECTORY_SEPARATOR)); } + /** + * Whether a support file such as tests/Pest.php only gained functions or imports, which no unchanged test can use. + */ + private static function onlyAddsFunctions(?string $before, string $path): bool + { + $after = str_ends_with($path, '.php') && is_file($path) ? file_get_contents($path) : false; + + if ($before === null || $after === false) { + return false; + } + + try { + [$before, $after] = array_map(fn (string $code): array => PhpParserFactory::make()->parse($code) ?? [], [$before, $after]); + } catch (Error) { + return false; + } + + $printer = new Standard; + $remaining = array_map(fn (Node $statement): string => $printer->prettyPrint([$statement]), $before); + + foreach ($after as $statement) { + if ($remaining !== [] && $printer->prettyPrint([$statement]) === $remaining[0]) { + array_shift($remaining); + + continue; + } + + if (! $statement instanceof Function_ && ! $statement instanceof Use_ && ! $statement instanceof GroupUse && ! $statement instanceof Nop) { + return false; + } + } + + return $remaining === []; + } + /** * The classes and namespaces a test file passes to covers() or mutates(), or null when an argument isn't a literal name. * diff --git a/tests/Unit/Support/ChangedScopeTest.php b/tests/Unit/Support/ChangedScopeTest.php index 954a6f3..330ef5e 100644 --- a/tests/Unit/Support/ChangedScopeTest.php +++ b/tests/Unit/Support/ChangedScopeTest.php @@ -135,6 +135,15 @@ expect(($this->files)(($this->scope)('main')))->toBe(['src/Foo.php']); }); +it('scopes nothing when a support file only gained functions', function (): void { + ($this->write)(['tests/Pest.php' => 'scope)(); + + expect($scope->everythingBecause)->toBeNull() + ->and(($this->files)($scope))->toBe([]); +}); + it('mutates everything when a non-test file in the tests path changed', function (): void { ($this->write)(['tests/Pest.php' => 'extend(Tests\TestCase::class);']); @@ -156,3 +165,17 @@ it('fails when the revision shares no history with HEAD', function (): void { ($this->scope)('does-not-exist'); })->throws(InvalidOption::class, 'Mutating changes requires git and a common ancestor of [does-not-exist] and HEAD.'); + +it('mutates everything when a support file changed what it already declared', function (): void { + ($this->commit)(['tests/Pest.php' => 'write)(['tests/Pest.php' => 'scope)()->everythingBecause)->toBe('tests/Pest.php changed'); +}); + +it('mutates everything when a non-php file in the tests path changed', function (): void { + ($this->commit)(['tests/Fixtures/answer.json' => '{}']); + ($this->write)(['tests/Fixtures/answer.json' => '{"changed": true}']); + + expect(($this->scope)()->everythingBecause)->toBe('tests/Fixtures/answer.json changed'); +}); From cafd5237e5ac166b3d0971649a0412f6ac49df20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Krzy=C5=BCanowski?= Date: Wed, 28 Oct 2026 18:32:57 +0100 Subject: [PATCH 3/3] docs: document the --changed option --- docs/mutation-testing.md | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/docs/mutation-testing.md b/docs/mutation-testing.md index d22b605..c7cc38e 100644 --- a/docs/mutation-testing.md +++ b/docs/mutation-testing.md @@ -127,8 +127,7 @@ The following options are available. - [`mutator()`](#mutator) - [`except()`](#except) - [`coveredOnly()`](#coveredOnly) -- [`uncommittedOnly()`](#uncommittedOnly) -- [`changedOnly()`](#changedOnly) +- [`changed()`](#changed) - [`stopOnEscaped()`](#stopOnEscaped) - [`stopOnNotCovered()`](#stopOnNotCovered) - [`bail()`](#bail) @@ -234,27 +233,19 @@ mutate() ``` - -### `uncommittedOnly()` -CLI: `--uncommitted-only` + +### `changed()` +CLI: `--changed` -Limit mutations to code that has uncommitted changes. +Only mutate the files a change since the given git revision can affect: the source files that changed, and the code the changed test files cover or run. Without a revision, only uncommitted changes count; with one, everything since HEAD forked from it does. When a file every test depends on changes, such as a fixture or `composer.lock`, everything is mutated; a support file like `tests/Pest.php` that only gained functions doesn't count. ```php mutate() - ->uncommittedOnly(); + ->changed(); // or ->changed('origin/main'); ``` - - -### `changedOnly()` -CLI: `--changed-only` - -Limit mutations to code that has changed relative to a common ancestor of the given branch (defaults to `main`). - -```php -mutate() - ->changedOnly(); // or ->changedOnly('add-xyz'); +```bash +vendor/bin/pest --mutate --changed=origin/main ```