Repository navigation
Expand file tree
/
Copy pathGroupManager.php
More file actions
170 lines (147 loc) · 5.8 KB
/
Copy pathGroupManager.php
File metadata and controls
170 lines (147 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
namespace Codeception\Lib;
use Codeception\Configuration;
use Codeception\Exception\ConfigurationException;
use Codeception\Test\Gherkin;
use Codeception\Test\Test;
use Codeception\Util\PathResolver;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use function realpath;
/**
* Loads information for groups from external sources (config, filesystem)
*/
class GroupManager
{
protected array $testsInGroups = [];
protected string $rootDir;
public function __construct(protected array $configuredGroups)
{
$this->rootDir = Configuration::baseDir();
$this->loadGroupsByPattern();
$this->loadConfiguredGroupSettings();
}
/**
* proceeds group names with asterisk:
*
* ```
* "tests/_log/g_*" => [
* "tests/_log/group_1",
* "tests/_log/group_2",
* "tests/_log/group_3",
* ]
* ```
*/
protected function loadGroupsByPattern(): void
{
foreach ($this->configuredGroups as $group => $pattern) {
if (!str_contains($group, '*')) {
continue;
}
$path = dirname($pattern);
if (!PathResolver::isPathAbsolute($pattern)) {
$path = $this->rootDir . $path;
}
$files = Finder::create()->files()
->name(basename($pattern))
->sortByName()
->in($path);
foreach ($files as $file) {
/** @var SplFileInfo $file * */
$prefix = str_replace('*', '', $group);
$pathPrefix = str_replace('*', '', basename($pattern));
$groupName = $prefix . str_replace($pathPrefix, '', $file->getRelativePathname());
$this->configuredGroups[$groupName] = dirname($pattern) . DIRECTORY_SEPARATOR . $file->getRelativePathname();
}
unset($this->configuredGroups[$group]);
}
}
protected function loadConfiguredGroupSettings(): void
{
foreach ($this->configuredGroups as $group => $tests) {
$this->testsInGroups[$group] = [];
if (is_array($tests)) {
foreach ($tests as $test) {
$file = str_replace(['/', '\\'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $test);
$this->testsInGroups[$group][] = $this->normalizeFilePath($file, $group);
}
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
// otherwise the current codeception directory becomes part of the group
// which causes every single test to run
if (trim($test) === '') {
continue;
}
$file = str_replace(['/', '\\'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], trim($test));
$this->testsInGroups[$group][] = $this->normalizeFilePath($file, $group);
}
fclose($handle);
}
}
}
}
private function normalizeFilePath(string $file, string $group): string
{
$pathParts = explode(':', $file);
if (codecept_is_path_absolute($file)) {
if ($file[0] === '/' && count($pathParts) > 1) {
//take segment before first :
$this->checkIfFileExists($pathParts[0], $group);
return sprintf('%s:%s', realpath($pathParts[0]), $pathParts[1]);
} elseif (count($pathParts) > 2) {
//on Windows take segment before second :
$fullPath = $pathParts[0] . ':' . $pathParts[1];
$this->checkIfFileExists($fullPath, $group);
return sprintf('%s:%s', realpath($fullPath), $pathParts[2]);
}
$this->checkIfFileExists($file, $group);
return realpath($file);
} elseif (!str_contains($file, ':')) {
$dirtyPath = $this->rootDir . $file;
$this->checkIfFileExists($dirtyPath, $group);
return realpath($dirtyPath);
}
$dirtyPath = $this->rootDir . $pathParts[0];
$this->checkIfFileExists($dirtyPath, $group);
return sprintf('%s:%s', realpath($dirtyPath), $pathParts[1]);
}
private function checkIfFileExists(string $path, string $group): void
{
if (!file_exists($path)) {
throw new ConfigurationException('GroupManager: File or directory ' . $path . ' set in ' . $group . ' group does not exist');
}
}
public function groupsForTest(Test $test): array
{
$filename = realpath($test->getFileName());
$testName = $test->getName();
$groups = $test->getMetadata()->getGroups();
foreach ($this->testsInGroups as $group => $tests) {
foreach ($tests as $testPattern) {
if ($filename == $testPattern) {
$groups[] = $group;
}
if (str_starts_with($filename . ':' . $testName, (string)$testPattern)) {
$groups[] = $group;
}
if (
$test instanceof Gherkin
&& mb_strtolower($filename . ':' . $test->getMetadata()->getFeature()) === mb_strtolower($testPattern)
) {
$groups[] = $group;
}
}
}
return array_unique($groups);
}
}