From 8eb2892f693d36a74cf604bce76dfcd33eb89ed3 Mon Sep 17 00:00:00 2001 From: Tiago Santos <48293277+tiagoabsantos@users.noreply.github.com> Date: Mon, 2 Nov 2026 11:56:12 +0000 Subject: [PATCH] fix(tia): skip locally() when the process is on CI (#1999) * fix(tia): skip locally() when the process is on CI locally() only looked at --ci, so a CI job that followed the docs and ran plain pest replayed cached results. Fixes #1970 * test(tia): clear CI variables so the local assertion passes on CI * test: update snapshots for TIA CI detection --------- Co-authored-by: Tiago Santos Co-authored-by: Punyapal Shah --- src/Plugins/Tia.php | 8 +++- src/Support/Ci.php | 2 +- tests/.snapshots/success.txt | 5 +- tests/Features/Tia/Locally.php | 28 +++++++++++ tests/Unit/Plugins/Tia/IsEnabledForRun.php | 56 ++++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 6 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 tests/Features/Tia/Locally.php diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index 7c7b2234..c107c115 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -33,6 +33,7 @@ use Pest\Plugins\Tia\SourceScope; use Pest\Plugins\Tia\Storage; use Pest\Plugins\Tia\TableExtractor; use Pest\Plugins\Tia\WatchPatterns; +use Pest\Support\Ci; use Pest\Support\Container; use Pest\Support\Git; use Pest\Support\View; @@ -334,7 +335,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument return true; } - return ! self::argumentPresent('--ci', $arguments); + return ! Ci::isRunning() && ! self::argumentPresent('--ci', $arguments); } /** @@ -482,8 +483,11 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument self::applyWatchPatternMarks($arguments, $watchPatterns); $disabled = $this->hasArgument(self::NO_OPTION, $arguments); $cliEnabled = $this->hasArgument(self::OPTION, $arguments) || self::envFlagEnabled(self::ENV_TIA); + $runningOnCi = Ci::isRunning() + || $this->hasArgument('--ci', $arguments) + || $this->hasArgument('--ci', $this->originalArguments); $alwaysEnabled = $watchPatterns->isEnabled() - && (! $watchPatterns->isLocally() || Environment::name() === Environment::LOCAL); + && (! $watchPatterns->isLocally() || ! $runningOnCi); if (! $isWorker && ! $disabled && ($cliEnabled || $alwaysEnabled)) { $this->guardUnsupportedOptions($arguments); } diff --git a/src/Support/Ci.php b/src/Support/Ci.php index 3c3620b4..3217cb3c 100644 --- a/src/Support/Ci.php +++ b/src/Support/Ci.php @@ -14,7 +14,7 @@ final class Ci /** * @var list */ - private const array ENVIRONMENT_VARIABLES = [ + public const array ENVIRONMENT_VARIABLES = [ 'CI', 'GITHUB_ACTIONS', 'GITLAB_CI', diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index d9926eba..f9116164 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1947,6 +1947,9 @@ PASS Tests\Unit\Plugins\Tia\IsEnabledForRun ✓ does not throw when an integer --random-order-seed is passed as a separate argv element ✓ still detects --tia when an integer argument is present + ✓ locally configured tia runs outside CI + ✓ locally configured tia stays off when CI is detected with ('CI') + ✓ locally configured tia stays off when CI is detected with ('GITHUB_ACTIONS') PASS Tests\Unit\Plugins\Tia\JsModuleGraph ✓ it accepts a page directory candidate only when every segment matches the casing on disk with dataset "exact" @@ -2289,4 +2292,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, 36 skipped, 1621 passed (3582 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 36 skipped, 1624 passed (3587 assertions) \ No newline at end of file diff --git a/tests/Features/Tia/Locally.php b/tests/Features/Tia/Locally.php new file mode 100644 index 00000000..86689ead --- /dev/null +++ b/tests/Features/Tia/Locally.php @@ -0,0 +1,28 @@ +path('tests/Pest.php'); + file_put_contents($pestFile, file_get_contents($pestFile)."\npest()->tia()->locally();\n"); + $project->seed('master'); + + $onCi = $project->pestWithEnvironment($project->path(), ['CI' => 'true']); + + expect($onCi->exitCode)->toBe(0, $onCi->describe()) + ->and($onCi->output)->not->toContain('TIA mode') + ->and($onCi->replayed())->toBe(0) + ->and($onCi->tally())->toContain(Project::TOTAL_TESTS.' passed'); + + $local = $project->pest(); + + expect($local->exitCode)->toBe(0, $local->describe()) + ->and($local->replayed())->toBe(Project::TOTAL_TESTS, $local->describe()); +})->skipOnWindows(); diff --git a/tests/Unit/Plugins/Tia/IsEnabledForRun.php b/tests/Unit/Plugins/Tia/IsEnabledForRun.php index 8c818d44..f6daac85 100644 --- a/tests/Unit/Plugins/Tia/IsEnabledForRun.php +++ b/tests/Unit/Plugins/Tia/IsEnabledForRun.php @@ -1,6 +1,9 @@ toBeTrue(); }); + +test('locally configured tia runs outside CI', function (): void { + $previous = []; + + foreach (Ci::ENVIRONMENT_VARIABLES as $variable) { + $previous[$variable] = getenv($variable); + putenv($variable); + } + + try { + withLocalTia(function (): void { + expect(Tia::isEnabledForRun([]))->toBeTrue(); + }); + } finally { + foreach ($previous as $variable => $value) { + putenv($value === false ? $variable : $variable.'='.$value); + } + } +}); + +test('locally configured tia stays off when CI is detected', function (string $variable): void { + withLocalTia(function () use ($variable): void { + $previous = getenv($variable); + putenv($variable.'=true'); + + try { + expect(Tia::isEnabledForRun([]))->toBeFalse() + ->and(Tia::isEnabledForRun(['--ci']))->toBeFalse(); + } finally { + putenv($previous === false ? $variable : $variable.'='.$previous); + } + }); +})->with(['CI', 'GITHUB_ACTIONS']); + +function withLocalTia(Closure $callback): void +{ + $patterns = Container::getInstance()->get(WatchPatterns::class); + $reflection = new ReflectionClass($patterns); + $enabled = $reflection->getProperty('enabled'); + $locally = $reflection->getProperty('locally'); + $wasEnabled = $enabled->getValue($patterns); + $wasLocally = $locally->getValue($patterns); + + $patterns->markEnabled(); + $patterns->markLocally(); + + try { + $callback(); + } finally { + $enabled->setValue($patterns, $wasEnabled); + $locally->setValue($patterns, $wasLocally); + } +} diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index bd6d3b4a..572ff80b 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -20,7 +20,7 @@ test('parallel', function () use ($run): void { $output = $run('--exclude-group=integration'); $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 28 skipped, 1602 passed (3524 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 28 skipped, 1605 passed (3529 assertions)'; expect($output) ->toContain("Tests: {$expected}")