Skip to content

Commit b171a7c

Browse files
authored
Merge pull request Codeception#6105 from Codeception/4.1-6103
Fix `run suite` when suite name matches directory
2 parents daf4fe1 + 5d0edff commit b171a7c

File tree

9 files changed

+115
-5
lines changed

9 files changed

+115
-5
lines changed

src/Codeception/Command/Run.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,10 @@ public function execute(InputInterface $input, OutputInterface $output)
385385
}
386386

387387
if ($test) {
388-
$filter = $this->matchFilteredTestName($test);
389-
$userOptions['filter'] = $filter;
388+
$userOptions['filter'] = $this->matchFilteredTestName($test);
389+
} elseif ($suite) {
390+
$userOptions['filter'] = $this->matchFilteredTestName($suite);
390391
}
391-
392392
if (!$this->options['silent'] && $config['settings']['shuffle']) {
393393
$this->output->writeln(
394394
"[Seed] <info>" . $userOptions['seed'] . "</info>"
@@ -470,7 +470,8 @@ protected function matchSingleTest($suite, $config)
470470
list($file) = explode(':', $suite, -1);
471471
}
472472
$realPath = $cwd . DIRECTORY_SEPARATOR . $file;
473-
if (file_exists($realPath) || is_dir($realPath)) {
473+
if (file_exists($realPath) && strpos($realPath, $realTestDir) === 0) {
474+
//only match test if file is in tests directory
474475
return $this->matchTestFromFilename(
475476
$cwd . DIRECTORY_SEPARATOR . $suite,
476477
$realTestDir
@@ -537,15 +538,38 @@ protected function runSuites($suites, $skippedSuites = [])
537538

538539
protected function matchTestFromFilename($filename, $testsPath)
539540
{
541+
$filter = '';
542+
if (strpos($filename, ':') !== false) {
543+
if ((PHP_OS === 'Windows' || PHP_OS === 'WINNT') && $filename[1] === ':') {
544+
// match C:\...
545+
list($drive, $path, $filter) = explode(':', $filename, 3);
546+
$filename = $drive . ':' . $path;
547+
} else {
548+
list($filename, $filter) = explode(':', $filename, 2);
549+
}
550+
551+
if ($filter) {
552+
$filter = ':' . $filter;
553+
}
554+
}
555+
540556
$testsPath = str_replace(['//', '\/', '\\'], '/', $testsPath);
541557
$filename = str_replace(['//', '\/', '\\'], '/', $filename);
558+
559+
if (rtrim($filename, '/') === $testsPath) {
560+
//codecept run tests
561+
return ['', '', $filter];
562+
}
542563
$res = preg_match("~^$testsPath/(.*?)(?>/(.*))?$~", $filename, $matches);
543564

544565
if (!$res) {
545566
throw new \InvalidArgumentException("Test file can't be matched");
546567
}
547568
if (!isset($matches[2])) {
548-
$matches[2] = null;
569+
$matches[2] = '';
570+
}
571+
if ($filter) {
572+
$matches[2] .= $filter;
549573
}
550574

551575
return $matches;

tests/cli/RunCest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,40 @@ public function runOneFileWithColors(\CliGuy $I)
2424
$I->seeInShellOutput("\033[35;1mFileExistsCept:\033[39;22m Check config exists");
2525
}
2626

27+
/**
28+
* https://github.com/Codeception/Codeception/issues/6103
29+
*/
30+
public function runSuiteWhenNameMatchesExistingDirectory(\CliGuy $I)
31+
{
32+
$I->amInPath(codecept_data_dir('dir_matches_suite'));
33+
$I->executeCommand('run api');
34+
$I->seeInShellOutput('SuccessCest');
35+
}
36+
37+
public function runTestsDoesntFail(\CliGuy $I)
38+
{
39+
$I->amInPath(codecept_data_dir('dir_matches_suite'));
40+
$I->executeCommand('run tests');
41+
$I->seeInShellOutput('SuccessCest');
42+
}
43+
44+
public function runTestsWithFilterDoesntFail(\CliGuy $I)
45+
{
46+
$I->amInPath(codecept_data_dir('dir_matches_suite'));
47+
$I->executeCommand('run tests:^success');
48+
$I->seeInShellOutput('SuccessCest');
49+
50+
$I->executeCommand('run tests/:^success');
51+
$I->seeInShellOutput('SuccessCest');
52+
}
53+
54+
public function filterTestsWithoutSpecifyingSuite(\CliGuy $I)
55+
{
56+
$I->amInPath(codecept_data_dir('dir_matches_suite'));
57+
$I->executeCommand('run :^success');
58+
$I->seeInShellOutput('SuccessCest');
59+
}
60+
2761
/**
2862
* @group reports
2963
* @group core

tests/data/dir_matches_suite/api/.gitignore

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
support: tests/_support
7+
settings:
8+
memory_limit: 1024M
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
*
16+
* @SuppressWarnings(PHPMD)
17+
*/
18+
class ApiTester extends \Codeception\Actor
19+
{
20+
use _generated\ApiTesterActions;
21+
22+
/**
23+
* Define custom actions here
24+
*/
25+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class_name: ApiTester
2+
modules:
3+
enabled:
4+
- Asserts
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
4+
class SuccessCest
5+
{
6+
7+
public function successful(ApiTester $I)
8+
{
9+
$I->assertTrue(true);
10+
}
11+
}

0 commit comments

Comments
 (0)