Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix run suite when suite name matches directory #6105

Merged
merged 6 commits into from Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/Codeception/Command/Run.php
Expand Up @@ -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] <info>" . $userOptions['seed'] . "</info>"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions tests/cli/RunCest.php
Expand Up @@ -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
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions tests/data/dir_matches_suite/tests/_output/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
25 changes: 25 additions & 0 deletions tests/data/dir_matches_suite/tests/_support/ApiTester.php
@@ -0,0 +1,25 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
*
* @SuppressWarnings(PHPMD)
*/
class ApiTester extends \Codeception\Actor
{
use _generated\ApiTesterActions;

/**
* Define custom actions here
*/
}
@@ -0,0 +1,2 @@
*
!.gitignore
4 changes: 4 additions & 0 deletions tests/data/dir_matches_suite/tests/api.suite.yml
@@ -0,0 +1,4 @@
class_name: ApiTester
modules:
enabled:
- Asserts
11 changes: 11 additions & 0 deletions tests/data/dir_matches_suite/tests/api/SuccessCest.php
@@ -0,0 +1,11 @@
<?php


class SuccessCest
{

public function successful(ApiTester $I)
{
$I->assertTrue(true);
}
}