Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 83 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
ComposerLaunchParallel | |
0.00% |
0 / 80 |
|
0.00% |
0 / 10 |
552 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
isDatabaseRun | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
start | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
prepareEnvironment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
runTestSuite | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
extractArgs | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
launchTests | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
launchTestsCustomGroups | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
launchTestsDatabase | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
launchTestsDatabaseless | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace MediaWiki\Composer; |
6 | |
7 | use Composer\Script\Event; |
8 | use MediaWiki\Composer\PhpUnitSplitter\PhpUnitXml; |
9 | use MediaWiki\Maintenance\ForkController; |
10 | use Shellbox\Shellbox; |
11 | |
12 | $basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/../..'; |
13 | |
14 | require_once $basePath . '/includes/BootstrapHelperFunctions.php'; |
15 | require_once $basePath . '/includes/Maintenance/ForkController.php'; |
16 | |
17 | /** |
18 | * Launch PHPUnit test suites in parallel. |
19 | * |
20 | * This class is run directly from composer.json, |
21 | * outside of any MediaWiki context; |
22 | * consequently, most MediaWiki code cannot be used here. |
23 | * We extend ForkController because it's convenient to do so and ForkController still works here, |
24 | * but we cannot use e.g. Shell::command() to run the composer sub-commands, |
25 | * nor anything else that requires MediaWiki services or config. |
26 | * (But we can use the underlying Shellbox library directly.) |
27 | * |
28 | * @license GPL-2.0-or-later |
29 | */ |
30 | class ComposerLaunchParallel extends ForkController { |
31 | |
32 | private const ALWAYS_EXCLUDE = [ 'Broken', 'ParserFuzz', 'Stub' ]; |
33 | private array $groups = []; |
34 | private array $excludeGroups = []; |
35 | |
36 | public function __construct( |
37 | array $groups, |
38 | array $excludeGroups |
39 | ) { |
40 | $this->groups = $groups; |
41 | $this->excludeGroups = $excludeGroups; |
42 | /** |
43 | * By default, the splitting process splits the tests into 8 groups. 7 of the groups are composed |
44 | * of evenly distributed test classes extracted from the `--list-tests-xml` phpunit function. The |
45 | * 8th group contains just the ExtensionsParserTestSuite. |
46 | */ |
47 | $splitGroupCount = 7; |
48 | if ( $this->isDatabaseRun() ) { |
49 | /** |
50 | * In the splitting, we put ExtensionsParserTestSuite in `split_group_7` on its own. We only |
51 | * need to run `split_group_7` when we run Database tests, since all Parser tests use the |
52 | * database. Running `split_group_7` when no matches tests get executed results in a phpunit |
53 | * error code. |
54 | */ |
55 | $splitGroupCount = 8; |
56 | } |
57 | parent::__construct( $splitGroupCount ); |
58 | } |
59 | |
60 | private function isDatabaseRun(): bool { |
61 | return in_array( 'Database', $this->groups ) && |
62 | !in_array( 'Database', $this->excludeGroups ); |
63 | } |
64 | |
65 | /** |
66 | * @inheritDoc |
67 | */ |
68 | public function start(): string { |
69 | $status = parent::start(); |
70 | if ( $status === 'child' ) { |
71 | $this->runTestSuite( $this->getChildNumber() ); |
72 | } |
73 | return $status; |
74 | } |
75 | |
76 | protected function prepareEnvironment() { |
77 | // Skip parent class method to avoid errors: |
78 | // this script does not run inside MediaWiki, so there is no environment to prepare |
79 | } |
80 | |
81 | private function runTestSuite( int $groupId ) { |
82 | $executor = Shellbox::createUnboxedExecutor(); |
83 | $command = $executor->createCommand() |
84 | ->params( |
85 | 'composer', 'run', |
86 | '--timeout=0', |
87 | 'phpunit:entrypoint', |
88 | '--', |
89 | '--testsuite', "split_group_$groupId", |
90 | '--exclude-group', implode( ",", array_diff( $this->excludeGroups, $this->groups ) ) |
91 | ); |
92 | if ( count( $this->groups ) ) { |
93 | $command->params( '--group', implode( ',', $this->groups ) ); |
94 | } |
95 | $groupName = $this->isDatabaseRun() ? "database" : "databaseless"; |
96 | $command->params( |
97 | "--cache-result-file=.phpunit_group_{$groupId}_{$groupName}.result.cache" |
98 | ); |
99 | $command->includeStderr( true ); |
100 | print( "Running command '" . $command->getCommandString() . "' ..." . PHP_EOL ); |
101 | $result = $command->execute(); |
102 | print( $result->getStdout() ); |
103 | exit( $result->getExitCode() ); |
104 | } |
105 | |
106 | private static function extractArgs(): array { |
107 | $options = []; |
108 | foreach ( [ "group", "exclude-group" ] as $argument ) { |
109 | $groupIndex = array_search( "--" . $argument, $_SERVER['argv'] ); |
110 | if ( $groupIndex > 0 ) { |
111 | if ( count( $_SERVER['argv'] ) > $groupIndex + 1 ) { |
112 | $nextArg = $_SERVER['argv'][$groupIndex + 1]; |
113 | if ( strpos( $nextArg, "--" ) === 0 ) { |
114 | throw new \InvalidArgumentException( |
115 | "parameter " . $argument . " takes a variable - none supplied" |
116 | ); |
117 | } |
118 | $options[$argument] = $nextArg; |
119 | } else { |
120 | throw new \InvalidArgumentException( |
121 | "parameter " . $argument . " takes a variable - not enough arguments supplied" |
122 | ); |
123 | } |
124 | } |
125 | } |
126 | return $options; |
127 | } |
128 | |
129 | public static function launchTests( Event $event, array $groups, array $excludeGroups ): void { |
130 | $phpUnitConfig = getcwd() . DIRECTORY_SEPARATOR . 'phpunit.xml'; |
131 | if ( !PhpUnitXml::isPhpUnitXmlPrepared( $phpUnitConfig ) ) { |
132 | $event->getIO()->error( "phpunit.xml is not present or does not contain split test suites" ); |
133 | $event->getIO()->error( "run `composer phpunit:prepare-parallel:...` to generate the split suites" ); |
134 | exit( 1 ); |
135 | } |
136 | $event->getIO()->info( "Running 'split_group_X' suites in parallel..." ); |
137 | $launcher = new ComposerLaunchParallel( $groups, $excludeGroups ); |
138 | $launcher->start(); |
139 | if ( $launcher->allSuccessful() ) { |
140 | $event->getIO()->info( "All split_groups succeeded!" ); |
141 | exit( 0 ); |
142 | } else { |
143 | $event->getIO()->warning( "Some split_groups failed - returning failure status" ); |
144 | exit( 1 ); |
145 | } |
146 | } |
147 | |
148 | public static function launchTestsCustomGroups( Event $event ) { |
149 | $options = self::extractArgs(); |
150 | if ( array_key_exists( 'exclude-group', $options ) ) { |
151 | $excludeGroups = explode( ',', $options['exclude-group'] ); |
152 | } else { |
153 | $excludeGroups = [ 'Broken', 'ParserFuzz', 'Stub', 'Standalone', 'Database' ]; |
154 | } |
155 | if ( array_key_exists( 'group', $options ) ) { |
156 | $groups = explode( ',', $options['group'] ); |
157 | } else { |
158 | $groups = []; |
159 | } |
160 | self::launchTests( $event, $groups, $excludeGroups ); |
161 | } |
162 | |
163 | public static function launchTestsDatabase( Event $event ) { |
164 | self::launchTests( |
165 | $event, |
166 | [ 'Database' ], |
167 | array_merge( self::ALWAYS_EXCLUDE, [ 'Standalone' ] ) |
168 | ); |
169 | } |
170 | |
171 | public static function launchTestsDatabaseless( Event $event ) { |
172 | self::launchTests( |
173 | $event, |
174 | [], |
175 | array_merge( self::ALWAYS_EXCLUDE, [ 'Standalone', 'Database' ] ) |
176 | ); |
177 | } |
178 | } |