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

Fixes include group #6292

Merged
merged 5 commits into from Dec 16, 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
12 changes: 12 additions & 0 deletions src/Codeception/Command/Run.php
Expand Up @@ -3,6 +3,7 @@

use Codeception\Codecept;
use Codeception\Configuration;
use Codeception\Lib\GroupManager;
use Codeception\Util\PathResolver;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -489,9 +490,20 @@ protected function matchSingleTest($suite, $config)
*/
protected function runIncludedSuites($suites, $parent_dir)
{
$defaultConfig = Configuration::config();
$absolutePath = \Codeception\Configuration::projectDir();

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

if (!empty($defaultConfig['groups'])) {
$groups = array_map(function($g) use ($absolutePath) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 1 space after FUNCTION keyword; 0 found

return $absolutePath . $g;
}, $defaultConfig['groups']);
Configuration::append(['groups' => $groups]);
}

$suites = Configuration::suites();

$namespace = $this->currentNamespace();
Expand Down
22 changes: 22 additions & 0 deletions src/Codeception/Configuration.php
Expand Up @@ -32,6 +32,12 @@ class Configuration
*/
protected static $dir = null;

/**
* @var string Directory of a base configuration file for the project with includes.
* @see self::projectDir()
*/
protected static $baseDir = null;

/**
* @var string Current project output directory.
*/
Expand Down Expand Up @@ -158,6 +164,11 @@ public static function config($configFile = null)
$dir = realpath(dirname($configFile));
self::$dir = $dir;

// set the one default base directory for included setup
if (!self::$baseDir) {
self::$baseDir = $dir;
}

$configDistFile = $dir . DIRECTORY_SEPARATOR . 'codeception.dist.yml';

if (!(file_exists($configDistFile) || file_exists($configFile))) {
Expand Down Expand Up @@ -568,6 +579,17 @@ public static function projectDir()
return self::$dir . DIRECTORY_SEPARATOR;
}

/**
* Returns path to the base dir for config which consists with included setup
* Returns path to `codeception.yml` which was executed.
* If config doesn't have "include" section the result is the same as `projectDir()`
* @return string
*/
public static function baseDir()
{
return self::$baseDir . DIRECTORY_SEPARATOR;
}


/**
* Returns path to tests directory
Expand Down
30 changes: 25 additions & 5 deletions src/Codeception/Lib/GroupManager.php
Expand Up @@ -18,8 +18,12 @@ class GroupManager
protected $configuredGroups;
protected $testsInGroups = [];

protected $rootDir;


public function __construct(array $groups)
{
$this->rootDir = Configuration::baseDir();
$this->configuredGroups = $groups;
$this->loadGroupsByPattern();
$this->loadConfiguredGroupSettings();
Expand All @@ -42,12 +46,19 @@ protected function loadGroupsByPattern()
if (strpos($group, '*') === false) {
continue;
}
$path = dirname($pattern);
if (!\Codeception\Util\PathResolver::isPathAbsolute($pattern)) {
$path = $this->rootDir . $path;
}

$files = Finder::create()->files()
->name(basename($pattern))
->sortByName()
->in(Configuration::projectDir().dirname($pattern));
->in($path);

$i = 1;


foreach ($files as $file) {
/** @var SplFileInfo $file * */
$this->configuredGroups[str_replace('*', $i, $group)] = dirname($pattern).DIRECTORY_SEPARATOR.$file->getRelativePathname();
Expand All @@ -66,8 +77,15 @@ protected function loadConfiguredGroupSettings()
$file = str_replace(['/', '\\'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $test);
$this->testsInGroups[$group][] = $this->normalizeFilePath($file, $group);
}
} elseif (is_file(Configuration::projectDir() . $tests)) {
$handle = @fopen(Configuration::projectDir() . $tests, "r");
continue;
}

$path = $tests;
if (!codecept_is_path_absolute($tests)) {
$path = $this->rootDir . $tests;
}
if (is_file($path)) {
$handle = @fopen($path, "r");
if ($handle) {
while (($test = fgets($handle, 4096)) !== false) {
// if the current line is blank then we need to move to the next line
Expand Down Expand Up @@ -95,6 +113,8 @@ protected function loadConfiguredGroupSettings()
private function normalizeFilePath($file, $group)
{
$pathParts = explode(':', $file);


if (codecept_is_path_absolute($file)) {
if ($file[0] === '/' && count($pathParts) > 1) {
//take segment before first :
Expand All @@ -110,12 +130,12 @@ private function normalizeFilePath($file, $group)
$this->checkIfFileExists($file, $group);
return realpath($file);
} elseif (strpos($file, ':') === false) {
$dirtyPath = Configuration::projectDir() . $file;
$dirtyPath = $this->rootDir . $file;
$this->checkIfFileExists($dirtyPath, $group);
return realpath($dirtyPath);
}

$dirtyPath = Configuration::projectDir() . $pathParts[0];
$dirtyPath = $this->rootDir . $pathParts[0];
$this->checkIfFileExists($dirtyPath, $group);
return sprintf('%s:%s', realpath($dirtyPath), $pathParts[1]);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/cli/IncludedCest.php
Expand Up @@ -182,4 +182,15 @@ public function cleanIncluded(\CliGuy $I)
$I->seeInShellOutput('Done');
}

/**
* @before moveToIncluded
* @param CliGuy $I
*/
public function runIncludedGroup(\CliGuy $I)
{
$I->executeCommand("run -g group", false);
$I->dontSeeInShellOutput('No tests executed');
$I->seeInShellOutput('2 tests');
}

}
6 changes: 5 additions & 1 deletion tests/data/included/codeception.yml
Expand Up @@ -2,7 +2,11 @@ include:
- jazz
- jazz/pianist
- shire

groups:
group: group.txt

paths:
log: "_log"
settings:
colors: false
colors: false
1 change: 1 addition & 0 deletions tests/data/included/group.txt
@@ -0,0 +1 @@
jazz/tests/unit/SimpleTest.php
2 changes: 1 addition & 1 deletion tests/data/included/jazz/codeception.yml
Expand Up @@ -4,8 +4,8 @@ paths:
log: tests/_log
data: tests/_data
helpers: tests/_helpers
bootstrap: functional/_bootstrap.php
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
colors: true
memory_limit: 1024M
Expand Down
2 changes: 1 addition & 1 deletion tests/data/included/jazz/pianist/codeception.yml
Expand Up @@ -4,8 +4,8 @@ paths:
log: tests/_log
data: tests/_data
helpers: tests/_helpers
bootstrap: functional/_bootstrap.php
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
colors: true
memory_limit: 1024M
Expand Down
2 changes: 1 addition & 1 deletion tests/data/included/shire/codeception.yml
Expand Up @@ -4,8 +4,8 @@ paths:
log: tests/_log
data: tests/_data
helpers: tests/_helpers
bootstrap: functional/_bootstrap.php
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
colors: true
memory_limit: 1024M
Expand Down