Debian Codegraph @

includes/composer/ComposerLaunchParallel.php

raw symbols SCIP
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 . '/maintenance/includes/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 $command->environment( [ 'MW_PHPUNIT_SPLIT_GROUP_ID' => $groupId ] );
101 print( "Running command '" . $command->getCommandString() . "' ..." . PHP_EOL );
102 $result = $command->execute();
103 print( $result->getStdout() );
104 exit( $result->getExitCode() );
105 }
106
107 private static function extractArgs(): array {
108 $options = [];
109 foreach ( [ "group", "exclude-group" ] as $argument ) {
110 $groupIndex = array_search( "--" . $argument, $_SERVER['argv'] );
111 if ( $groupIndex > 0 ) {
112 if ( count( $_SERVER['argv'] ) > $groupIndex + 1 ) {
113 $nextArg = $_SERVER['argv'][$groupIndex + 1];
114 if ( strpos( $nextArg, "--" ) === 0 ) {
115 throw new \InvalidArgumentException(
116 "parameter " . $argument . " takes a variable - none supplied"
117 );
118 }
119 $options[$argument] = $nextArg;
120 } else {
121 throw new \InvalidArgumentException(
122 "parameter " . $argument . " takes a variable - not enough arguments supplied"
123 );
124 }
125 }
126 }
127 return $options;
128 }
129
130 public static function launchTests( Event $event, array $groups, array $excludeGroups ): void {
131 $phpUnitConfig = getcwd() . DIRECTORY_SEPARATOR . 'phpunit.xml';
132 if ( !PhpUnitXml::isPhpUnitXmlPrepared( $phpUnitConfig ) ) {
133 $event->getIO()->error( "phpunit.xml is not present or does not contain split test suites" );
134 $event->getIO()->error( "run `composer phpunit:prepare-parallel:...` to generate the split suites" );
135 exit( 1 );
136 }
137 $event->getIO()->info( "Running 'split_group_X' suites in parallel..." );
138 $launcher = new ComposerLaunchParallel( $groups, $excludeGroups );
139 $launcher->start();
140 if ( $launcher->allSuccessful() ) {
141 $event->getIO()->info( "All split_groups succeeded!" );
142 exit( 0 );
143 } else {
144 $event->getIO()->warning( "Some split_groups failed - returning failure status" );
145 exit( 1 );
146 }
147 }
148
149 public static function launchTestsCustomGroups( Event $event ) {
150 $options = self::extractArgs();
151 if ( array_key_exists( 'exclude-group', $options ) ) {
152 $excludeGroups = explode( ',', $options['exclude-group'] );
153 } else {
154 $excludeGroups = [ 'Broken', 'ParserFuzz', 'Stub', 'Standalone', 'Database' ];
155 }
156 if ( array_key_exists( 'group', $options ) ) {
157 $groups = explode( ',', $options['group'] );
158 } else {
159 $groups = [];
160 }
161 self::launchTests( $event, $groups, $excludeGroups );
162 }
163
164 public static function launchTestsDatabase( Event $event ) {
165 self::launchTests(
166 $event,
167 [ 'Database' ],
168 array_merge( self::ALWAYS_EXCLUDE, [ 'Standalone' ] )
169 );
170 }
171
172 public static function launchTestsDatabaseless( Event $event ) {
173 self::launchTests(
174 $event,
175 [],
176 array_merge( self::ALWAYS_EXCLUDE, [ 'Standalone', 'Database' ] )
177 );
178 }
179 }