From 67d3733eb2211d7160875dfd0562e121f8f0b0ed Mon Sep 17 00:00:00 2001 From: Ikki Nishihara Date: Fri, 20 Nov 2026 18:50:17 +0900 Subject: [PATCH] Allow help and version before the test suite is initialized (#2051) Co-authored-by: haranishi <223786623+haranishi@users.noreply.github.com> Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> Co-authored-by: Punyapal Shah --- src/Bootstrappers/BootFiles.php | 10 +++++++--- tests/.snapshots/success.txt | 4 +++- tests/Features/Init.php | 6 +----- tests/Visual/Version.php | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Bootstrappers/BootFiles.php b/src/Bootstrappers/BootFiles.php index 8ddcd932..4c084f25 100644 --- a/src/Bootstrappers/BootFiles.php +++ b/src/Bootstrappers/BootFiles.php @@ -36,7 +36,7 @@ final class BootFiles implements Bootstrapper public function boot(): void { - if ($this->runningInit()) { + if ($this->hasCliOption('--init')) { return; } @@ -44,6 +44,10 @@ final class BootFiles implements Bootstrapper $testsPath = $rootPath.DIRECTORY_SEPARATOR.testDirectory(); if (! is_dir($testsPath)) { + if ($this->hasCliOption('--help', '--version')) { + return; + } + throw new FatalException(sprintf('The test directory [%s] does not exist.', $testsPath)); } @@ -69,7 +73,7 @@ final class BootFiles implements Bootstrapper $this->bootDatasets($testsPath); } - private function runningInit(): bool + private function hasCliOption(string ...$options): bool { $arguments = $_SERVER['argv'] ?? null; @@ -78,7 +82,7 @@ final class BootFiles implements Bootstrapper } /** @var array $arguments */ - return $this->hasArgument('--init', $arguments); + return array_any($options, fn (string $option): bool => $this->hasArgument($option, $arguments)); } private function load(string $filename): void diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 8a57548b..c6172463 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2559,9 +2559,11 @@ WARN Tests\Visual\Version - visual snapshot of help command output + ✓ prints information when the tests directory is missing with dataset "help" + ✓ prints information when the tests directory is missing with dataset "version" PASS Testsexternal\Features\Expect\toMatchSnapshot ✓ 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, 38 skipped, 1858 passed (4273 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1860 passed (4277 assertions) \ No newline at end of file diff --git a/tests/Features/Init.php b/tests/Features/Init.php index 6222fa26..9fa04ae8 100644 --- a/tests/Features/Init.php +++ b/tests/Features/Init.php @@ -63,7 +63,7 @@ test('a missing tests directory does not crash while resolving tia state', funct $root = makeInitProject(); try { - $process = runInitProject($root, '--version'); + $process = runInitProject($root); expect($process->getExitCode())->not->toBe(0) ->and($process->getOutput())->toContain('does not exist') @@ -110,10 +110,6 @@ require {$autoload}; \$arguments = array_slice(\$argv, 1); -if (\$arguments === []) { - \$arguments = ['--version']; -} - \$GLOBALS['_composer_bin_dir'] = getcwd().'/vendor/bin'; \$_SERVER['argv'] = ['pest', ...\$arguments]; diff --git a/tests/Visual/Version.php b/tests/Visual/Version.php index 1f923452..5ea258f9 100644 --- a/tests/Visual/Version.php +++ b/tests/Visual/Version.php @@ -1,6 +1,7 @@ toMatchSnapshot(); })->skipOnWindows()->skip(! getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE')); + +test('prints information when the tests directory is missing', function (string $option, string $expected): void { + $project = Project::withoutGit(); + + try { + rename($project->path('tests'), $project->path('fixtures')); + unlink($project->path('phpunit.xml')); + + $result = $project->pest($option); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->output)->toContain($expected); + } finally { + Project::destroyAll(); + } +})->with([ + 'help' => ['--help', '--init'], + 'version' => ['--version', 'Pest Testing Framework'], +]);