From 4c886b16d1e8e37a3c0d4d437d1fbfc3c89998be Mon Sep 17 00:00:00 2001 From: Jhon Farias Date: Mon, 23 Nov 2026 10:57:13 +0100 Subject: [PATCH] fix(tia): fingerprint the generated configuration like the restarter does (#2065) * fix(tia): fingerprint the generated configuration like the restarter does * test(tia): pass one configuration option per call --------- Co-authored-by: Punyapal Shah --- src/Plugins/Configuration.php | 5 ++ src/Plugins/Tia.php | 4 +- src/Plugins/Tia/ConfigurationFile.php | 6 ++- tests/.snapshots/success.txt | 6 ++- tests/Features/Tia/ActiveConfiguration.php | 31 +++++++++++ tests/Unit/Plugins/Tia/ConfigurationFile.php | 56 +++++++++++++++++++- tests/Visual/Parallel.php | 2 +- 7 files changed, 104 insertions(+), 6 deletions(-) diff --git a/src/Plugins/Configuration.php b/src/Plugins/Configuration.php index 5f0489e4..dba46927 100644 --- a/src/Plugins/Configuration.php +++ b/src/Plugins/Configuration.php @@ -42,6 +42,11 @@ final class Configuration implements HandlesArguments, Terminable return $this->pushArgument((string) realpath($this->fromGeneratedConfigurationFile()), $arguments); } + public function generatedConfigurationPath(): ?string + { + return $this->generatedConfiguration['path'] ?? null; + } + private function fromGeneratedConfigurationFile(): string { $directory = getcwd(); diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index 91ea258d..68b43a2e 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -475,7 +475,9 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument exit(0); } - $this->configuration = ConfigurationFile::fromArguments($arguments); + $configurationPlugin = Container::getInstance()->get(Configuration::class); + assert($configurationPlugin instanceof Configuration); + $this->configuration = ConfigurationFile::fromArguments($arguments, $configurationPlugin->generatedConfigurationPath()); $isWorker = Parallel::isWorker(); $recordingGlobal = $isWorker && (string) Parallel::getGlobal(self::RECORDING_GLOBAL) === '1'; diff --git a/src/Plugins/Tia/ConfigurationFile.php b/src/Plugins/Tia/ConfigurationFile.php index b61f5c5e..310f711e 100644 --- a/src/Plugins/Tia/ConfigurationFile.php +++ b/src/Plugins/Tia/ConfigurationFile.php @@ -26,7 +26,7 @@ final readonly class ConfigurationFile /** * @param array $arguments */ - public static function fromArguments(array $arguments): self + public static function fromArguments(array $arguments, ?string $generatedPath = null): self { try { $configuration = new Builder(EventFacade::emitter())->fromParameters(self::configurationParameters($arguments)); @@ -41,6 +41,10 @@ final readonly class ConfigurationFile $file = new XmlConfigurationFileFinder()->find($configuration); $path = $file === false ? false : realpath($file); + if ($path !== false && $generatedPath !== null && $path === realpath($generatedPath)) { + return self::projectDefault(); + } + return new self($path === false ? null : $path, false); } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 71cdac0c..21ef7210 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1987,6 +1987,10 @@ ✓ it tells an identical copy under another name apart from the original ✓ it resolves a configuration directory the way PHPUnit does ✓ it gives a run with --no-configuration its own fingerprint + ✓ it fingerprints the configuration Pest generates the way the Xdebug restarter does with dataset "first name" + ✓ it fingerprints the configuration Pest generates the way the Xdebug restarter does with dataset "fallback name" + ✓ it fingerprints the configuration Pest generates the way the Xdebug restarter does with dataset "random name" + ✓ it fingerprints an explicit configuration apart from the one Pest generates PASS Tests\Unit\Plugins\Tia\ContentHash ✓ of() → it returns false when file does not exist @@ -2662,4 +2666,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1951 passed (4419 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1955 passed (4433 assertions) \ No newline at end of file diff --git a/tests/Features/Tia/ActiveConfiguration.php b/tests/Features/Tia/ActiveConfiguration.php index d0253fbd..ac30d975 100644 --- a/tests/Features/Tia/ActiveConfiguration.php +++ b/tests/Features/Tia/ActiveConfiguration.php @@ -100,3 +100,34 @@ test('an identical copy of the configuration file under another name does not re ->and($result->replayed())->toBe(0, $result->describe()) ->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed'); })->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows(); + +function removeConfiguration(Project $project): void +{ + $project->git()->run(['rm', '--quiet', 'phpunit.xml']); + $project->write('.gitignore', file_get_contents($project->path('.gitignore'))."/.pest*.xml\n"); + $project->git()->commit('use the configuration Pest generates'); + $project->write('vendor/pestphp/pest/.temp/.gitkeep', ''); +} + +test('a project without a configuration file replays the graph', function (array $arguments): void { + $project = Project::make('master'); + removeConfiguration($project); + $project->seed('master'); + + $result = $project->pest('--tia', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe()); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows(); + +test('a leftover generated configuration file does not discard the graph', function (array $arguments): void { + $project = Project::make('master'); + removeConfiguration($project); + $project->seed('master'); + $project->write('.pest.xml', (string) file_get_contents($project->path('vendor/pestphp/pest/resources/base-phpunit.xml'))); + + $replayed = $project->pest('--tia', ...$arguments); + + expect($replayed->exitCode)->toBe(0, $replayed->describe()) + ->and($replayed->replayed())->toBe(Project::TOTAL_TESTS, $replayed->describe()); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows(); diff --git a/tests/Unit/Plugins/Tia/ConfigurationFile.php b/tests/Unit/Plugins/Tia/ConfigurationFile.php index 6164ac51..e3ed2565 100644 --- a/tests/Unit/Plugins/Tia/ConfigurationFile.php +++ b/tests/Unit/Plugins/Tia/ConfigurationFile.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Pest\Plugins\Configuration; use Pest\Plugins\Tia\ConfigurationFile; beforeEach(function (): void { @@ -14,8 +15,12 @@ beforeEach(function (): void { }); afterEach(function (): void { - @unlink($this->projectRoot.'/phpunit.xml'); - @unlink($this->projectRoot.'/phpunit.copy.xml'); + foreach (['phpunit.xml', 'phpunit.copy.xml'] as $file) { + if (is_file($this->projectRoot.'/'.$file)) { + unlink($this->projectRoot.'/'.$file); + } + } + @rmdir($this->projectRoot); }); @@ -40,3 +45,50 @@ it('gives a run with --no-configuration its own fingerprint', function (): void ->toBe('none') ->not->toBe(ConfigurationFile::projectDefault()->fingerprint($this->projectRoot)); }); + +it('fingerprints the configuration Pest generates the way the Xdebug restarter does', function (array $leftovers): void { + unlink($this->projectRoot.'/phpunit.xml'); + + foreach ($leftovers as $leftover) { + file_put_contents($this->projectRoot.'/'.$leftover, ''); + } + + $workingDirectory = (string) getcwd(); + chdir($this->projectRoot); + $configuration = new Configuration; + + try { + $arguments = $configuration->handleArguments(['pest', '--tia']); + $generatedPath = $configuration->generatedConfigurationPath(); + + expect($generatedPath)->toBeString() + ->and(basename((string) $generatedPath))->not->toBeIn($leftovers) + ->and(ConfigurationFile::fromArguments($arguments, $generatedPath)->fingerprint($this->projectRoot)) + ->toBeNull() + ->toBe(ConfigurationFile::fromArguments(['pest', '--tia'])->fingerprint($this->projectRoot)); + } finally { + $configuration->terminate(); + chdir($workingDirectory); + + foreach ($leftovers as $leftover) { + unlink($this->projectRoot.'/'.$leftover); + } + } +})->with([ + 'first name' => [[]], + 'fallback name' => [['.pest.xml']], + 'random name' => [['.pest.xml', '.pest-fallback.xml']], +]); + +it('fingerprints an explicit configuration apart from the one Pest generates', function (): void { + $generatedPath = $this->projectRoot.'/.pest.xml'; + file_put_contents($generatedPath, ''); + + try { + expect(ConfigurationFile::fromArguments(['-c'.$this->projectRoot.'/phpunit.copy.xml'], $generatedPath)->fingerprint($this->projectRoot)) + ->toBe(ConfigurationFile::at($this->projectRoot.'/phpunit.copy.xml')->fingerprint($this->projectRoot)) + ->not->toBe(ConfigurationFile::fromArguments(['--configuration', $generatedPath], $generatedPath)->fingerprint($this->projectRoot)); + } finally { + unlink($generatedPath); + } +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 47152363..0c934df4 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -21,7 +21,7 @@ test('parallel', function () use ($run): void { $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); $profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile'); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1824 passed (3894 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1828 passed (3908 assertions)'; expect($output) ->toContain("Tests: {$expected}")