diff --git a/src/Bootstrappers/BootFiles.php b/src/Bootstrappers/BootFiles.php index d8848fe0..8ddcd932 100644 --- a/src/Bootstrappers/BootFiles.php +++ b/src/Bootstrappers/BootFiles.php @@ -6,6 +6,7 @@ namespace Pest\Bootstrappers; use Pest\Contracts\Bootstrapper; use Pest\Exceptions\FatalException; +use Pest\Plugins\Concerns\HandleArguments; use Pest\Support\DatasetInfo; use Pest\Support\Str; use Pest\TestSuite; @@ -20,6 +21,8 @@ use function Pest\testDirectory; */ final class BootFiles implements Bootstrapper { + use HandleArguments; + /** * @var array */ @@ -33,6 +36,10 @@ final class BootFiles implements Bootstrapper public function boot(): void { + if ($this->runningInit()) { + return; + } + $rootPath = TestSuite::getInstance()->rootPath; $testsPath = $rootPath.DIRECTORY_SEPARATOR.testDirectory(); @@ -62,6 +69,18 @@ final class BootFiles implements Bootstrapper $this->bootDatasets($testsPath); } + private function runningInit(): bool + { + $arguments = $_SERVER['argv'] ?? null; + + if (! is_array($arguments)) { + return false; + } + + /** @var array $arguments */ + return $this->hasArgument('--init', $arguments); + } + private function load(string $filename): void { if (! Str::endsWith($filename, '.php')) { diff --git a/src/Plugins/Init.php b/src/Plugins/Init.php index fe0105bb..d5aea2be 100644 --- a/src/Plugins/Init.php +++ b/src/Plugins/Init.php @@ -17,6 +17,8 @@ use Symfony\Component\Console\Output\OutputInterface; */ final readonly class Init implements HandlesArguments { + use Concerns\HandleArguments; + private const string INIT_OPTION = '--init'; private const array STUBS = [ @@ -40,14 +42,9 @@ final readonly class Init implements HandlesArguments */ public function handleArguments(array $arguments): array { - if (! array_key_exists(1, $arguments)) { + if (! $this->hasArgument(self::INIT_OPTION, $arguments)) { return $arguments; } - if ($arguments[1] !== self::INIT_OPTION) { - return $arguments; - } - - unset($arguments[1]); $this->init(); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index fd069671..2ff35acd 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1280,6 +1280,12 @@ ✓ it is not incompleted because of test with assertions … a "describe" group of tests → it is incompleted + PASS Tests\Features\Init + ✓ init creates a suite when the tests directory is missing + ✓ init creates a suite when --init is not the first argument + ✓ init creates a suite when --init is passed with a value + ✓ a missing tests directory does not crash while resolving tia state + PASS Tests\Features\Issue ✓ it may be associated with an issue #1, #2 ✓ nested → it may be associated with an issue #1, #4, #5, #6, #3 @@ -2274,4 +2280,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, 35 skipped, 1613 passed (3519 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1617 passed (3540 assertions) \ No newline at end of file diff --git a/tests/Features/Init.php b/tests/Features/Init.php new file mode 100644 index 00000000..4344449b --- /dev/null +++ b/tests/Features/Init.php @@ -0,0 +1,171 @@ +getExitCode())->toBe(0) + ->and($process->getErrorOutput())->not->toContain('cannot be resolved') + ->and($process->getOutput())->toContain('File created.') + ->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile() + ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile() + ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'TestCase.php')->toBeFile() + ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Unit'.DIRECTORY_SEPARATOR.'ExampleTest.php')->toBeFile(); + } finally { + removeInitProject($root); + } +}); + +test('init creates a suite when --init is not the first argument', function (): void { + $root = makeInitProject(); + + try { + $process = runInitProject($root, '--colors=never', '--init'); + + expect($process->getExitCode())->toBe(0) + ->and($process->getErrorOutput())->not->toContain('cannot be resolved') + ->and($process->getOutput())->toContain('File created.') + ->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile() + ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile(); + } finally { + removeInitProject($root); + } +}); + +test('init creates a suite when --init is passed with a value', function (): void { + $root = makeInitProject(); + + try { + $process = runInitProject($root, '--init=pest'); + + expect($process->getExitCode())->toBe(0) + ->and($process->getErrorOutput())->not->toContain('cannot be resolved') + ->and($process->getOutput())->toContain('File created.') + ->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile() + ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile(); + } finally { + removeInitProject($root); + } +}); + +test('a missing tests directory does not crash while resolving tia state', function (): void { + $root = makeInitProject(); + + try { + $process = runInitProject($root, '--version'); + + expect($process->getExitCode())->not->toBe(0) + ->and($process->getOutput())->toContain('does not exist') + ->and($process->getErrorOutput())->not->toContain('cannot be resolved') + ->and($process->getOutput())->not->toContain('cannot be resolved'); + } finally { + removeInitProject($root); + } +}); + +function makeInitProject(): string +{ + $root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-init-'.bin2hex(random_bytes(4)); + + mkdir($root.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'bin', 0777, true); + + copy( + dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'pest-plugins.json', + $root.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'pest-plugins.json', + ); + + $autoload = var_export( + dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php', + true, + ); + + file_put_contents($root.DIRECTORY_SEPARATOR.'run.php', <<handle(\$_SERVER['argv'], \$_SERVER['argv']); + \$kernel->terminate(); +} catch (Throwable \$throwable) { + Panic::with(\$throwable); +} + +exit(\$result); +PHP); + + return $root; +} + +function runInitProject(string $root, string ...$command): Process +{ + $process = new Process( + [PHP_BINARY, 'run.php', ...$command], + $root, + [ + 'PEST_NO_SUPPORT' => 'true', + 'COLLISION_PRINTER' => 'DefaultPrinter', + 'COLLISION_IGNORE_DURATION' => 'true', + ], + ); + + $process->setTimeout(60); + $process->run(); + + return $process; +} + +function removeInitProject(string $root): void +{ + if (! is_dir($root)) { + return; + } + + $items = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST, + ); + + foreach ($items as $item) { + if ($item->isDir() && ! $item->isLink()) { + rmdir($item->getPathname()); + + continue; + } + + unlink($item->getPathname()); + } + + rmdir($root); +} diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 6f42099a..4e3fbf1c 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, 27 skipped, 1594 passed (3461 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1598 passed (3482 assertions)'; expect($output) ->toContain("Tests: {$expected}")