diff --git a/src/Exceptions/GitTimedOut.php b/src/Exceptions/GitTimedOut.php new file mode 100644 index 00000000..1f0cfafa --- /dev/null +++ b/src/Exceptions/GitTimedOut.php @@ -0,0 +1,24 @@ + $arguments + */ + public function __construct(array $arguments, float $timeout) + { + parent::__construct(sprintf('Git did not answer within %s seconds: git %s', $timeout, implode(' ', $arguments))); + } +} diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index 013d2313..6a60f8d3 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -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 ''; diff --git a/src/Plugins/Tia/Fingerprint.php b/src/Plugins/Tia/Fingerprint.php index a51ffee1..52f7e1e0 100644 --- a/src/Plugins/Tia/Fingerprint.php +++ b/src/Plugins/Tia/Fingerprint.php @@ -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; } diff --git a/src/Support/Git.php b/src/Support/Git.php index dbed0cc5..1df64d58 100644 --- a/src/Support/Git.php +++ b/src/Support/Git.php @@ -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, diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 17a43140..06b35572 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -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) diff --git a/tests/Features/Tia/SlowGit.php b/tests/Features/Tia/SlowGit.php new file mode 100644 index 00000000..c70274e8 --- /dev/null +++ b/tests/Features/Tia/SlowGit.php @@ -0,0 +1,118 @@ +find('git'); + + assert(is_string($git)); + + $directory = $project->path('.fake-git'); + $realGit = escapeshellarg($git); + + mkdir($directory); + file_put_contents($directory.'/git', <<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(); diff --git a/tests/Unit/Support/Git.php b/tests/Unit/Support/Git.php new file mode 100644 index 00000000..0401e730 --- /dev/null +++ b/tests/Unit/Support/Git.php @@ -0,0 +1,13 @@ + $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(); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 555ef25b..ad31332e 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -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}")