mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
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 <tsantos@lynixt.pt> Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
+6
-2
@@ -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);
|
||||
}
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ final class Ci
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private const array ENVIRONMENT_VARIABLES = [
|
||||
public const array ENVIRONMENT_VARIABLES = [
|
||||
'CI',
|
||||
'GITHUB_ACTIONS',
|
||||
'GITLAB_CI',
|
||||
|
||||
@@ -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)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 36 skipped, 1624 passed (3587 assertions)
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
afterEach(function (): void {
|
||||
Project::destroyAll();
|
||||
});
|
||||
|
||||
test('locally configured tia does not replay when the process is on CI', function (): void {
|
||||
$project = Project::make('master');
|
||||
$pestFile = $project->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();
|
||||
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
|
||||
use Pest\Plugins\Tia;
|
||||
use Pest\Plugins\Tia\WatchPatterns;
|
||||
use Pest\Support\Ci;
|
||||
use Pest\Support\Container;
|
||||
|
||||
test('does not throw when an integer --random-order-seed is passed as a separate argv element', function (): void {
|
||||
$arguments = ['--order-by=random', '--random-order-seed', 1782350398];
|
||||
@@ -13,3 +16,56 @@ test('still detects --tia when an integer argument is present', function (): voi
|
||||
|
||||
expect(Tia::isEnabledForRun($arguments))->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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user