-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPhpUnitDrupalModulesTask.php
More file actions
194 lines (161 loc) · 5.07 KB
/
PhpUnitDrupalModulesTask.php
File metadata and controls
194 lines (161 loc) · 5.07 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
namespace Wunderio\GrumPHP\Task\PhpUnitDrupalModules;
use GrumPHP\Collection\ProcessArgumentsCollection;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
use Wunderio\GrumPHP\Task\AbstractMultiPathProcessingTask;
/**
* Class PhpUnitDrupalModulesTask.
*
* Runs phpunit only for affected Drupal custom modules.
*/
class PhpUnitDrupalModulesTask extends AbstractMultiPathProcessingTask {
/**
* {@inheritdoc}
*/
public function run(ContextInterface $context): TaskResultInterface {
$config = $this->getConfig()->getOptions();
$paths = $this->getPathsOrResult($context, $config, $this);
if ($paths instanceof TaskResultInterface) {
return $paths;
}
$moduleRoots = $this->getModuleRoots($config);
$modules = $this->collectModulesFromPaths($paths, $moduleRoots);
[$modulesWithTests, $modulesWithoutTests] = $this->splitModulesByTests($modules);
$this->printModulesWithoutTests($modulesWithoutTests);
if (!$modulesWithTests) {
return TaskResult::createSkipped($this, $context);
}
$this->printModulesWithTests($modulesWithTests);
$process = $this->processBuilder->buildProcess($this->buildArguments($modulesWithTests));
$process->run();
return $this->getTaskResult($process, $context);
}
/**
* Determine which directory roots should be treated as Drupal module roots.
*
* Defaults to web/modules/custom for backward compatibility, but can be
* configured via the run_on option in tasks.yml.
*
* @param array $config
* Task configuration options.
*
* @return string[]
* Normalised module root paths.
*/
private function getModuleRoots(array $config): array {
$moduleRoots = $config['run_on'] ?? ['web/modules/custom'];
$normalisedRoots = [];
foreach ($moduleRoots as $root) {
$normalisedRoots[] = rtrim((string) $root, '/');
}
return array_values($normalisedRoots);
}
/**
* Collect all affected modules from the changed file paths.
*
* @param iterable $paths
* Changed paths.
* @param string[] $moduleRoots
* Module root directories.
*
* @return string[]
* Module paths keyed by path for uniqueness.
*/
private function collectModulesFromPaths(iterable $paths, array $moduleRoots): array {
$modules = [];
foreach ($paths as $file) {
$path = (string) $file;
foreach ($moduleRoots as $root) {
$rootWithSlash = $root . '/';
if (!str_starts_with($path, $rootWithSlash)) {
continue;
}
if (preg_match('#^(' . preg_quote($root, '#') . '/[^/]+)#', $path, $matches)) {
$modules[$matches[1]] = $matches[1];
}
}
}
return $modules;
}
/**
* Split modules into ones with and without tests directories.
*
* @param string[] $modules
* All affected modules.
*
* @return array{0: string[], 1: string[]}
* First array contains modules with tests, second without.
*/
private function splitModulesByTests(array $modules): array {
$modulesWithTests = [];
foreach ($modules as $modulePath) {
if (is_dir($modulePath . '/tests')) {
$modulesWithTests[] = $modulePath;
}
}
$modulesWithoutTests = array_values(array_diff($modules, $modulesWithTests));
return [$modulesWithTests, $modulesWithoutTests];
}
/**
* Print a note about affected modules that do not have tests.
*
* @param string[] $modulesWithoutTests
* Affected modules without tests.
*/
private function printModulesWithoutTests(array $modulesWithoutTests): void {
if (!$modulesWithoutTests) {
return;
}
$lines = [];
foreach ($modulesWithoutTests as $modulePath) {
$lines[] = ' - ' . $modulePath;
}
fwrite(
STDOUT,
"\nphpunit_drupal_modules: NOTE: affected modules without tests:\n" .
implode("\n", $lines) .
"\n\n"
);
}
/**
* Print a list of modules for which tests will be executed.
*
* @param string[] $modulesWithTests
* Affected modules with tests.
*/
private function printModulesWithTests(array $modulesWithTests): void {
$lines = [];
foreach ($modulesWithTests as $modulePath) {
$lines[] = ' - ' . $modulePath;
}
fwrite(
STDOUT,
"phpunit_drupal_modules: running tests for modules:\n" .
implode("\n", $lines) .
"\n\n"
);
}
/**
* {@inheritdoc}
*/
public function buildArguments(iterable $modules): ProcessArgumentsCollection {
$config = $this->getConfig()->getOptions();
$arguments = $this->processBuilder->createArgumentsForCommand('phpunit');
if (!empty($config['config_file'])) {
// Mirror GrumPHP's core phpunit task: allow passing a custom config file.
$arguments->add('-c');
$arguments->add($config['config_file']);
}
if (!empty($config['testsuite'])) {
$arguments->add('--testsuite');
$arguments->add($config['testsuite']);
}
foreach ($modules as $modulePath) {
$arguments->add($modulePath);
}
return $arguments;
}
}