Keep focused tests local to each Pest run (#2049)

Co-authored-by: haranishi <223786623+haranishi@users.noreply.github.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
Ikki Nishihara
2026-11-20 06:08:12 +09:00
committed by GitHub
parent d65b04a389
commit a865244b92
3 changed files with 93 additions and 40 deletions
+38 -39
View File
@@ -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));
}
}
}
+5 -1
View File
@@ -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)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1853 passed (4263 assertions)
+50
View File
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
use Tests\Fixtures\Tia\Project;
afterEach(function (): void {
Project::destroyAll();
});
test('limits focus to the invocation that discovers it', function (array $arguments): void {
$project = Project::withoutGit();
$testFile = 'tests/Unit/FocusTest.php';
$project->write('vendor/pestphp/pest/.temp/.gitkeep', '');
$project->write($testFile, <<<'FIXTURE'
<?php
use Symfony\Component\Process\Process;
test('runs with a named focus group in CI', function (): void {
expect(true)->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');