Skip to content

Commit

Permalink
ensure that directly running root level suites
Browse files Browse the repository at this point in the history
does not run included app suites.
  • Loading branch information
calvinalkan committed May 21, 2022
1 parent 30b102e commit 411f341
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/Codeception/Command/Run.php
Expand Up @@ -328,7 +328,7 @@ public function execute(InputInterface $input, OutputInterface $output)

$suite = $input->getArgument('suite');
$test = $input->getArgument('test');

if ($this->options['group']) {
$this->output->writeln(sprintf("[Groups] <info>%s</info> ", implode(', ', $this->options['group'])));
}
Expand All @@ -346,7 +346,6 @@ public function execute(InputInterface $input, OutputInterface $output)
foreach ($config['include'] as $include) {
// Find if the suite begins with an include path
if (strpos($suite, $include) === 0) {

// Use include config
$config = Configuration::config($projectDir.$include);

Expand Down Expand Up @@ -384,7 +383,7 @@ public function execute(InputInterface $input, OutputInterface $output)
}
}
}

if ($test) {
$userOptions['filter'] = $this->matchFilteredTestName($test);
} elseif (
Expand All @@ -408,10 +407,13 @@ public function execute(InputInterface $input, OutputInterface $output)

// Run all tests of given suite or all suites
if (!$test) {
$rawSuites = $suite ? explode(',', $suite) : Configuration::suites();

/** @var string[] $mainAppSuite */
$mainAppSuite = [];
$didPassCliSuite = !empty($suite);

$rawSuites = $didPassCliSuite ? explode(',', $suite) : Configuration::suites();

/** @var string[] $mainAppSuites */
$mainAppSuites = [];

/** @var array<string,string> $appSpecificSuites */
$appSpecificSuites = [];
Expand All @@ -429,19 +431,22 @@ public function execute(InputInterface $input, OutputInterface $output)
$appSpecificSuites[$appAndSuite[0]][] = $appAndSuite[1];
continue;
}
$mainAppSuite[] = $rawSuite;
$mainAppSuites[] = $rawSuite;
}

