-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathTestDependencyCheck.php
305 lines (268 loc) · 10.7 KB
/
TestDependencyCheck.php
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\StaticCheck;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Finder\Finder;
use Exception;
use Magento\FunctionalTestingFramework\Util\Script\ScriptUtil;
use Magento\FunctionalTestingFramework\Util\Script\TestDependencyUtil;
/**
* Class TestDependencyCheck
* @package Magento\FunctionalTestingFramework\StaticCheck
*/
class TestDependencyCheck implements StaticCheckInterface
{
const EXTENDS_REGEX_PATTERN = '/extends=["\']([^\'"]*)/';
const ACTIONGROUP_REGEX_PATTERN = '/ref=["\']([^\'"]*)/';
const ERROR_LOG_FILENAME = 'mftf-dependency-checks-errors';
const ERROR_LOG_MESSAGE = 'MFTF File Dependency Check';
const WARNING_LOG_FILENAME = 'mftf-dependency-checks-warnings';
const ALLOW_LIST_FILENAME = 'test-dependency-allowlist';
/**
* Array of FullModuleName => [dependencies], including flattened dependency tree
* @var array
*/
private $flattenedDependencies;
/**
* Array of FullModuleName => PathToModule
* @var array
*/
private $moduleNameToPath;
/**
* Array of FullModuleName => ComposerModuleName
* @var array
*/
private $moduleNameToComposerName;
/**
* Array containing all errors found after running the execute() function.
* @var array
*/
private $errors = [];
/**
* Array containing all warnings found after running the execute() function.
* @var array
*/
private $warnings = [];
/**
* Array containing warnings found while iterating through files
* @var array
*/
private $tempWarnings = [];
/**
* String representing the output summary found after running the execute() function.
* @var string
*/
private $output;
/**
* Array containing all entities after resolving references.
* @var array
*/
private $allEntities = [];
/**
* ScriptUtil instance
*
* @var ScriptUtil
*/
private $scriptUtil;
/**
* @var TestDependencyUtil
*/
private $testDependencyUtil;
/**
* @var array $allowFailureEntities
*/
private $allowFailureEntities = [];
/**
* Checks test dependencies, determined by references in tests versus the dependencies listed in the Magento module
*
* @param InputInterface $input
* @return void
* @throws Exception
*/
public function execute(InputInterface $input)
{
$this->scriptUtil = new ScriptUtil();
$this->testDependencyUtil = new TestDependencyUtil();
$allModules = $this->scriptUtil->getAllModulePaths();
if (!class_exists('\Magento\Framework\Component\ComponentRegistrar')) {
throw new TestFrameworkException(
"TEST DEPENDENCY CHECK ABORTED: MFTF must be attached or pointing to Magento codebase."
);
}
// Build array of entities found in allow-list files
// Expect one entity per file line, no commas or anything else
foreach ($allModules as $modulePath) {
if (file_exists($modulePath . DIRECTORY_SEPARATOR . self::ALLOW_LIST_FILENAME)) {
$contents = file_get_contents($modulePath . DIRECTORY_SEPARATOR . self::ALLOW_LIST_FILENAME);
foreach (explode("\n", $contents) as $entity) {
$this->allowFailureEntities[$entity] = true;
}
}
}
$registrar = new \Magento\Framework\Component\ComponentRegistrar();
$this->moduleNameToPath = $registrar->getPaths(\Magento\Framework\Component\ComponentRegistrar::MODULE);
$this->moduleNameToComposerName = $this->testDependencyUtil->buildModuleNameToComposerName(
$this->moduleNameToPath
);
$this->flattenedDependencies = $this->testDependencyUtil->buildComposerDependencyList(
$this->moduleNameToPath,
$this->moduleNameToComposerName
);
$filePaths = [
DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR . 'ActionGroup' . DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR,
];
// These files can contain references to other modules.
$testXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($allModules, $filePaths[0]);
$actionGroupXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($allModules, $filePaths[1]);
$dataXmlFiles= $this->scriptUtil->getModuleXmlFilesByScope($allModules, $filePaths[2]);
$this->errors = [];
$this->errors += $this->findErrorsInFileSet($testXmlFiles);
$this->errors += $this->findErrorsInFileSet($actionGroupXmlFiles);
$this->errors += $this->findErrorsInFileSet($dataXmlFiles);
// hold on to the output and print any errors to a file
$this->output = $this->scriptUtil->printErrorsToFile(
$this->errors,
StaticChecksList::getErrorFilesPath() . DIRECTORY_SEPARATOR . self::ERROR_LOG_FILENAME . '.txt',
self::ERROR_LOG_MESSAGE
);
if (!empty($this->warnings)) {
$this->output .= "\n " . $this->scriptUtil->printWarningsToFile(
$this->warnings,
StaticChecksList::getErrorFilesPath() . DIRECTORY_SEPARATOR . self::WARNING_LOG_FILENAME . '.txt',
self::ERROR_LOG_MESSAGE
);
}
}
/**
* Return array containing all errors found after running the execute() function.
* @return array
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* Return string of a short human readable result of the check. For example: "No Dependency errors found."
* @return string
*/
public function getOutput(): string
{
return $this->output??"";
}
/**
* Finds all reference errors in given set of files
* @param Finder $files
* @return array
* @throws XmlException
*/
private function findErrorsInFileSet(Finder $files): array
{
$testErrors = [];
foreach ($files as $filePath) {
$this->allEntities = [];
$moduleName = $this->testDependencyUtil->getModuleName($filePath, $this->moduleNameToPath);
// Not a module, is either dev/tests/acceptance or loose folder with test materials
if ($moduleName === null) {
continue;
}
$contents = file_get_contents($filePath);
preg_match_all(ActionObject::ACTION_ATTRIBUTE_VARIABLE_REGEX_PATTERN, $contents, $braceReferences);
preg_match_all(self::ACTIONGROUP_REGEX_PATTERN, $contents, $actionGroupReferences);
preg_match_all(self::EXTENDS_REGEX_PATTERN, $contents, $extendReferences);
// Remove Duplicates
$braceReferences[0] = array_unique($braceReferences[0]);
$actionGroupReferences[1] = array_unique($actionGroupReferences[1]);
$braceReferences[1] = array_unique($braceReferences[1]);
$braceReferences[2] = array_filter(array_unique($braceReferences[2]));
// resolve entity references
$this->allEntities = array_merge(
$this->allEntities,
$this->scriptUtil->resolveEntityReferences($braceReferences[0], $contents)
);
// resolve parameterized references
$this->allEntities = array_merge(
$this->allEntities,
$this->scriptUtil->resolveParametrizedReferences($braceReferences[2], $contents)
);
// resolve entity by names
$this->allEntities = array_merge(
$this->allEntities,
$this->scriptUtil->resolveEntityByNames($actionGroupReferences[1])
);
// resolve entity by names
$this->allEntities = array_merge(
$this->allEntities,
$this->scriptUtil->resolveEntityByNames($extendReferences[1])
);
// Find violating references and set error output
$violatingReferences = $this->findViolatingReferences($moduleName);
$testErrors = array_merge($testErrors, $this->setErrorOutput($violatingReferences, $filePath));
$this->warnings = array_merge($this->warnings, $this->setErrorOutput($this->tempWarnings, $filePath));
}
return $testErrors;
}
/**
* Find violating references
*
* @param string $moduleName
* @return array
*/
private function findViolatingReferences(string $moduleName): array
{
// Find Violations
$violatingReferences = [];
$currentModule = $this->moduleNameToComposerName[$moduleName];
$modulesReferencedInTest = $this->testDependencyUtil->getModuleDependenciesFromReferences(
$this->allEntities,
$this->moduleNameToComposerName,
$this->moduleNameToPath
);
$moduleDependencies = $this->flattenedDependencies[$moduleName];
foreach ($modulesReferencedInTest as $entityName => $files) {
$isInAllowList = array_key_exists($entityName, $this->allowFailureEntities);
$valid = false;
foreach ($files as $module) {
if (array_key_exists($module, $moduleDependencies) || $module === $currentModule) {
$valid = true;
break;
}
}
if (!$valid) {
if ($isInAllowList) {
$this->tempWarnings[$entityName] = $files;
continue;
}
$violatingReferences[$entityName] = $files;
}
}
return $violatingReferences;
}
/**
* Builds and returns error output for violating references
*
* @param array $violatingReferences
* @return array
*/
private function setErrorOutput(array $violatingReferences, $path): array
{
$testErrors = [];
if (!empty($violatingReferences)) {
// Build error output
$errorOutput = "\nFile \"{$path->getRealPath()}\"";
$errorOutput .= "\ncontains entity references that violate dependency constraints:\n\t\t";
foreach ($violatingReferences as $entityName => $files) {
$errorOutput .= "\n\t {$entityName} from module(s): " . implode(", ", $files);
}
$testErrors[$path->getRealPath()][] = $errorOutput;
}
return $testErrors;
}
}