ENH Don't index test from PHPUNit 5.7 module wdon using PHPUnit 9.5

This commit is contained in:
Maxime Rainville 2021-11-17 12:37:16 +13:00
parent 640a7e3eea
commit cbc4593ab4
12 changed files with 89 additions and 30 deletions

View File

@ -509,6 +509,18 @@ class CoreKernel implements Kernel
return false; return false;
} }
/**
* When manifests are discovering files, tests files in modules using the following CI library type will be ignored.
*
* The purpose of this method is to avoid loading PHPUnit test files with incompatible definitions.
*
* @return string[] List of CI types to ignore as defined by `Module`.
*/
protected function getIgnoreCILibraries(): array
{
return [];
}
/** /**
* Boot all manifests * Boot all manifests
* *
@ -517,10 +529,18 @@ class CoreKernel implements Kernel
protected function bootManifests($flush) protected function bootManifests($flush)
{ {
// Setup autoloader // Setup autoloader
$this->getClassLoader()->init($this->getIncludeTests(), $flush); $this->getClassLoader()->init(
$this->getIncludeTests(),
$flush,
$this->getIgnoreCILibraries()
);
// Find modules // Find modules
$this->getModuleLoader()->init($this->getIncludeTests(), $flush); $this->getModuleLoader()->init(
$this->getIncludeTests(),
$flush,
$this->getIgnoreCILibraries()
);
// Flush config // Flush config
if ($flush) { if ($flush) {
@ -538,7 +558,11 @@ class CoreKernel implements Kernel
$defaultSet->setProject( $defaultSet->setProject(
ModuleManifest::config()->get('project') ModuleManifest::config()->get('project')
); );
$defaultSet->init($this->getIncludeTests(), $flush); $defaultSet->init(
$this->getIncludeTests(),
$flush,
$this->getIgnoreCILibraries()
);
} }
} }

View File

@ -120,13 +120,14 @@ class ClassLoader
* *
* @param bool $includeTests * @param bool $includeTests
* @param bool $forceRegen * @param bool $forceRegen
* @param string[] $ignoredCILibs
*/ */
public function init($includeTests = false, $forceRegen = false) public function init($includeTests = false, $forceRegen = false, array $ignoredCILibs = [])
{ {
foreach ($this->manifests as $manifest) { foreach ($this->manifests as $manifest) {
/** @var ClassManifest $instance */ /** @var ClassManifest $instance */
$instance = $manifest['instance']; $instance = $manifest['instance'];
$instance->init($includeTests, $forceRegen); $instance->init($includeTests, $forceRegen, $ignoredCILibs);
} }
$this->registerAutoloader(); $this->registerAutoloader();

View File

@ -260,8 +260,9 @@ class ClassManifest
* *
* @param bool $includeTests * @param bool $includeTests
* @param bool $forceRegen * @param bool $forceRegen
* @param string[] $ignoredCILibs
*/ */
public function init($includeTests = false, $forceRegen = false) public function init($includeTests = false, $forceRegen = false, array $ignoredCILibs = [])
{ {
$this->cache = $this->buildCache($includeTests); $this->cache = $this->buildCache($includeTests);
@ -275,7 +276,7 @@ class ClassManifest
} }
// Build // Build
$this->regenerate($includeTests); $this->regenerate($includeTests, $ignoredCILibs);
} }
/** /**
@ -500,8 +501,9 @@ class ClassManifest
* Completely regenerates the manifest file. * Completely regenerates the manifest file.
* *
* @param bool $includeTests * @param bool $includeTests
* @param string[] $ignoredCILibs
*/ */
public function regenerate($includeTests) public function regenerate($includeTests, array $ignoredCILibs = [])
{ {
// Reset the manifest so stale info doesn't cause errors. // Reset the manifest so stale info doesn't cause errors.
$this->loadState([]); $this->loadState([]);
@ -513,6 +515,7 @@ class ClassManifest
'name_regex' => '/^[^_].*\\.php$/', 'name_regex' => '/^[^_].*\\.php$/',
'ignore_files' => ['index.php', 'cli-script.php'], 'ignore_files' => ['index.php', 'cli-script.php'],
'ignore_tests' => !$includeTests, 'ignore_tests' => !$includeTests,
'ignore_ci_libraries' => $ignoredCILibs,
'file_callback' => function ($basename, $pathname, $depth) use ($includeTests, $finder) { 'file_callback' => function ($basename, $pathname, $depth) use ($includeTests, $finder) {
$this->handleFile($basename, $pathname, $includeTests); $this->handleFile($basename, $pathname, $includeTests);
}, },

View File

@ -33,7 +33,7 @@ class ManifestFileFinder extends FileFinder
'ignore_tests' => true, 'ignore_tests' => true,
'min_depth' => 1, 'min_depth' => 1,
'ignore_dirs' => ['node_modules'], 'ignore_dirs' => ['node_modules'],
'ignore_ci_library' => [] 'ignore_ci_libraries' => []
]; ];
public function acceptDir($basename, $pathname, $depth) public function acceptDir($basename, $pathname, $depth)
@ -75,7 +75,7 @@ class ManifestFileFinder extends FileFinder
} }
// Skip if test dir inside vendor module with unexpected CI library // Skip if test dir inside vendor module with unexpected CI library
if ($depth > 3 && $basename === self::TESTS_DIR && $ignoreCILib = $this->getOption('ignore_ci_library')) { if ($depth > 3 && $basename === self::TESTS_DIR && $ignoreCILib = $this->getOption('ignore_ci_libraries')) {
$ciLib = $this->findModuleCILib($basename, $pathname, $depth); $ciLib = $this->findModuleCILib($basename, $pathname, $depth);
if (in_array($ciLib, $ignoreCILib)) { if (in_array($ciLib, $ignoreCILib)) {
return false; return false;
@ -264,7 +264,7 @@ class ManifestFileFinder extends FileFinder
private function findModuleCILib(string $basename, string $pathname, int $depth): string private function findModuleCILib(string $basename, string $pathname, int $depth): string
{ {
if ($depth < 1) { if ($depth < 1) {
return Module::CI_PHPUNIT_UNKNOWN; return Module::CI_UNKNOWN;
} }
$newBasename = basename($pathname); $newBasename = basename($pathname);

View File

@ -33,7 +33,7 @@ class Module implements Serializable
/** /**
* Return value of getCILibrary() when module does not use any CI * Return value of getCILibrary() when module does not use any CI
*/ */
const CI_PHPUNIT_UNKNOWN = 'NoPHPUnit'; const CI_UNKNOWN = 'NoPHPUnit';
@ -302,12 +302,12 @@ class Module implements Serializable
{ {
// We don't have any composer data at all // We don't have any composer data at all
if (empty($this->composerData)) { if (empty($this->composerData)) {
return self::CI_PHPUNIT_UNKNOWN; return self::CI_UNKNOWN;
} }
// We don't have any dev dependencies // We don't have any dev dependencies
if (empty($this->composerData['require-dev']) || !is_array($this->composerData['require-dev'])) { if (empty($this->composerData['require-dev']) || !is_array($this->composerData['require-dev'])) {
return self::CI_PHPUNIT_UNKNOWN; return self::CI_UNKNOWN;
} }
// We are assuming a typical setup where the CI lib is defined in require-dev rather than require // We are assuming a typical setup where the CI lib is defined in require-dev rather than require
@ -335,7 +335,7 @@ class Module implements Serializable
} }
} }
return self::CI_PHPUNIT_UNKNOWN; return self::CI_UNKNOWN;
} }
/** /**

View File

@ -91,11 +91,12 @@ class ModuleLoader
* *
* @param bool $includeTests * @param bool $includeTests
* @param bool $forceRegen * @param bool $forceRegen
* @param string[] $ignoredCILibs
*/ */
public function init($includeTests = false, $forceRegen = false) public function init($includeTests = false, $forceRegen = false, array $ignoredCILibs = [])
{ {
foreach ($this->manifests as $manifest) { foreach ($this->manifests as $manifest) {
$manifest->init($includeTests, $forceRegen); $manifest->init($includeTests, $forceRegen, $ignoredCILibs);
} }
} }
} }

