phpunit: Determine what extensions to load in unit tests via config
When running unit tests, the bootstrap would previously load all
extensions and skins in the filesystem. This was OK for an initial
implementation, but is not acceptable if we want to eventually do that
for all PHPUnit entry points (once we'll have a single config and
bootstrap). Instead, it's desirable to only load the extensions
specified in LocalSettings.php. The problem is that it's pretty much
impossible to load LocalSettings.php without also loading the rest of
MediaWiki, with all the side effects this might have.
This patch introduces a helper script that loads all the config, then
prints what extensions and skins were loaded. The bootstrap file runs
this script via proc_open and then reads the list of extensions to
load. Because the script is run in a separate process, any side effects
only affect the spawned process, not the one where PHPUnit is running.
Currently, there doesn't seem to be a better way to obtain the list of
extensions loaded in LocalSettings.php without all the other side
effects. YAML settings
(https://www.mediawiki.org/wiki/Manual:YAML_settings_file_format) would
probably help, but that's very far from becoming the only supported
config format (if it will ever be).
Also add two TestSuite implementations to replace the '*' wildcard in the
extensions:unit and skins:unit suites. These use the same list of loaded
extensions to determine where to look for tests.
And last but not least: my most sincere apologies to you if the hack
you're seeing here has ruined your day. If you think a better approach
exists, please tell me and I'll be so relieved!
Bug: T227900
Change-Id: Ib578644b8a4c0b64dca607afb9eb8204ca7fc660
2023-07-13 00:57:20 +00:00
|
|
|
#!/usr/bin/env php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WARNING: Hic sunt dracones!
|
|
|
|
|
*
|
|
|
|
|
* This script is used in the PHPUnit bootstrap to get a list of extensions and skins to autoload,
|
|
|
|
|
* without having the bootstrap file itself load any settings. It is a HUGE but unavoidable hack,
|
|
|
|
|
* if we want to avoid loading settings in unit tests, and at the same time only load the extensions
|
|
|
|
|
* enabled in LocalSettings.php without triggering any other side effects of Setup.php and the other
|
|
|
|
|
* files it includes.
|
|
|
|
|
* One day this may become unnecessary if we enforce YAML settings with a static list of extensions
|
|
|
|
|
* and skins (https://www.mediawiki.org/wiki/Manual:YAML_settings_file_format).
|
|
|
|
|
* The script was introduced for T227900, the idea being to have a single config file that can be used
|
|
|
|
|
* with both unit and integration tests.
|
|
|
|
|
* @internal This script should only be invoked by bootstrap.php, as part of the PHPUnit bootstrap process
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-13 20:13:18 +00:00
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
phpunit: Determine what extensions to load in unit tests via config
When running unit tests, the bootstrap would previously load all
extensions and skins in the filesystem. This was OK for an initial
implementation, but is not acceptable if we want to eventually do that
for all PHPUnit entry points (once we'll have a single config and
bootstrap). Instead, it's desirable to only load the extensions
specified in LocalSettings.php. The problem is that it's pretty much
impossible to load LocalSettings.php without also loading the rest of
MediaWiki, with all the side effects this might have.
This patch introduces a helper script that loads all the config, then
prints what extensions and skins were loaded. The bootstrap file runs
this script via proc_open and then reads the list of extensions to
load. Because the script is run in a separate process, any side effects
only affect the spawned process, not the one where PHPUnit is running.
Currently, there doesn't seem to be a better way to obtain the list of
extensions loaded in LocalSettings.php without all the other side
effects. YAML settings
(https://www.mediawiki.org/wiki/Manual:YAML_settings_file_format) would
probably help, but that's very far from becoming the only supported
config format (if it will ever be).
Also add two TestSuite implementations to replace the '*' wildcard in the
extensions:unit and skins:unit suites. These use the same list of loaded
extensions to determine where to look for tests.
And last but not least: my most sincere apologies to you if the hack
you're seeing here has ruined your day. If you think a better approach
exists, please tell me and I'll be so relieved!
Bug: T227900
Change-Id: Ib578644b8a4c0b64dca607afb9eb8204ca7fc660
2023-07-13 00:57:20 +00:00
|
|
|
require_once __DIR__ . '/bootstrap.common.php';
|
|
|
|
|
|
|
|
|
|
TestSetup::loadSettingsFiles();
|
|
|
|
|
|
|
|
|
|
$extensionsAndSkins = ExtensionRegistry::getInstance()->getQueue();
|
|
|
|
|
|
|
|
|
|
echo implode( "\n", array_keys( $extensionsAndSkins ) );
|
2023-07-13 20:13:18 +00:00
|
|
|
|
|
|
|
|
echo "\n\nTESTPATHS\n\n";
|
|
|
|
|
|
|
|
|
|
// Build a list of extension tests, based on autodiscovered tests as well as tests added with the
|
|
|
|
|
// UnitTestsList hook. The list is only to be used in ExtensionsTestSuite.
|
|
|
|
|
// This is a hack inside the hack, motivated by the fact that we can't access MediaWikiServices, or even
|
|
|
|
|
// just the config needed to create a hook container, as early as when ExtensionsTestSuite::suite is called.
|
|
|
|
|
// This particular hack can be removed once the UnitTestsList hook is removed (T298509)
|
|
|
|
|
$registry = ExtensionRegistry::getInstance();
|
|
|
|
|
foreach ( $registry->getAllThings() as $info ) {
|
|
|
|
|
$paths[] = dirname( $info['path'] ) . '/tests/phpunit';
|
|
|
|
|
}
|
|
|
|
|
// Extensions can return a list of files or directories
|
|
|
|
|
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onUnitTestsList( $paths );
|
|
|
|
|
|
|
|
|
|
echo implode( "\n", $paths );
|