mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
fix(tia): report a git timeout like any other git failure (#2069)
* fix(tia): report a git timeout like any other git failure * fix(tia): raise a git timeout so no git helper reads it as an answer * test(tia): cover a slow git check-ignore and stop probing git after a timeout --------- Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Exceptions;
|
||||
|
||||
use NunoMaduro\Collision\Contracts\RenderlessEditor;
|
||||
use NunoMaduro\Collision\Contracts\RenderlessTrace;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Exception\ExceptionInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class GitTimedOut extends RuntimeException implements ExceptionInterface, RenderlessEditor, RenderlessTrace
|
||||
{
|
||||
/**
|
||||
* @param array<int, string> $arguments
|
||||
*/
|
||||
public function __construct(array $arguments, float $timeout)
|
||||
{
|
||||
parent::__construct(sprintf('Git did not answer within %s seconds: git %s', $timeout, implode(' ', $arguments)));
|
||||
}
|
||||
}
|
||||
+16
-5
@@ -10,6 +10,7 @@ use Pest\Contracts\Plugins\AddsOutput;
|
||||
use Pest\Contracts\Plugins\HandlesArguments;
|
||||
use Pest\Contracts\Plugins\HandlesOriginalArguments;
|
||||
use Pest\Contracts\Plugins\Terminable;
|
||||
use Pest\Exceptions\GitTimedOut;
|
||||
use Pest\Exceptions\InvalidOption;
|
||||
use Pest\Exceptions\MissingDependency;
|
||||
use Pest\Exceptions\NoAffectedTestsFound;
|
||||
@@ -2190,7 +2191,11 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$graph->pruneMissingTests();
|
||||
$graph->pruneResultsForMissingFiles($this->branch);
|
||||
|
||||
$branches = new ChangedFiles(TestSuite::getInstance()->rootPath)->branchNames();
|
||||
try {
|
||||
$branches = new ChangedFiles(TestSuite::getInstance()->rootPath)->branchNames();
|
||||
} catch (GitTimedOut) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($branches === null) {
|
||||
return;
|
||||
@@ -2231,7 +2236,9 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
try {
|
||||
$this->resolveBranch($projectRoot);
|
||||
} catch (MissingDependency|TiaGitFailed) {
|
||||
} catch (MissingDependency) {
|
||||
} catch (TiaGitFailed|GitTimedOut) {
|
||||
return;
|
||||
}
|
||||
|
||||
$graph->setFallbackBranch($this->fallbackBranch);
|
||||
@@ -2392,8 +2399,6 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
return;
|
||||
}
|
||||
|
||||
$this->branchResolved = true;
|
||||
|
||||
$changedFiles = new ChangedFiles($projectRoot);
|
||||
|
||||
$resolved = $this->resolveFallbackBranch($changedFiles);
|
||||
@@ -2406,6 +2411,8 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->branch = $changedFiles->currentBranch()
|
||||
?? CiBranch::detectCurrent()
|
||||
?? $this->workspaceBranch($projectRoot);
|
||||
|
||||
$this->branchResolved = true;
|
||||
}
|
||||
|
||||
private function workspaceBranch(string $projectRoot): string
|
||||
@@ -2641,7 +2648,11 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
$git = new Git($projectRoot);
|
||||
|
||||
$baseline = $git->show($sha, $git->pathPrefix().'composer.lock');
|
||||
try {
|
||||
$baseline = $git->show($sha, $git->pathPrefix().'composer.lock');
|
||||
} catch (GitTimedOut) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($baseline === null) {
|
||||
return '';
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
use Pest\Exceptions\GitTimedOut;
|
||||
use Pest\Plugins\Tia\Contracts\Lockfile;
|
||||
use Pest\Support\Git;
|
||||
use Symfony\Component\Process\Exception\ExceptionInterface as ProcessException;
|
||||
@@ -316,7 +317,7 @@ final readonly class Fingerprint
|
||||
|
||||
try {
|
||||
$result = new Git($projectRoot)->checkIgnore([$relativePath]);
|
||||
} catch (ProcessException) {
|
||||
} catch (ProcessException|GitTimedOut) {
|
||||
return $cache[$key] = true;
|
||||
}
|
||||
|
||||
|
||||
+7
-1
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\Support;
|
||||
|
||||
use Pest\Exceptions\GitTimedOut;
|
||||
use Symfony\Component\Process\Exception\ProcessTimedOutException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
@@ -108,7 +110,11 @@ final readonly class Git
|
||||
$process->setInput($input);
|
||||
}
|
||||
|
||||
$process->run();
|
||||
try {
|
||||
$process->run();
|
||||
} catch (ProcessTimedOutException) {
|
||||
throw new GitTimedOut($arguments, $this->timeout);
|
||||
}
|
||||
|
||||
return [
|
||||
'exitCode' => $process->getExitCode() ?? 1,
|
||||
|
||||
@@ -2582,6 +2582,12 @@
|
||||
✓ it ensures the given closures reports the correct class name
|
||||
✓ it ensures the given closures reports the correct class name and suggests the [pest()] function
|
||||
|
||||
PASS Tests\Unit\Support\Git
|
||||
✓ it raises a timed out command instead of answering with ('result')
|
||||
✓ it raises a timed out command instead of answering with ('raw')
|
||||
✓ it raises a timed out command instead of answering with ('output')
|
||||
✓ it raises a timed out command instead of answering with ('succeeds')
|
||||
|
||||
PASS Tests\Unit\Support\HigherOrderMessage
|
||||
✓ undefined method exceptions
|
||||
|
||||
@@ -2839,4 +2845,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, 44 skipped, 2107 passed (4644 assertions)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 44 skipped, 2111 passed (4652 assertions)
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Symfony\Component\Process\ExecutableFinder;
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
afterEach(function (): void {
|
||||
Project::destroyAll();
|
||||
});
|
||||
|
||||
function fakeGitPath(Project $project, string $case): string
|
||||
{
|
||||
$git = (new ExecutableFinder)->find('git');
|
||||
|
||||
assert(is_string($git));
|
||||
|
||||
$directory = $project->path('.fake-git');
|
||||
$realGit = escapeshellarg($git);
|
||||
|
||||
mkdir($directory);
|
||||
file_put_contents($directory.'/git', <<<SH
|
||||
#!/bin/sh
|
||||
case "\$1 \$2" in
|
||||
{$case}
|
||||
esac
|
||||
exec {$realGit} "\$@"
|
||||
SH);
|
||||
chmod($directory.'/git', 0755);
|
||||
|
||||
if (! is_dir($project->path('.git/info'))) {
|
||||
mkdir($project->path('.git/info'));
|
||||
}
|
||||
|
||||
file_put_contents($project->path('.git/info/exclude'), "/.fake-git/\n", FILE_APPEND);
|
||||
|
||||
return $directory.PATH_SEPARATOR.getenv('PATH');
|
||||
}
|
||||
|
||||
test('a slow git while naming the branch records nothing without --tia', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$path = fakeGitPath($project, '"symbolic-ref "*) exec sleep 6 ;;');
|
||||
$result = $project->pestWithEnvironment($project->path(), ['PATH' => $path]);
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed')
|
||||
->and($result->output)->not->toContain('Git did not answer')
|
||||
->and($delta->baselineUntouched('master'))->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a slow branch listing after the suite keeps the run results without --tia', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$path = fakeGitPath($project, '"for-each-ref "*) exec sleep 6 ;;');
|
||||
$result = $project->pestWithEnvironment($project->path(), ['PATH' => $path]);
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed')
|
||||
->and($result->output)->not->toContain('Git did not answer')
|
||||
->and($project->branchKeys())->toBe(['master'])
|
||||
->and($delta->writtenCount())->toBe(Project::TOTAL_TESTS, $delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a run without --tia records nothing when git cannot name the current branch', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$path = fakeGitPath($project, '"rev-parse --abbrev-ref") exit 128 ;;');
|
||||
$result = $project->pestWithEnvironment($project->path(), ['PATH' => $path]);
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed')
|
||||
->and($project->branchKeys())->toBe(['master'])
|
||||
->and($delta->baselineUntouched('master'))->toBeTrue($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a slow git stops a --tia run with the timeout', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$path = fakeGitPath($project, '"symbolic-ref "*) exec sleep 6 ;;');
|
||||
$result = $project->pestWithEnvironment($project->path(), ['PATH' => $path], '--tia');
|
||||
|
||||
expect($result->exitCode)->toBe(1, $result->describe())
|
||||
->and($result->output)->toContain('Git did not answer within 5 seconds: git symbolic-ref')
|
||||
->and($result->output)->not->toContain('default branch');
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a slow git check-ignore keeps composer.lock in the fingerprint', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$path = fakeGitPath($project, '"check-ignore -z") exec sleep 6 ;;');
|
||||
$result = $project->pestWithEnvironment($project->path(), ['PATH' => $path], '--tia');
|
||||
$delta = $project->delta();
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->not->toContain('Git did not answer')
|
||||
->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
||||
->and($delta->structureMoved())->toBeFalse($delta->summary());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a slow git stops a --tia run with the command that timed out first', function (): void {
|
||||
$project = Project::make('master');
|
||||
$project->seed('master');
|
||||
|
||||
$path = fakeGitPath($project, '"symbolic-ref "*|"rev-parse --git-dir") exec sleep 6 ;;');
|
||||
$result = $project->pestWithEnvironment($project->path(), ['PATH' => $path], '--tia');
|
||||
|
||||
expect($result->exitCode)->toBe(1, $result->describe())
|
||||
->and($result->output)->toContain('Git did not answer within 5 seconds: git symbolic-ref');
|
||||
})->skipOnWindows();
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Exceptions\GitTimedOut;
|
||||
use Pest\Support\Git;
|
||||
|
||||
it('raises a timed out command instead of answering', function (string $method): void {
|
||||
$git = new Git(timeout: 0.2);
|
||||
|
||||
expect(fn (): mixed => $git->{$method}(['-c', 'alias.slow=!sleep 5', 'slow']))
|
||||
->toThrow(GitTimedOut::class, 'Git did not answer within 0.2 seconds: git -c alias.slow=!sleep 5 slow');
|
||||
})->with(['result', 'raw', 'output', 'succeeds'])->skipOnWindows();
|
||||
@@ -21,7 +21,7 @@ test('parallel', function () use ($run): void {
|
||||
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
|
||||
$profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile');
|
||||
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 36 skipped, 1976 passed (4114 assertions)';
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 36 skipped, 1980 passed (4122 assertions)';
|
||||
|
||||
expect($output)
|
||||
->toContain("Tests: {$expected}")
|
||||
|
||||
Reference in New Issue
Block a user