View File

@ -121,8 +121,9 @@ class ModuleManifest
/** /**
* @param bool $includeTests * @param bool $includeTests
* @param bool $forceRegen Force the manifest to be regenerated. * @param bool $forceRegen Force the manifest to be regenerated.
* @param string[] $ignoredCILibs
*/ */
public function init($includeTests = false, $forceRegen = false) public function init($includeTests = false, $forceRegen = false, array $ignoredCILibs = [])
{ {
// build cache from factory // build cache from factory
if ($this->cacheFactory) { if ($this->cacheFactory) {
@ -137,7 +138,7 @@ class ModuleManifest
$this->modules = $this->cache->get($this->cacheKey) ?: []; $this->modules = $this->cache->get($this->cacheKey) ?: [];
} }
if (empty($this->modules)) { if (empty($this->modules)) {
$this->regenerate($includeTests); $this->regenerate($includeTests, $ignoredCILibs);
} }
} }
@ -162,8 +163,9 @@ class ModuleManifest
* Does _not_ build the actual variant * Does _not_ build the actual variant
* *
* @param bool $includeTests * @param bool $includeTests
* @param string[] $ignoredCILibs
*/ */
public function regenerate($includeTests = false) public function regenerate($includeTests = false, array $ignoredCILibs = [])
{ {
$this->modules = []; $this->modules = [];
@ -171,6 +173,7 @@ class ModuleManifest
$finder->setOptions([ $finder->setOptions([
'min_depth' => 0, 'min_depth' => 0,
'ignore_tests' => !$includeTests, 'ignore_tests' => !$includeTests,
'ignore_ci_libraries' => $ignoredCILibs,
'dir_callback' => function ($basename, $pathname, $depth) use ($finder) { 'dir_callback' => function ($basename, $pathname, $depth) use ($finder) {
if ($finder->isDirectoryModule($basename, $pathname, $depth)) { if ($finder->isDirectoryModule($basename, $pathname, $depth)) {
$this->addModule($pathname); $this->addModule($pathname);

View File

@ -29,6 +29,7 @@ use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Injector\InjectorLoader; use SilverStripe\Core\Injector\InjectorLoader;
use SilverStripe\Core\Manifest\ClassLoader; use SilverStripe\Core\Manifest\ClassLoader;
use SilverStripe\Core\Manifest\Module;
use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Dev\Constraint\SSListContains; use SilverStripe\Dev\Constraint\SSListContains;
use SilverStripe\Dev\Constraint\SSListContainsOnly; use SilverStripe\Dev\Constraint\SSListContainsOnly;
@ -1010,6 +1011,9 @@ if (class_exists(IsEqualCanonicalizing::class)) {
// Test application // Test application
$kernel = new TestKernel(BASE_PATH); $kernel = new TestKernel(BASE_PATH);
// PHPUnit 9 only logic to exclude old test still targeting PHPUNit 5.7
$kernel->setIgnoreCILibraries([Module::CI_PHPUNIT_FIVE, Module::CI_UNKNOWN]);
if (class_exists(HTTPApplication::class)) { if (class_exists(HTTPApplication::class)) {
// Mock request // Mock request
$_SERVER['argv'] = ['vendor/bin/phpunit', '/']; $_SERVER['argv'] = ['vendor/bin/phpunit', '/'];

View File

@ -9,6 +9,11 @@ use SilverStripe\Core\CoreKernel;
*/ */
class TestKernel extends CoreKernel class TestKernel extends CoreKernel
{ {
/** @var string[] $ciLibs */
private $ciLibs = [];
public function __construct($basePath) public function __construct($basePath)
{ {
$this->setEnvironment(self::DEV); $this->setEnvironment(self::DEV);
@ -41,6 +46,21 @@ class TestKernel extends CoreKernel
return true; return true;
} }
/**
* @param string[] $ciLibs
*/
public function setIgnoreCILibraries(array $ciLibs): self
{
$this->ciLibs = $ciLibs;
return $this;
}
protected function getIgnoreCILibraries(): array
{
return $this->ciLibs;
}
protected function bootErrorHandling() protected function bootErrorHandling()
{ {
// Leave phpunit to capture errors // Leave phpunit to capture errors

View File

@ -73,8 +73,9 @@ class ThemeManifest implements ThemeList
/** /**
* @param bool $includeTests Include tests in the manifest * @param bool $includeTests Include tests in the manifest
* @param bool $forceRegen Force the manifest to be regenerated. * @param bool $forceRegen Force the manifest to be regenerated.
* @param string[] $ignoredCILibs
*/ */
public function init($includeTests = false, $forceRegen = false) public function init($includeTests = false, $forceRegen = false, array $ignoredCILibs = [])
{ {
// build cache from factory // build cache from factory
if ($this->cacheFactory) { if ($this->cacheFactory) {
@ -87,7 +88,7 @@ class ThemeManifest implements ThemeList
if (!$forceRegen && $this->cache && ($data = $this->cache->get($this->cacheKey))) { if (!$forceRegen && $this->cache && ($data = $this->cache->get($this->cacheKey))) {
$this->themes = $data; $this->themes = $data;
} else { } else {
$this->regenerate($includeTests); $this->regenerate($includeTests, $ignoredCILibs);
} }
} }
@ -129,14 +130,16 @@ class ThemeManifest implements ThemeList
* Regenerates the manifest by scanning the base path. * Regenerates the manifest by scanning the base path.
* *
* @param bool $includeTests * @param bool $includeTests
* @param string[] $ignoredCILibs
*/ */
public function regenerate($includeTests = false) public function regenerate($includeTests = false, array $ignoredCILibs = [])
{ {
$finder = new ManifestFileFinder(); $finder = new ManifestFileFinder();
$finder->setOptions([ $finder->setOptions([
'include_themes' => false, 'include_themes' => false,
'ignore_dirs' => ['node_modules', THEMES_DIR], 'ignore_dirs' => ['node_modules', THEMES_DIR],
'ignore_tests' => !$includeTests, 'ignore_tests' => !$includeTests,
'ignore_ci_libraries' => $ignoredCILibs,
'dir_callback' => [$this, 'handleDirectory'] 'dir_callback' => [$this, 'handleDirectory']
]); ]);

View File

@ -91,7 +91,7 @@ class ManifestFileFinderTest extends SapphireTest
$finder = new ManifestFileFinder(); $finder = new ManifestFileFinder();
$finder->setOption('name_regex', '/\.txt$/'); $finder->setOption('name_regex', '/\.txt$/');
$finder->setOption('ignore_tests', false); $finder->setOption('ignore_tests', false);
$finder->setOption('ignore_ci_library', [Module::CI_PHPUNIT_FIVE]); $finder->setOption('ignore_ci_libraries', [Module::CI_PHPUNIT_FIVE]);
$this->assertFinderFinds( $this->assertFinderFinds(
$finder, $finder,
@ -115,7 +115,7 @@ class ManifestFileFinderTest extends SapphireTest
$finder = new ManifestFileFinder(); $finder = new ManifestFileFinder();
$finder->setOption('name_regex', '/\.txt$/'); $finder->setOption('name_regex', '/\.txt$/');
$finder->setOption('ignore_tests', false); $finder->setOption('ignore_tests', false);
$finder->setOption('ignore_ci_library', [Module::CI_PHPUNIT_FIVE, Module::CI_PHPUNIT_UNKNOWN]); $finder->setOption('ignore_ci_libraries', [Module::CI_PHPUNIT_FIVE, Module::CI_UNKNOWN]);
$this->assertFinderFinds( $this->assertFinderFinds(
$finder, $finder,

View File

@ -35,11 +35,11 @@ class ModuleTest extends SapphireTest
public function ciLibraryProvider() public function ciLibraryProvider()
{ {
return [ return [
'empty require-dev' => ['empty-require-dev', Module::CI_PHPUNIT_UNKNOWN], 'empty require-dev' => ['empty-require-dev', Module::CI_UNKNOWN],
'no require-dev' => ['no-require-dev', Module::CI_PHPUNIT_UNKNOWN], 'no require-dev' => ['no-require-dev', Module::CI_UNKNOWN],
'older version of phpunit' => ['old-phpunit', Module::CI_PHPUNIT_UNKNOWN], 'older version of phpunit' => ['old-phpunit', Module::CI_UNKNOWN],
'phpunit between 5 and 9' => ['inbetween-phpunit', Module::CI_PHPUNIT_UNKNOWN], 'phpunit between 5 and 9' => ['inbetween-phpunit', Module::CI_UNKNOWN],
'phpunit beyond 9' => ['future-phpunit', Module::CI_PHPUNIT_UNKNOWN], 'phpunit beyond 9' => ['future-phpunit', Module::CI_UNKNOWN],
'phpunit 5.0' => ['phpunit-five-zero', Module::CI_PHPUNIT_FIVE], 'phpunit 5.0' => ['phpunit-five-zero', Module::CI_PHPUNIT_FIVE],
'phpunit 5.7' => ['phpunit-five-seven', Module::CI_PHPUNIT_FIVE], 'phpunit 5.7' => ['phpunit-five-seven', Module::CI_PHPUNIT_FIVE],