if([] !== $mainAppSuite) {
$this->executed = $this->runSuites($mainAppSuite, $this->options['skip']);
if([] !== $mainAppSuites) {
$this->executed = $this->runSuites($mainAppSuites, $this->options['skip']);
}

if(!empty($wildcardSuites) && ! empty($appSpecificSuites)) {
$this->output->writeLn('<error>Wildcard options can not be combined with specific suites of included apps.</error>');
return 2;
}

if(!empty($config['include'])) {
if(
!empty($config['include'])
&& (!$didPassCliSuite || !empty($wildcardSuites) || !empty($appSpecificSuites))
) {

$currentDir = Configuration::projectDir();
$includedApps = $config['include'];
Expand Down Expand Up @@ -542,10 +547,10 @@ protected function runIncludedSuites($suites, $parent_dir, $filterAppSuites = []
$absolutePath = \Codeception\Configuration::projectDir();

foreach ($suites as $relativePath) {
$current_dir = rtrim($parent_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$relativePath;
$current_dir = rtrim($parent_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relativePath;
$config = Configuration::config($current_dir);

if (!empty($defaultConfig['groups'])) {
if ( !empty($defaultConfig['groups'])) {
$groups = array_map(function($g) use ($absolutePath) {
return $absolutePath . $g;
}, $defaultConfig['groups']);
Expand All @@ -554,11 +559,11 @@ protected function runIncludedSuites($suites, $parent_dir, $filterAppSuites = []

$suites = Configuration::suites();

if(!empty($filterSuitesByWildcard)){
if( !empty($filterSuitesByWildcard)){
$suites = array_intersect($suites, $filterSuitesByWildcard);
}

if(isset($filterAppSuites[$relativePath])) {
if( isset($filterAppSuites[$relativePath])) {
$suites = array_intersect($suites, $filterAppSuites[$relativePath]);
}

Expand Down Expand Up @@ -737,4 +742,13 @@ private function isSuiteInMultiApplication($suite_name)
return false !== strpos($suite_name, '::');
}

/**
* @param string $suite_name
*
* @return bool
*/
private function isRootLevelSuite($suite_name) {
return !$this->isSuiteInMultiApplication($suite_name) && ! $this->isWildcardSuiteName($suite_name);
}

}
67 changes: 67 additions & 0 deletions tests/cli/IncludedCest.php
Expand Up @@ -279,4 +279,71 @@ public function wildCardSuitesAndAppSpecificSuitesCantBeCombined(CliGuy $I)
$I->seeInShellOutput('Wildcard options can not be combined with specific suites of included apps.');
}

/**
* @before moveToIncluded
* @param CliGuy $I
*/
public function runningASuiteInTheRootApplicationDoesNotRunTheIncludedAppSuites(CliGuy $I)
{
$I->executeCommand('run unit');

$I->seeInShellOutput('Unit Tests (1)');
$I->seeInShellOutput('RootApplicationUnitTest:');

$I->dontSeeInShellOutput('Functional Tests (1)');
$I->dontSeeInShellOutput('RootApplicationFunctionalTest:');
$I->dontSeeInShellOutput('Jazz.functional Tests');
$I->dontSeeInShellOutput('Jazz.unit Tests');

$I->executeCommand('run functional');

$I->seeInShellOutput('Functional Tests (1)');
$I->seeInShellOutput('RootApplicationFunctionalTest:');

$I->dontSeeInShellOutput('Unit Tests (1)');
$I->dontSeeInShellOutput('RootApplicationUnitTest:');
$I->dontSeeInShellOutput('Jazz.functional Tests');
$I->dontSeeInShellOutput('Jazz.unit Tests');
}

/**
* @before moveToIncluded
* @param CliGuy $I
*/
public function rootSuitesCanBeRunInCombinationWithIncludedSuites(CliGuy $I)
{
$I->executeCommand('run unit,*::unit');

// root level
$I->seeInShellOutput('Unit Tests (1)');
$I->seeInShellOutput('RootApplicationUnitTest:');
$I->dontSeeInShellOutput('Functional Tests (1)');
$I->dontSeeInShellOutput('RootApplicationFunctionalTest:');

// included
$I->seeInShellOutput('Jazz.unit Tests');
$I->dontSeeInShellOutput('Jazz.functional Tests');
$I->dontSeeInShellOutput('Jazz\Pianist.functional');
$I->dontSeeInShellOutput('Shire.functional Tests');

// Ensure that root level suites are not run twice.
$I->seeInShellOutput('OK (3 tests, 3 assertions)');


$I->executeCommand('run unit,jazz::functional');

// root level
$I->seeInShellOutput('Unit Tests (1)');
$I->seeInShellOutput('RootApplicationUnitTest:');
$I->dontSeeInShellOutput('Functional Tests (1)');
$I->dontSeeInShellOutput('RootApplicationFunctionalTest:');

// included apps
$I->seeInShellOutput('Jazz.functional Tests');
$I->dontSeeInShellOutput('Jazz.unit Tests');
$I->dontSeeInShellOutput('Jazz\Pianist.functional');
$I->dontSeeInShellOutput('Shire.functional Tests');

}

}
5 changes: 5 additions & 0 deletions tests/data/included/codeception.yml
Expand Up @@ -3,10 +3,15 @@ include:
- jazz/pianist
- shire



groups:
group: group.txt

paths:
tests: tests
data: tests/_data
support: tests/_support
log: "_log"
settings:
colors: false
1 change: 1 addition & 0 deletions tests/data/included/tests/functional.suite.yml
@@ -0,0 +1 @@
##
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace data\included\tests\functional;

use Codeception\Test\Unit;

/**
* This is not a Cept test case because it does not matter for what we are testing.
*/
class RootApplicationFunctionalTest extends Unit
{

public function testBarEqualsBar()
{
$this->assertSame('bar', 'bar');
}
}
1 change: 1 addition & 0 deletions tests/data/included/tests/unit.suite.yml
@@ -0,0 +1 @@
##
15 changes: 15 additions & 0 deletions tests/data/included/tests/unit/RootApplicationUnitTest.php
@@ -0,0 +1,15 @@
<?php

namespace data\included\tests\unit;

use Codeception\Test\Unit;

class RootApplicationUnitTest extends Unit
{

public function testFooEqualsFoo()
{
$this->assertSame('foo', 'foo');
}

}

0 comments on commit 411f341

Please sign in to comment.