diff --git a/src/Codeception/Command/Run.php b/src/Codeception/Command/Run.php index 4184f6f2cb..568b0ccda1 100644 --- a/src/Codeception/Command/Run.php +++ b/src/Codeception/Command/Run.php @@ -385,10 +385,10 @@ public function execute(InputInterface $input, OutputInterface $output) } if ($test) { - $filter = $this->matchFilteredTestName($test); - $userOptions['filter'] = $filter; + $userOptions['filter'] = $this->matchFilteredTestName($test); + } elseif ($suite) { + $userOptions['filter'] = $this->matchFilteredTestName($suite); } - if (!$this->options['silent'] && $config['settings']['shuffle']) { $this->output->writeln( "[Seed] " . $userOptions['seed'] . "" @@ -470,7 +470,8 @@ protected function matchSingleTest($suite, $config) list($file) = explode(':', $suite, -1); } $realPath = $cwd . DIRECTORY_SEPARATOR . $file; - if (file_exists($realPath) || is_dir($realPath)) { + if (file_exists($realPath) && strpos($realPath, $realTestDir) === 0) { + //only match test if file is in tests directory return $this->matchTestFromFilename( $cwd . DIRECTORY_SEPARATOR . $suite, $realTestDir @@ -537,15 +538,38 @@ protected function runSuites($suites, $skippedSuites = []) protected function matchTestFromFilename($filename, $testsPath) { + $filter = ''; + if (strpos($filename, ':') !== false) { + if ((PHP_OS === 'Windows' || PHP_OS === 'WINNT') && $filename[1] === ':') { + // match C:\... + list($drive, $path, $filter) = explode(':', $filename, 3); + $filename = $drive . ':' . $path; + } else { + list($filename, $filter) = explode(':', $filename, 2); + } + + if ($filter) { + $filter = ':' . $filter; + } + } + $testsPath = str_replace(['//', '\/', '\\'], '/', $testsPath); $filename = str_replace(['//', '\/', '\\'], '/', $filename); + + if (rtrim($filename, '/') === $testsPath) { + //codecept run tests + return ['', '', $filter]; + } $res = preg_match("~^$testsPath/(.*?)(?>/(.*))?$~", $filename, $matches); if (!$res) { throw new \InvalidArgumentException("Test file can't be matched"); } if (!isset($matches[2])) { - $matches[2] = null; + $matches[2] = ''; + } + if ($filter) { + $matches[2] .= $filter; } return $matches; diff --git a/tests/cli/RunCest.php b/tests/cli/RunCest.php index ad99b0ff7d..e4bcc61fb3 100755 --- a/tests/cli/RunCest.php +++ b/tests/cli/RunCest.php @@ -24,6 +24,40 @@ public function runOneFileWithColors(\CliGuy $I) $I->seeInShellOutput("\033[35;1mFileExistsCept:\033[39;22m Check config exists"); } + /** + * https://github.com/Codeception/Codeception/issues/6103 + */ + public function runSuiteWhenNameMatchesExistingDirectory(\CliGuy $I) + { + $I->amInPath(codecept_data_dir('dir_matches_suite')); + $I->executeCommand('run api'); + $I->seeInShellOutput('SuccessCest'); + } + + public function runTestsDoesntFail(\CliGuy $I) + { + $I->amInPath(codecept_data_dir('dir_matches_suite')); + $I->executeCommand('run tests'); + $I->seeInShellOutput('SuccessCest'); + } + + public function runTestsWithFilterDoesntFail(\CliGuy $I) + { + $I->amInPath(codecept_data_dir('dir_matches_suite')); + $I->executeCommand('run tests:^success'); + $I->seeInShellOutput('SuccessCest'); + + $I->executeCommand('run tests/:^success'); + $I->seeInShellOutput('SuccessCest'); + } + + public function filterTestsWithoutSpecifyingSuite(\CliGuy $I) + { + $I->amInPath(codecept_data_dir('dir_matches_suite')); + $I->executeCommand('run :^success'); + $I->seeInShellOutput('SuccessCest'); + } + /** * @group reports * @group core diff --git a/tests/data/dir_matches_suite/api/.gitignore b/tests/data/dir_matches_suite/api/.gitignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/data/dir_matches_suite/codeception.yml b/tests/data/dir_matches_suite/codeception.yml new file mode 100644 index 0000000000..685a815595 --- /dev/null +++ b/tests/data/dir_matches_suite/codeception.yml @@ -0,0 +1,8 @@ +actor: Tester +paths: + tests: tests + log: tests/_output + data: tests/_data + support: tests/_support +settings: + memory_limit: 1024M diff --git a/tests/data/dir_matches_suite/tests/_output/.gitignore b/tests/data/dir_matches_suite/tests/_output/.gitignore new file mode 100644 index 0000000000..c96a04f008 --- /dev/null +++ b/tests/data/dir_matches_suite/tests/_output/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/data/dir_matches_suite/tests/_support/ApiTester.php b/tests/data/dir_matches_suite/tests/_support/ApiTester.php new file mode 100644 index 0000000000..a33632c6fa --- /dev/null +++ b/tests/data/dir_matches_suite/tests/_support/ApiTester.php @@ -0,0 +1,25 @@ +assertTrue(true); + } +}