mirror of
https://github.com/pestphp/pest.git
synced 2026-12-05 07:15:33 +01:00
feat(tia): replay unaffected tests in a partial run (#2092)
* feat(tia): replay unaffected tests in a partial run * fix(tia): run a partial selection directly where a complete run is refused * fix(tia): leave a graph from another environment alone in a partial run
This commit is contained in:
+118
-25
@@ -223,6 +223,10 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
private bool $resultsOnlyWrites = false;
|
||||
|
||||
private bool $scoped = false;
|
||||
|
||||
private bool $announcesPartialRun = false;
|
||||
|
||||
private bool $flushesWorkerResults = false;
|
||||
|
||||
private bool $unreadableGraphReported = false;
|
||||
@@ -554,19 +558,29 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
$hasExplicitPath = $this->hasExplicitPathArgument($arguments);
|
||||
$partial = ! $isWorker && ($hasExplicitPath || $this->hasPartialSelection($arguments));
|
||||
$disabled = $disabled || $partial || $this->hasListingOption($arguments);
|
||||
|
||||
$freshRequested = $this->hasArgument(self::FRESH_OPTION, $arguments);
|
||||
$refetchRequested = $this->hasArgument(self::REFETCH_OPTION, $arguments);
|
||||
$mutating = self::isMutationRun($this->originalArguments);
|
||||
$this->scoped = $partial
|
||||
&& ! $freshRequested
|
||||
&& ! $refetchRequested
|
||||
&& ! $mutating
|
||||
&& ! $this->coverageReportActive();
|
||||
$disabled = $disabled || ($partial && ! $this->scoped) || $this->hasListingOption($arguments);
|
||||
|
||||
if ($partial) {
|
||||
$this->resultsOnlyWrites = true;
|
||||
}
|
||||
|
||||
if (getenv(self::ENV_MUTATION_TESTING) !== false) {
|
||||
$this->writesSuppressed = true;
|
||||
}
|
||||
$enabled = ! $disabled && ($cliEnabled || $alwaysEnabled);
|
||||
$this->filteredMode = ($this->hasArgument(self::FILTERED_OPTION, $arguments) || self::envFlagEnabled(self::ENV_FILTERED) || $watchPatterns->isFiltered())
|
||||
&& ! $hasExplicitPath
|
||||
$filteredRequested = $this->hasArgument(self::FILTERED_OPTION, $arguments) || self::envFlagEnabled(self::ENV_FILTERED) || $watchPatterns->isFiltered();
|
||||
$this->filteredMode = $filteredRequested
|
||||
&& ! $partial
|
||||
&& ! $this->coverageReportActive();
|
||||
$freshRequested = $this->hasArgument(self::FRESH_OPTION, $arguments);
|
||||
$this->forceRefetch = $this->hasArgument(self::REFETCH_OPTION, $arguments);
|
||||
$this->forceRefetch = $refetchRequested;
|
||||
|
||||
$arguments = $this->popArgument(self::OPTION, $arguments);
|
||||
$arguments = $this->popArgument(self::NO_OPTION, $arguments);
|
||||
@@ -576,22 +590,14 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$arguments = $this->popArgument(self::LOCALLY_OPTION, $arguments);
|
||||
$arguments = $this->popArgument(self::BASELINED_OPTION, $arguments);
|
||||
|
||||
$this->announcesPartialRun = $partial
|
||||
&& ($cliEnabled || $freshRequested || $refetchRequested || ($filteredRequested && ! $hasExplicitPath && ! $this->coverageReportActive()));
|
||||
|
||||
if ($disabled) {
|
||||
$this->requestWorkerResults();
|
||||
|
||||
if ($partial) {
|
||||
$this->resultsOnlyWrites = true;
|
||||
|
||||
if ($cliEnabled || $freshRequested || $this->forceRefetch || $this->filteredMode) {
|
||||
$this->output->writeln('');
|
||||
$this->renderChild('TIA does not apply to partial runs — running the selected tests directly.');
|
||||
}
|
||||
}
|
||||
|
||||
$this->forceRefetch = false;
|
||||
$this->filteredMode = false;
|
||||
|
||||
return $arguments;
|
||||
return $this->runSelectionDirectly($arguments);
|
||||
}
|
||||
|
||||
if (! $isWorker && $mutating && ($enabled || $this->forceRefetch)) {
|
||||
@@ -934,6 +940,10 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
try {
|
||||
$this->resolveBranch($projectRoot);
|
||||
} catch (MissingDependency|TiaGitFailed $missingGit) {
|
||||
if ($this->scoped) {
|
||||
return $this->runSelectionDirectly($arguments);
|
||||
}
|
||||
|
||||
$repository = new ChangedFiles($projectRoot);
|
||||
|
||||
if ($repository->isRepository() && ! $repository->hasCommits()) {
|
||||
@@ -944,6 +954,10 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
}
|
||||
|
||||
if (! $this->fallbackBranchResolved) {
|
||||
if ($this->scoped) {
|
||||
return $this->runSelectionDirectly($arguments);
|
||||
}
|
||||
|
||||
Panic::with(new ChangedFiles($projectRoot)->hasRemote()
|
||||
? new TiaRequiresDefaultBranch
|
||||
: new TiaRequiresRemote);
|
||||
@@ -960,6 +974,17 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
$graph = ($forceRebuild || $this->forceRefetch) ? null : $this->loadGraph($projectRoot);
|
||||
|
||||
if ($graph instanceof Graph && $this->scoped) {
|
||||
$drift = [
|
||||
...Fingerprint::structuralDrift($graph->fingerprint(), $fingerprint),
|
||||
...Fingerprint::environmentalDrift($graph->fingerprint(), $fingerprint),
|
||||
];
|
||||
|
||||
if ($drift !== []) {
|
||||
return $this->runSelectionLeavingTheGraph($drift, $arguments);
|
||||
}
|
||||
}
|
||||
|
||||
if ($graph instanceof Graph) {
|
||||
$graph = $this->reconcileFingerprint($graph, $fingerprint);
|
||||
}
|
||||
@@ -1006,6 +1031,13 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->state->write(self::KEY_COVERAGE_MARKER, '');
|
||||
}
|
||||
|
||||
if (! $graph instanceof Graph && $this->scoped) {
|
||||
$this->requestWorkerResults();
|
||||
$this->emitScopedRecordSkipped();
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
if (! $graph instanceof Graph && $this->piggybackCoverage) {
|
||||
$this->emitCoverageScopedRecordSkipped();
|
||||
|
||||
@@ -1047,7 +1079,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->output->writeln(sprintf(' <fg=gray>%s</>', $path));
|
||||
}
|
||||
|
||||
$this->renderChild('Running the full suite, and recording nothing — git reports no change there, so no result could stand behind a replay.');
|
||||
$this->renderChild($this->runningScope().', and recording nothing — git reports no change there, so no result could stand behind a replay.');
|
||||
$this->renderChild('Move what the project loads into the repository to let TIA select tests again.');
|
||||
|
||||
return true;
|
||||
@@ -1065,7 +1097,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
if ($dirty === null) {
|
||||
$this->renderBadge('WARN', 'Git could not read the working tree of the code this project loads from outside its root.');
|
||||
$this->renderChild('Running the full suite, and recording nothing.');
|
||||
$this->renderChild($this->runningScope().', and recording nothing.');
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1080,7 +1112,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->output->writeln(sprintf(' <fg=gray>%s</>', $file));
|
||||
}
|
||||
|
||||
$this->renderChild('Running the full suite, and recording nothing — git stops reporting such an edit once it is undone.');
|
||||
$this->renderChild($this->runningScope().', and recording nothing — git stops reporting such an edit once it is undone.');
|
||||
$this->renderChild('Commit what you changed there to let a run leave a baseline behind.');
|
||||
|
||||
return true;
|
||||
@@ -1346,6 +1378,13 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
$this->requestWorkerResults();
|
||||
|
||||
if ($this->scoped) {
|
||||
$this->renderChild('Running the selected tests directly — the dependency graph only reaches files under the project root.');
|
||||
$this->renderChild('Run a plain --tia run to record a fresh dependency graph.');
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
$this->renderChild('Running the full suite — the dependency graph only reaches files under the project root.');
|
||||
|
||||
if ($this->piggybackCoverage) {
|
||||
@@ -1375,7 +1414,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->requestWorkerResults();
|
||||
|
||||
$this->renderBadge('WARN', 'Detected PHP source changes but no coverage driver is available.');
|
||||
$this->renderChild('Running the full suite to avoid using a stale dependency graph.');
|
||||
$this->renderChild($this->runningScope().' to avoid using a stale dependency graph.');
|
||||
$this->renderChild('Install / enable pcov or xdebug (mode: coverage) so edges can be safely refreshed after PHP refactors.');
|
||||
|
||||
return $arguments;
|
||||
@@ -1403,7 +1442,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->reportAffectedSummary($changed, $affectedFromChanges, $rerunFromCache, $affected, $outsideProject);
|
||||
|
||||
$affectedSet = array_fill_keys($affected, true);
|
||||
$canRefreshReplayEdges = $coverageAvailable;
|
||||
$canRefreshReplayEdges = $coverageAvailable && ! $this->scoped;
|
||||
|
||||
$this->replayRan = true;
|
||||
$this->replayGraph = $graph;
|
||||
@@ -1570,7 +1609,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->writesSuppressed = true;
|
||||
|
||||
$this->renderPhpChangesOutsideProject($uncommittedPhpChanges);
|
||||
$this->renderChild('Running the full suite, and recording nothing — the project may load that code, and git stops reporting an uncommitted edit once it is undone.');
|
||||
$this->renderChild($this->runningScope().', and recording nothing — the project may load that code, and git stops reporting an uncommitted edit once it is undone.');
|
||||
$this->renderChild('Commit what you changed there to let a run leave a baseline behind.');
|
||||
}
|
||||
|
||||
@@ -1594,6 +1633,13 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
$this->requestWorkerResults();
|
||||
|
||||
if ($this->scoped) {
|
||||
$this->renderChild('Running the selected tests directly — the project may load that code, and a partial run cannot refresh the dependency graph.');
|
||||
$this->renderChild('Run a plain --tia run to record a fresh dependency graph.');
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
if ($this->piggybackCoverage) {
|
||||
$this->renderChild('Running the full suite — the project may load that code, and a coverage run cannot refresh the dependency graph.');
|
||||
$this->renderChild('Run --tia without --coverage to record a fresh dependency graph.');
|
||||
@@ -1733,7 +1779,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
private function canRebuildGraph(): bool
|
||||
{
|
||||
return ! $this->piggybackCoverage && $this->recorder->driverAvailable();
|
||||
return ! $this->scoped && ! $this->piggybackCoverage && $this->recorder->driverAvailable();
|
||||
}
|
||||
|
||||
private function renderFreshGraph(): void
|
||||
@@ -1774,6 +1820,53 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->renderChild('Record the baseline with a plain --tia run first; coverage runs then reuse it.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $arguments
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function runSelectionDirectly(array $arguments): array
|
||||
{
|
||||
$this->requestWorkerResults();
|
||||
|
||||
if ($this->announcesPartialRun) {
|
||||
$this->output->writeln('');
|
||||
$this->renderChild('TIA does not apply to partial runs — running the selected tests directly.');
|
||||
}
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $drift
|
||||
* @param array<int, string> $arguments
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function runSelectionLeavingTheGraph(array $drift, array $arguments): array
|
||||
{
|
||||
$this->writesSuppressed = true;
|
||||
|
||||
$this->renderBadge('WARN', sprintf(
|
||||
'The graph was recorded in a different environment (%s).',
|
||||
implode(', ', $drift),
|
||||
));
|
||||
$this->renderChild('Running the selected tests directly, and recording nothing — a plain --tia run records the graph again.');
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
private function runningScope(): string
|
||||
{
|
||||
return $this->scoped ? 'Running the selected tests directly' : 'Running the full suite';
|
||||
}
|
||||
|
||||
private function emitScopedRecordSkipped(): void
|
||||
{
|
||||
$this->output->writeln('');
|
||||
|
||||
$this->renderChild('Running the selected tests directly, as a partial run cannot record the dependency graph of the whole suite.');
|
||||
$this->renderChild('Record the baseline with a plain --tia run first; partial runs then replay it.');
|
||||
}
|
||||
|
||||
private function emitMutationScopedRecordSkipped(): void
|
||||
{
|
||||
$this->output->writeln('');
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
afterEach(function (): void {
|
||||
Project::destroyAll();
|
||||
});
|
||||
|
||||
test('a filtered configuration alone does not announce tia on a partial run without --tia', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->write('tests/Pest.php', <<<'PHP'
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__.'/../app/Calculator.php';
|
||||
require_once __DIR__.'/../app/Greeter.php';
|
||||
|
||||
pest()->tia()->filtered();
|
||||
PHP);
|
||||
$project->git()->commit('filter tia runs');
|
||||
|
||||
$result = $project->pest('--no-tia', 'tests/Unit');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->tally())->toContain('4 passed')
|
||||
->and($result->output)->not->toContain('TIA does not apply to partial runs');
|
||||
})->skipOnWindows();
|
||||
@@ -29,10 +29,10 @@ test('an explicit path turns filtered mode off', function (): void {
|
||||
$result = $project->pest('--tia', '--filtered', 'tests/Unit');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
->and($result->output)->not->toContain('No affected tests found')
|
||||
expect($result->output)->not->toContain('No affected tests found')
|
||||
->and($result->tally())->toContain('4 passed')
|
||||
->and($delta->writtenCount())->toBe(4, $delta->summary())
|
||||
->and($result->replayed())->toBe(4, $result->describe())
|
||||
->and($delta->writtenCount())->toBe(0, $delta->summary())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
|
||||
@@ -21,28 +21,29 @@ test('a filtered run rewrites only the test that ran', function (): void {
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a filtered run under --tia announces that tia does not apply', function (): void {
|
||||
test('a filtered run under --tia replays the selected test', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$result = $project->pest('--tia', '--filter=adds two numbers');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
->and($delta->writtenCount())->toBe(1, $delta->summary())
|
||||
expect($result->output)->not->toContain('TIA does not apply to partial runs')
|
||||
->and($result->replayed())->toBe(1, $result->describe())
|
||||
->and($delta->writtenCount())->toBe(0, $delta->summary())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a test suffix narrows the tier even though every test runs', function (): void {
|
||||
test('a test suffix narrows the tier and replays every selected test', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$result = $project->pest('--tia', '--test-suffix=Test.php');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed')
|
||||
->and($delta->writtenCount())->toBe(Project::TOTAL_TESTS, $delta->summary())
|
||||
expect($result->tally())->toContain(Project::TOTAL_TESTS.' passed')
|
||||
->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
||||
->and($delta->writtenCount())->toBe(0, $delta->summary())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
@@ -73,7 +74,7 @@ test('a dirty run narrows to the uncommitted test edit', function (): void {
|
||||
$result = $project->pest('--tia', '--dirty');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
expect($result->affected())->toBe(3, $result->describe())
|
||||
->and($result->tally())->toContain('3 passed')
|
||||
->and($delta->removed())->toBe(0, $delta->summary())
|
||||
->and($delta->structureMoved())->toBeFalse($delta->summary());
|
||||
@@ -86,13 +87,14 @@ test('filtered mode yields to an explicit filter', function (): void {
|
||||
$result = $project->pest('--tia', '--filtered', '--filter=adds two numbers');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
expect($result->output)->not->toContain('No affected tests found')
|
||||
->and($result->tally())->toContain('1 passed')
|
||||
->and($delta->writtenCount())->toBe(1, $delta->summary())
|
||||
->and($result->replayed())->toBe(1, $result->describe())
|
||||
->and($delta->writtenCount())->toBe(0, $delta->summary())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('an env flag narrows exactly like the option it mirrors', function (string $variable): void {
|
||||
test('an env flag narrows exactly like the option it mirrors', function (string $variable, int $replayed): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
@@ -102,10 +104,14 @@ test('an env flag narrows exactly like the option it mirrors', function (string
|
||||
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
->and($delta->writtenCount())->toBe(1, $delta->summary())
|
||||
expect($result->output)->not->toContain('TIA does not apply to partial runs')
|
||||
->and($result->replayed())->toBe($replayed, $result->describe())
|
||||
->and($delta->writtenCount())->toBe(1 - $replayed, $delta->summary())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->with(['PEST_TIA', 'PEST_TIA_FILTERED'])->skipOnWindows();
|
||||
})->with([
|
||||
'PEST_TIA' => ['PEST_TIA', 1],
|
||||
'PEST_TIA_FILTERED' => ['PEST_TIA_FILTERED', 0],
|
||||
])->skipOnWindows();
|
||||
|
||||
test('a partial run does not purge the graph even with --fresh', function (): void {
|
||||
$project = Project::make('master');
|
||||
@@ -151,20 +157,20 @@ test('a shard is a partial run', function (): void {
|
||||
$result = $project->pest('--tia', '--shard=1/2');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
expect($result->replayed())->toBeGreaterThan(0, $result->describe())
|
||||
->and($delta->removed())->toBe(0, $delta->summary())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a parallel partial run records the test that ran, like a sequential one', function (): void {
|
||||
test('a parallel partial run replays the selected test, like a sequential one', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$result = $project->pest('--tia', '--parallel', '--processes=2', '--filter=adds two numbers');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
->and($result->tally())->toContain('1 passed')
|
||||
->and($delta->writtenCount())->toBe(1, $delta->summary())
|
||||
expect($result->tally())->toContain('1 passed')
|
||||
->and($result->replayed())->toBe(1, $result->describe())
|
||||
->and($delta->writtenCount())->toBe(0, $delta->summary())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
afterEach(function (): void {
|
||||
Project::destroyAll();
|
||||
});
|
||||
|
||||
test('a partial run without a coverage driver names its selection, not the full suite', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$project->write('app/Calculator.php', str_replace(
|
||||
'return $a + $b;',
|
||||
'return $b + $a;',
|
||||
(string) file_get_contents($project->path().'/app/Calculator.php'),
|
||||
));
|
||||
|
||||
$result = $project->pestWithEnvironment($project->path(), ['XDEBUG_MODE' => 'off'], '--tia', 'tests/Unit');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('no coverage driver is available')
|
||||
->and($result->tally())->toContain('4 passed')
|
||||
->and($result->output)->not->toContain('Running the full suite');
|
||||
})->skipOnWindows();
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Plugins\Tia;
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
afterEach(function (): void {
|
||||
Project::destroyAll();
|
||||
});
|
||||
|
||||
function tiaEditGreeterTest(Project $project): void
|
||||
{
|
||||
$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!');
|
||||
})->group('greeter');
|
||||
|
||||
test('greets the world', function (): void {
|
||||
expect((new Greeter)->greet('world'))->toBe('Hello, world!');
|
||||
})->group('greeter');
|
||||
|
||||
test('greets again', function (): void {
|
||||
expect((new Greeter)->greet('again'))->toBe('Hello, again!');
|
||||
})->group('greeter');
|
||||
PHP);
|
||||
}
|
||||
|
||||
test('a partial run replays the unaffected tests of its selection', function (array $selection, int $affected, int $replayed, array $mode): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
tiaEditGreeterTest($project);
|
||||
|
||||
$result = $project->pest('--tia', ...$selection, ...$mode);
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->not->toContain('TIA does not apply to partial runs')
|
||||
->and($result->tally())->toContain(($affected + $replayed).' passed')
|
||||
->and($result->affected())->toBe($affected, $result->describe())
|
||||
->and($result->replayed())->toBe($replayed, $result->describe())
|
||||
->and($delta->structureMoved())->toBeFalse($delta->summary())
|
||||
->and($delta->shaMoved())->toBeFalse($delta->summary())
|
||||
->and($delta->treeMoved())->toBeFalse($delta->summary())
|
||||
->and($delta->removed())->toBe(0, $delta->summary());
|
||||
})->with([
|
||||
'an explicit path' => [['tests/Unit'], 3, 2],
|
||||
'--filter' => [['--filter=greets a person|adds two numbers'], 1, 1],
|
||||
'--group' => [['--group=greeter'], 3, 0],
|
||||
'--exclude-group' => [['--exclude-group=greeter'], 0, 4],
|
||||
'--testsuite' => [['--testsuite=default'], 3, 4],
|
||||
'--dirty' => [['--dirty'], 3, 0],
|
||||
])->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();
|
||||
|
||||
test('a partial run re-runs a cached failure in its selection', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master', failing: ['adds two numbers']);
|
||||
|
||||
$result = $project->pest('--tia', 'tests/Unit');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->tally())->toContain('4 passed')
|
||||
->and($result->replayed())->toBe(3, $result->describe())
|
||||
->and($delta->writtenCount())->toBe(1, $delta->summary())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a partial run leaves the affected tests to the next complete run', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
tiaEditGreeterTest($project);
|
||||
|
||||
$project->pest('--tia', '--exclude-group=greeter');
|
||||
$result = $project->pest('--tia');
|
||||
|
||||
expect($result->affected())->toBe(3, $result->describe())
|
||||
->and($result->replayed())->toBe(4, $result->describe());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a partial run without a graph runs its selection and records nothing', function (): void {
|
||||
$project = Project::make('master');
|
||||
|
||||
$result = $project->pest('--tia', 'tests/Unit');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('a partial run cannot record the dependency graph')
|
||||
->and($result->tally())->toContain('4 passed')
|
||||
->and($result->replayed())->toBe(0, $result->describe())
|
||||
->and($project->graphExists())->toBeFalse();
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a partial run with a coverage report runs its selection directly', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$result = $project->pest('--tia', 'tests/Unit', '--coverage-text');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->output)->toContain('TIA does not apply to partial runs')
|
||||
->and($result->replayed())->toBe(0, $result->describe())
|
||||
->and($delta->isResultsOnly())->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a partial run in a repository a complete run refuses runs its selection directly', function (Closure $prepare): void {
|
||||
$project = $prepare();
|
||||
|
||||
$result = $project->pest('--tia', 'tests/Unit');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->tally())->toContain('4 passed')
|
||||
->and($result->output)->toContain('TIA does not apply to partial runs')
|
||||
->and($project->graphExists())->toBeFalse();
|
||||
})->with([
|
||||
'no remote' => [function (): Project {
|
||||
$project = Project::make('master');
|
||||
$project->git()->removeOrigin();
|
||||
$project->git()->switchTo('feature-x', new: true);
|
||||
|
||||
return $project;
|
||||
}],
|
||||
'no default branch' => [function (): Project {
|
||||
$project = Project::make('master');
|
||||
$project->git()->unsetOriginHead();
|
||||
$project->git()->config('init.defaultBranch', 'main');
|
||||
$project->git()->switchTo('feature-x', new: true);
|
||||
|
||||
return $project;
|
||||
}],
|
||||
'no commits' => [function (): Project {
|
||||
$project = Project::withoutGit();
|
||||
$project->git()->run(['init', '--quiet']);
|
||||
$project->git()->run(['checkout', '--quiet', '-b', 'master']);
|
||||
$project->git()->addOrigin();
|
||||
|
||||
return $project;
|
||||
}],
|
||||
'no git' => [fn (): Project => Project::withoutGit()],
|
||||
])->skipOnWindows();
|
||||
|
||||
test('a partial run leaves a graph recorded in a different environment alone', function (string $section, string $key, string $value): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
$project->mutateGraph(function (array $graph) use ($section, $key, $value): array {
|
||||
$graph['fingerprint'][$section][$key] = $value;
|
||||
|
||||
return $graph;
|
||||
});
|
||||
file_put_contents($project->graphDir().'/'.Tia::KEY_COVERAGE_CACHE, 'cached coverage');
|
||||
$project->snapshot();
|
||||
|
||||
$result = $project->pest('--tia', '--filter=adds two numbers');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('The graph was recorded in a different environment ('.$key.')')
|
||||
->and($result->tally())->toContain('1 passed')
|
||||
->and($result->replayed())->toBe(0, $result->describe())
|
||||
->and($delta->isHardSuppressed())->toBeTrue($delta->summary())
|
||||
->and($project->graphDir().'/'.Tia::KEY_COVERAGE_CACHE)->toBeFile();
|
||||
})->with([
|
||||
'another PHP minor version' => ['environmental', 'php_minor', '7.4'],
|
||||
'another lockfile' => ['structural', 'composer_lock', 'a-lockfile-this-project-never-had'],
|
||||
])->skipOnWindows();
|
||||
Reference in New Issue
Block a user