From d65b04a389e065da2a5fe050db82b18cab154312 Mon Sep 17 00:00:00 2001 From: Ikki Nishihara Date: Fri, 20 Nov 2026 06:05:43 +0900 Subject: [PATCH] Preserve existing PHPUnit configuration files (#2044) Co-authored-by: haranishi <223786623+haranishi@users.noreply.github.com> Co-authored-by: Punyapal Shah --- src/Plugins/Configuration.php | 64 +++++++++++++++++++++++++------- tests/.snapshots/success.txt | 10 ++++- tests/Features/Configuration.php | 62 +++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 tests/Features/Configuration.php diff --git a/src/Plugins/Configuration.php b/src/Plugins/Configuration.php index 4ad4d41a..5f0489e4 100644 --- a/src/Plugins/Configuration.php +++ b/src/Plugins/Configuration.php @@ -11,6 +11,7 @@ use Pest\Plugins\Concerns\HandleArguments; use PHPUnit\Event\Facade as EventFacade; use PHPUnit\TextUI\CliArguments\Builder as CliConfigurationBuilder; use PHPUnit\TextUI\CliArguments\XmlConfigurationFileFinder; +use RuntimeException; /** * @internal @@ -27,6 +28,9 @@ final class Configuration implements HandlesArguments, Terminable .DIRECTORY_SEPARATOR .'resources/base-phpunit.xml'; + /** @var array{path: string, dev: int, ino: int}|null */ + private ?array $generatedConfiguration = null; + public function handleArguments(array $arguments): array { if ($this->hasArgument('--configuration', $arguments) || $this->hasArgument('-c', $arguments) || $this->hasCustomConfigurationFile()) { @@ -40,19 +44,50 @@ final class Configuration implements HandlesArguments, Terminable private function fromGeneratedConfigurationFile(): string { - $path = $this->getTempPhpunitXmlPath(); - if (file_exists($path)) { - unlink($path); + $directory = getcwd(); + + if ($directory === false) { + throw new RuntimeException('Unable to determine the working directory.'); } $doc = new DOMDocument; $doc->load(self::BASE_PHPUNIT_FILE); $contents = $doc->saveXML(); + assert(is_string($contents)); - assert(is_int(file_put_contents($path, $contents))); + foreach (['.pest.xml', '.pest-fallback.xml', null] as $filename) { + $filename ??= '.pest-'.bin2hex(random_bytes(8)).'.xml'; + $path = $directory.DIRECTORY_SEPARATOR.$filename; + $file = @fopen($path, 'xb'); - return $path; + if ($file === false) { + continue; + } + + try { + $stat = @fstat($file); + + if ($stat === false) { + throw new RuntimeException('Unable to identify the temporary PHPUnit configuration file.'); + } + + $this->generatedConfiguration = ['path' => $path, 'dev' => $stat['dev'], 'ino' => $stat['ino']]; + $written = @fwrite($file, $contents); + } finally { + fclose($file); + } + + if ($written !== strlen($contents)) { + $this->terminate(); + + throw new RuntimeException('Unable to write the temporary PHPUnit configuration file.'); + } + + return $path; + } + + throw new RuntimeException('Unable to create a temporary PHPUnit configuration file.'); } private function hasCustomConfigurationFile(): bool @@ -63,17 +98,20 @@ final class Configuration implements HandlesArguments, Terminable return is_string($configurationFile); } - private function getTempPhpunitXmlPath(): string - { - return getcwd().'/.pest.xml'; - } - public function terminate(): void { - $path = $this->getTempPhpunitXmlPath(); + $configuration = $this->generatedConfiguration; + $this->generatedConfiguration = null; - if (file_exists($path)) { - unlink($path); + if ($configuration === null) { + return; + } + + clearstatcache(); + $stat = @lstat($configuration['path']); + + if ($stat !== false && $stat['dev'] === $configuration['dev'] && $stat['ino'] === $configuration['ino']) { + unlink($configuration['path']); } } } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index a34b6bb5..2b082012 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -73,6 +73,14 @@ ↓ is marked as todo 3 ↓ shouldBeMarkedAsTodo + PASS Tests\Features\Configuration + ✓ preserves an existing .pest.xml file with dataset "explicit configuration" + ✓ preserves an existing .pest.xml file with dataset "discovered configuration" + ✓ preserves an existing .pest.xml file with dataset "generated configuration" + ✓ removes the generated configuration when it finishes with dataset "sequential" + ✓ removes the generated configuration when it finishes with dataset "parallel" + ✓ preserves a replacement for the generated configuration + WARN Tests\Features\Coverage ✓ it has plugin - it adds coverage if --coverage exist → Coverage is not available @@ -2545,4 +2553,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, 38 skipped, 1845 passed (4231 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1851 passed (4255 assertions) \ No newline at end of file diff --git a/tests/Features/Configuration.php b/tests/Features/Configuration.php new file mode 100644 index 00000000..1a8fdab0 --- /dev/null +++ b/tests/Features/Configuration.php @@ -0,0 +1,62 @@ +path('.pest.xml'); + $contents = $arguments === [] ? 'An existing project file.' : (string) file_get_contents($project->path('phpunit.xml')); + $project->write('.pest.xml', $contents); + + if (! $hasDefaultConfiguration) { + unlink($project->path('phpunit.xml')); + } + + $result = $project->pest('tests/Unit/GreeterTest.php', '--cache-directory', $project->path('.phpunit.cache'), ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->tally())->toContain('2 passed') + ->and(glob($project->path('.pest*.xml')))->toHaveCount(1) + ->and($configuration)->toBeFile() + ->and(file_get_contents($configuration))->toBe($contents); +})->with([ + 'explicit configuration' => [['--configuration=.pest.xml'], false], + 'discovered configuration' => [[], true], + 'generated configuration' => [[], false], +])->group('integration'); + +test('removes the generated configuration when it finishes', function (array $arguments): void { + $project = Project::withoutGit(); + unlink($project->path('phpunit.xml')); + + $result = $project->pest('tests/Unit/GreeterTest.php', '--cache-directory', $project->path('.phpunit.cache'), ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->tally())->toContain('2 passed') + ->and(glob($project->path('.pest*.xml')))->toBe([]); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->group('integration'); + +test('preserves a replacement for the generated configuration', function (): void { + $project = Project::withoutGit(); + unlink($project->path('phpunit.xml')); + $project->write('replacement.xml', 'A replacement project file.'); + $project->write('tests/Unit/ReplaceConfigurationTest.php', <<<'PHP' + toBeTrue(); + }); + PHP); + + $result = $project->pest('tests/Unit/ReplaceConfigurationTest.php', '--cache-directory', $project->path('.phpunit.cache')); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($project->path('.pest.xml'))->toBeFile() + ->and(file_get_contents($project->path('.pest.xml')))->toBe('A replacement project file.'); +})->group('integration');