From a865244b92fc3f5ccc55927b68f48df30b437a1e Mon Sep 17 00:00:00 2001 From: Ikki Nishihara Date: Fri, 20 Nov 2026 06:08:12 +0900 Subject: [PATCH] Keep focused tests local to each Pest run (#2049) Co-authored-by: haranishi <223786623+haranishi@users.noreply.github.com> Co-authored-by: Punyapal Shah --- src/Plugins/Only.php | 77 ++++++++++++++++++------------------ tests/.snapshots/success.txt | 6 ++- tests/Features/Only.php | 50 +++++++++++++++++++++++ 3 files changed, 93 insertions(+), 40 deletions(-) create mode 100644 tests/Features/Only.php diff --git a/src/Plugins/Only.php b/src/Plugins/Only.php index bd228e33..3c4d9a59 100644 --- a/src/Plugins/Only.php +++ b/src/Plugins/Only.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Pest\Plugins; +use Pest\Contracts\Plugins\HandlesArguments; use Pest\Contracts\Plugins\Terminable; use Pest\Factories\Attribute; use Pest\Factories\TestCaseMethodFactory; @@ -13,17 +14,24 @@ use PHPUnit\Framework\Attributes\Group; /** * @internal */ -final class Only implements Terminable +final class Only implements HandlesArguments, Terminable { - private const string TEMPORARY_FOLDER = __DIR__ - .DIRECTORY_SEPARATOR - .'..' - .DIRECTORY_SEPARATOR - .'..' - .DIRECTORY_SEPARATOR - .'.temp'; + private const string DEFAULT_GROUP = '__pest_only'; - public static function enable(TestCall|TestCaseMethodFactory $testCall, string $group = '__pest_only'): void + private const string WORKER_GROUP = 'ONLY_GROUP'; + + private static ?string $group = null; + + public function handleArguments(array $arguments): array + { + if (! Parallel::isWorker()) { + self::shareGroupWithWorkers(); + } + + return $arguments; + } + + public static function enable(TestCall|TestCaseMethodFactory $testCall, string $group = self::DEFAULT_GROUP): void { if ($testCall instanceof TestCall) { $testCall->group($group); @@ -38,52 +46,43 @@ final class Only implements Terminable return; } - $lockFile = self::TEMPORARY_FOLDER.DIRECTORY_SEPARATOR.'only.lock'; - - if (file_exists($lockFile) && $group === '__pest_only') { - file_put_contents($lockFile, $group); - - return; - } - - if (! file_exists($lockFile)) { - touch($lockFile); - - file_put_contents($lockFile, $group); + if (self::$group === null || $group === self::DEFAULT_GROUP) { + self::$group = $group; + self::shareGroupWithWorkers(); } } public static function isEnabled(): bool { - $lockFile = self::TEMPORARY_FOLDER.DIRECTORY_SEPARATOR.'only.lock'; - - return file_exists($lockFile); + return self::selectedGroup() !== null; } public static function group(): string { - $lockFile = self::TEMPORARY_FOLDER.DIRECTORY_SEPARATOR.'only.lock'; - - if (! file_exists($lockFile)) { - return '__pest_only'; - } - - return file_get_contents($lockFile) ?: '__pest_only'; // @phpstan-ignore-line + return self::selectedGroup() ?: self::DEFAULT_GROUP; } - /** - * {@inheritDoc} - */ public function terminate(): void { - if (Parallel::isWorker()) { - return; + self::$group = null; + self::shareGroupWithWorkers(); + } + + private static function selectedGroup(): ?string + { + if (! Parallel::isWorker()) { + return self::$group; } - $lockFile = self::TEMPORARY_FOLDER.DIRECTORY_SEPARATOR.'only.lock'; + $group = Parallel::getGlobal(self::WORKER_GROUP); - if (file_exists($lockFile)) { - unlink($lockFile); + return is_string($group) ? base64_decode($group) : null; + } + + private static function shareGroupWithWorkers(): void + { + if (Parallel::isEnabled()) { + Parallel::setGlobal(self::WORKER_GROUP, self::$group === null ? false : base64_encode(self::$group)); } } } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 2b082012..3c8a1557 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1394,6 +1394,10 @@ ! notice → This is a notice description // tests/Features/Notices.php:6 ! a "describe" group of tests → notice → This is a notice description // tests/Features/Notices.php:13 + PASS Tests\Features\Only + ✓ limits focus to the invocation that discovers it with dataset "sequential" + ✓ limits focus to the invocation that discovers it with dataset "parallel" + PASS Tests\Features\Pr ✓ it may be associated with an pr #1, #2 ✓ nested → it may be associated with an pr #1, #4, #5, #6, #3 @@ -2553,4 +2557,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, 1851 passed (4255 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1853 passed (4263 assertions) \ No newline at end of file diff --git a/tests/Features/Only.php b/tests/Features/Only.php new file mode 100644 index 00000000..9e63967f --- /dev/null +++ b/tests/Features/Only.php @@ -0,0 +1,50 @@ +write('vendor/pestphp/pest/.temp/.gitkeep', ''); + $project->write($testFile, <<<'FIXTURE' + toBeTrue(); + })->only('named-focus'); + + test('runs an independent Pest invocation', function (): void { + foreach ([[], ['--parallel', '--processes=2']] as $arguments) { + $process = new Process( + [PHP_BINARY, 'vendor/pestphp/pest/bin/pest', 'tests/Unit/GreeterTest.php', '--cache-directory', '.home/nested', ...$arguments], + env: ['PARATEST' => '0'], + ); + $process->mustRun(); + + expect($process->getOutput())->toContain('2 passed'); + } + })->only(); + + test('runs without focus in CI', function (): void { + expect(true)->toBeTrue(); + }); + FIXTURE); + + $focused = $project->pest($testFile, '--cache-directory', $project->path('.home/focused'), ...$arguments); + + expect($focused->exitCode)->toBe(0, $focused->describe()) + ->and($focused->tally())->toContain('1 passed (2 assertions)'); + + $ci = $project->pest($testFile, '--ci', '--cache-directory', $project->path('.home/ci'), ...$arguments); + + expect($ci->exitCode)->toBe(0, $ci->describe()) + ->and($ci->tally())->toContain('3 passed'); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->group('integration');