| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
$splitGroupCount = 7; |
| 48 |
if ( $this->isDatabaseRun() ) { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 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 |
|
| 78 |
|
| 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 |
} |