diff --git a/bin/pest b/bin/pest index ba6d9810..088755ff 100755 --- a/bin/pest +++ b/bin/pest @@ -129,6 +129,60 @@ use Symfony\Component\Console\Output\ConsoleOutput; } } + $optionsWithSeparateValue = [ + '-c', '-d', '--atleast-version', '--bootstrap', '--cache-directory', '--columns', '--configuration', + '--coverage-filter', '--coverage-clover', '--coverage-cobertura', '--coverage-crap4j', '--coverage-html', + '--coverage-jsonl', '--coverage-openclover', '--coverage-php', '--coverage-xml', '--default-time-limit', + '--diff-context', '--exclude-group', '--filter', '--exclude-filter', '--generate-baseline', '--use-baseline', + '--group', '--covers', '--uses', '--requires-php-extension', '--include-path', '--list-tests-xml', + '--log-junit', '--log-otr', '--log-teamcity', '--order-by', '--random-order-seed', '--repeat', + '--timeout', '--testdox-html', '--testdox-text', '--test-suffix', '--testsuite', '--exclude-testsuite', + '--test-files-file', '--test-id-filter-file', '--run-test-id', '--log-events-text', + '--log-events-verbose-text', '--extension', + ]; + + $isOptionValue = false; + + // Convert namespace-style arguments (e.g. Tests\Unit\TestName) to file paths. + foreach ($arguments as $key => $value) { + if ($isOptionValue) { + $isOptionValue = false; + + continue; + } + + if (str_starts_with($value, '-')) { + $isOptionValue = in_array($value, $optionsWithSeparateValue, true); + + continue; + } + + if (! str_contains($value, '\\')) { + continue; + } + + $path = str_replace('\\', DIRECTORY_SEPARATOR, $value); + + if (! str_ends_with($path, '.php')) { + $path .= '.php'; + } + + if (file_exists($path)) { + $arguments[$key] = $path; + + continue; + } + + // Try lowercasing the first directory segment (e.g. Tests -> tests). + $segments = explode(DIRECTORY_SEPARATOR, $path); + $segments[0] = lcfirst($segments[0]); + $lowercasedPath = implode(DIRECTORY_SEPARATOR, $segments); + + if (file_exists($lowercasedPath)) { + $arguments[$key] = $lowercasedPath; + } + } + // Used when Pest is required using composer. $autoloadPath = $GLOBALS['_composer_autoload_path'] ?? dirname(__DIR__, 3).'/autoload.php'; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index c6172463..6112430c 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2536,6 +2536,9 @@ PASS Tests\Visual\SingleTestOrDirectory ✓ allows to run a single test ✓ allows to run a directory + ✓ allows to run a single test using its namespace + ✓ keeps namespace filters unchanged + ✓ converts a namespace after the retry flag ✓ it disable decorating printer when colors is set to never WARN Tests\Visual\Success @@ -2566,4 +2569,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, 38 skipped, 1860 passed (4277 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1863 passed (4281 assertions) \ No newline at end of file diff --git a/tests/Visual/SingleTestOrDirectory.php b/tests/Visual/SingleTestOrDirectory.php index 6c8cc3b6..a7db3431 100644 --- a/tests/Visual/SingleTestOrDirectory.php +++ b/tests/Visual/SingleTestOrDirectory.php @@ -30,6 +30,44 @@ test('allows to run a directory', function () use ($run, $snapshot): void { expect($run('tests/Fixtures/DirectoryWithTests'))->toContain($snapshot('allows-to-run-a-directory')); })->skipOnWindows(); +test('allows to run a single test using its namespace', function () use ($run, $snapshot): void { + expect($run('Tests\\Fixtures\\DirectoryWithTests\\ExampleTest'))->toContain($snapshot('allows-to-run-a-single-test')); +})->skipOnWindows(); + +test('keeps namespace filters unchanged', function () use ($snapshot): void { + $process = new Process([ + 'php', + 'bin/pest', + 'tests/Fixtures/DirectoryWithTests', + '--filter', + 'Tests\\\\Fixtures\\\\DirectoryWithTests\\\\ExampleTest', + '--colors=never', + ], dirname(__DIR__, 2), + ['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'], + ); + + $process->run(); + + expect(removeAnsiEscapeSequences($process->getOutput()))->toContain($snapshot('allows-to-run-a-single-test')); +})->skipOnWindows(); + +test('converts a namespace after the retry flag', function () use ($snapshot): void { + $process = new Process([ + 'php', + 'bin/pest', + '--retry', + 'Tests\\Fixtures\\DirectoryWithTests\\ExampleTest', + '--colors=never', + ], dirname(__DIR__, 2), + ['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'], + ); + + $process->run(); + + expect($process->getExitCode())->toBe(0) + ->and(removeAnsiEscapeSequences($process->getOutput()))->toContain($snapshot('allows-to-run-a-single-test')); +})->skipOnWindows(); + it('disable decorating printer when colors is set to never', function () use ($snapshot): void { $process = new Process([ 'php',