mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
7cdebc6a95
* refactor: extract shared CI detection utility Consolidate duplicated CI environment detection logic from Snapshot and BaselineSync into a shared Pest\Support\Ci class. This reduces duplication and provides a single source of truth for CI detection, including comprehensive environment variable checks for all major CI providers. * fix(tia): persist state on detached HEAD Allow detached checkouts to write, delete, and rebuild TIA state so baseline publishers and local detached workflows produce a usable graph. Resolve the writable baseline from the Git branch, CI branch metadata, or a stable workspace key. Keep the default branch as the read fallback so local workspaces can use a published baseline without changing it.
188 lines
6.9 KiB
PHP
188 lines
6.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Tests\Fixtures\Tia\Project;
|
|
|
|
afterEach(function (): void {
|
|
Project::destroyAll();
|
|
});
|
|
|
|
test('narrows to the affected tests on a new branch', function (): void {
|
|
$project = Project::make('master');
|
|
$project->seed('master');
|
|
|
|
$project->git()->switchTo('feature-x', new: true);
|
|
|
|
$project->write('tests/Unit/GreeterTest.php', <<<'PHP'
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Fixture\App\Greeter;
|
|
|
|
test('greets a person', function (): void {
|
|
expect((new Greeter)->greet('Nuno'))->toBe('Hello, Nuno!');
|
|
expect((new Greeter)->greet('Nuno'))->toBeString();
|
|
});
|
|
|
|
test('greets the world', function (): void {
|
|
expect((new Greeter)->greet('world'))->toBe('Hello, world!');
|
|
});
|
|
PHP);
|
|
|
|
$result = $project->pest('--tia');
|
|
$delta = $project->delta();
|
|
|
|
expect($result->affected())->toBe(2, $result->describe())
|
|
->and($result->replayed())->toBe(4, $result->describe())
|
|
->and($result->exitCode)->toBe(0, $result->describe())
|
|
->and($project->branchKeys())->toBe(['master', 'feature-x'])
|
|
->and($delta->baselineUntouched('master'))->toBeTrue($delta->summary())
|
|
->and($delta->edgesMoved())->toBeFalse($delta->summary());
|
|
})->skipOnWindows();
|
|
|
|
test('filtered mode reads the fallback too', function (): void {
|
|
$project = Project::make('master');
|
|
|
|
$project->seed('master', failing: ['adds two numbers']);
|
|
|
|
$project->git()->switchTo('feature-x', new: true);
|
|
|
|
$result = $project->pest('--tia', '--filtered');
|
|
|
|
expect($result->output)->toContain('from 1 previously unsuccessful test')
|
|
->and($result->output)->not->toContain('No affected tests found')
|
|
->and($result->tally())->toContain('2 passed');
|
|
})->skipOnWindows();
|
|
|
|
test('filtered mode finds nothing to do on a clean green feature branch', function (): void {
|
|
$project = Project::make('master');
|
|
$project->seed('master');
|
|
|
|
$project->git()->switchTo('feature-x', new: true);
|
|
|
|
$result = $project->pest('--tia', '--filtered');
|
|
$delta = $project->delta();
|
|
|
|
expect($result->output)->toContain('No affected tests found')
|
|
->and($result->exitCode)->toBe(0, $result->describe())
|
|
->and($delta->isHardSuppressed())->toBeTrue($delta->summary());
|
|
})->skipOnWindows();
|
|
|
|
test('filtered mode falls back to a full replay when a cached failure cannot be located', function (): void {
|
|
$project = Project::make('master');
|
|
|
|
$project->seed('master', failing: ['adds two numbers']);
|
|
|
|
$project->mutateGraph(function (array $graph): array {
|
|
$testId = Project::testId('tests/Unit/CalculatorTest.php', 'adds two numbers');
|
|
|
|
$graph['baselines']['master']['results'][$testId]['file'] = '/build/agent/tests/Unit/DeletedTest.php';
|
|
|
|
return $graph;
|
|
});
|
|
|
|
$project->git()->switchTo('feature-x', new: true);
|
|
|
|
$result = $project->pest('--tia', '--filtered');
|
|
|
|
expect($result->output)->toContain('Some cached tests due a re-run could not be located on disk.')
|
|
->and($result->output)->toContain('Running the full suite with replay instead of a filtered run.')
|
|
->and($result->output)->not->toContain('No affected tests found');
|
|
})->skipOnWindows();
|
|
|
|
test('filtered mode finds nothing to do on the default branch itself', function (): void {
|
|
$project = Project::make('master');
|
|
$project->seed('master');
|
|
|
|
$result = $project->pest('--tia', '--filtered');
|
|
$delta = $project->delta();
|
|
|
|
expect($result->output)->toContain('No affected tests found')
|
|
->and($result->exitCode)->toBe(0, $result->describe())
|
|
->and($delta->isHardSuppressed())->toBeTrue($delta->summary());
|
|
})->skipOnWindows();
|
|
|
|
test('a detached HEAD replays through a workspace baseline', function (): void {
|
|
$project = Project::make('master');
|
|
$project->seed('master');
|
|
|
|
$project->git()->detach();
|
|
|
|
$result = $project->pest('--tia');
|
|
$hasWorkspaceBranch = array_any($project->branchKeys(), fn (string $branch): bool => str_starts_with($branch, '@workspace:'));
|
|
|
|
expect($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
|
->and($result->uncached())->toBe(0, $result->describe())
|
|
->and($project->branchKeys())->toHaveCount(2)
|
|
->and($project->branchKeys())->toContain('master')
|
|
->and($hasWorkspaceBranch)->toBeTrue();
|
|
})->skipOnWindows();
|
|
|
|
test('a detached HEAD does not write into the default branch baseline', function (array $arguments): void {
|
|
$project = Project::make('master');
|
|
$project->seed('master');
|
|
|
|
$project->git()->detach();
|
|
$project->pest(...$arguments);
|
|
|
|
$delta = $project->delta();
|
|
$hasWorkspaceBranch = array_any($project->branchKeys(), fn (string $branch): bool => str_starts_with($branch, '@workspace:'));
|
|
|
|
expect($delta->baselineUntouched('master'))->toBeTrue($delta->summary())
|
|
->and($project->branchKeys())->toHaveCount(2)
|
|
->and($project->branchKeys())->toContain('master')
|
|
->and($hasWorkspaceBranch)->toBeTrue()
|
|
->and($delta->writtenCount())->toBe(0, $delta->summary());
|
|
})->with([
|
|
'sequential' => [['--filter=adds two numbers']],
|
|
'parallel' => [['--parallel', '--processes=2', '--filter=adds two numbers']],
|
|
])->skipOnWindows();
|
|
|
|
test('the branch that ran gets its own key and the default branch keeps its baseline', function (): void {
|
|
$project = Project::make('master');
|
|
$project->seed('master');
|
|
|
|
$project->git()->switchTo('feature-x', new: true);
|
|
$project->pest('--tia');
|
|
|
|
$delta = $project->delta();
|
|
|
|
expect($project->branchKeys())->toBe(['master', 'feature-x'])
|
|
->and($delta->baselineUntouched('master'))->toBeTrue($delta->summary())
|
|
->and($delta->writtenCount())->toBe(0, $delta->summary());
|
|
})->skipOnWindows();
|
|
|
|
test('a detached CI branch writes into its branch baseline', function (array $environment): void {
|
|
$project = Project::make('master');
|
|
$project->seed('master');
|
|
|
|
$project->git()->detach();
|
|
$project->pestWithEnvironment($project->path(), $environment, '--filter=adds two numbers');
|
|
|
|
expect($project->branchKeys())->toBe(['master', 'feature-x'])
|
|
->and(array_any($project->branchKeys(), fn (string $branch): bool => str_starts_with($branch, '@workspace:')))->toBeFalse();
|
|
})->with([
|
|
'GitLab' => [[
|
|
'GITLAB_CI' => 'true',
|
|
'CI_COMMIT_BRANCH' => 'feature-x',
|
|
]],
|
|
'GitHub' => [[
|
|
'GITHUB_ACTIONS' => 'true',
|
|
'GITHUB_REF_NAME' => 'feature-x',
|
|
]],
|
|
])->skipOnWindows();
|
|
|
|
test('the fallback reaches parallel workers', function (): void {
|
|
$project = Project::make('master');
|
|
$project->seed('master');
|
|
|
|
$project->git()->switchTo('feature-x', new: true);
|
|
|
|
$result = $project->pest('--tia', '--parallel', '--processes=2');
|
|
|
|
expect($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
|
->and($result->uncached())->toBe(0, $result->describe());
|
|
})->skipOnWindows();
|