ENH Improve ManifestFileFinder so it can ignore test based on the testing library

This commit is contained in:
Maxime Rainville 2021-11-16 18:44:48 +13:00
parent 2922370d81
commit 640a7e3eea
11 changed files with 103 additions and 3 deletions

View File

@ -32,7 +32,8 @@ class ManifestFileFinder extends FileFinder
'include_themes' => false,
'ignore_tests' => true,
'min_depth' => 1,
'ignore_dirs' => ['node_modules']
'ignore_dirs' => ['node_modules'],
'ignore_ci_library' => []
];
public function acceptDir($basename, $pathname, $depth)
@ -73,6 +74,14 @@ class ManifestFileFinder extends FileFinder
return false;
}
// Skip if test dir inside vendor module with unexpected CI library
if ($depth > 3 && $basename === self::TESTS_DIR && $ignoreCILib = $this->getOption('ignore_ci_library')) {
$ciLib = $this->findModuleCILib($basename, $pathname, $depth);
if (in_array($ciLib, $ignoreCILib)) {
return false;
}
}
return parent::acceptDir($basename, $pathname, $depth);
}
@ -251,4 +260,22 @@ class ManifestFileFinder extends FileFinder
return false;
}
private function findModuleCILib(string $basename, string $pathname, int $depth): string
{
if ($depth < 1) {
return Module::CI_PHPUNIT_UNKNOWN;
}
$newBasename = basename($pathname);
$newPathname = dirname($pathname);
$newDepth = $depth - 1;
if ($this->isDirectoryModule($newBasename, $newPathname, $newDepth)) {
$module = new Module($newPathname, $this->upLevels($newPathname, $newDepth));
return $module->getCILibrary();
}
return $this->findModuleCILib($newBasename, $newPathname, $newDepth);
}
}

View File

@ -5,7 +5,6 @@ namespace SilverStripe\Core\Manifest;
use Composer\Semver\Semver;
use Exception;
use InvalidArgumentException;
use RuntimeException;
use Serializable;
use SilverStripe\Core\Path;
use SilverStripe\Dev\Deprecation;
@ -301,8 +300,9 @@ class Module implements Serializable
*/
public function getCILibrary(): string
{
// We don't have any composer data at all
if (empty($this->composerData)) {
throw new RuntimeException('No composer data at all');
return self::CI_PHPUNIT_UNKNOWN;
}
// We don't have any dev dependencies

View File

@ -4,6 +4,7 @@ namespace SilverStripe\Core\Tests\Manifest;
use SilverStripe\Core\Manifest\ManifestFileFinder;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Core\Manifest\Module;
/**
* Tests for the {@link ManifestFileFinder} class.
@ -55,6 +56,8 @@ class ManifestFileFinderTest extends SapphireTest
[
'module/module.txt',
'vendor/myvendor/thismodule/module.txt',
'vendor/myvendor/phpunit5module/code/logic.txt',
'vendor/myvendor/phpunit9module/code/logic.txt',
]
);
}
@ -75,6 +78,56 @@ class ManifestFileFinderTest extends SapphireTest
'vendor/myvendor/thismodule/module.txt',
'vendor/myvendor/thismodule/tests/tests.txt',
'vendor/myvendor/thismodule/code/tests/tests2.txt',
'vendor/myvendor/phpunit5module/code/logic.txt',
'vendor/myvendor/phpunit5module/tests/phpunit5tests.txt',
'vendor/myvendor/phpunit9module/code/logic.txt',
'vendor/myvendor/phpunit9module/tests/phpunit9tests.txt',
]
);
}
public function testIgnorePHPUnit5Tests()
{
$finder = new ManifestFileFinder();
$finder->setOption('name_regex', '/\.txt$/');
$finder->setOption('ignore_tests', false);
$finder->setOption('ignore_ci_library', [Module::CI_PHPUNIT_FIVE]);
$this->assertFinderFinds(
$finder,
null,
[
'module/module.txt',
'module/tests/tests.txt',
'module/code/tests/tests2.txt',
'vendor/myvendor/thismodule/module.txt',
'vendor/myvendor/thismodule/tests/tests.txt',
'vendor/myvendor/thismodule/code/tests/tests2.txt',
'vendor/myvendor/phpunit5module/code/logic.txt',
'vendor/myvendor/phpunit9module/code/logic.txt',
'vendor/myvendor/phpunit9module/tests/phpunit9tests.txt',
]
);
}
public function testIgnoreNonePHPUnit9Tests()
{
$finder = new ManifestFileFinder();
$finder->setOption('name_regex', '/\.txt$/');
$finder->setOption('ignore_tests', false);
$finder->setOption('ignore_ci_library', [Module::CI_PHPUNIT_FIVE, Module::CI_PHPUNIT_UNKNOWN]);
$this->assertFinderFinds(
$finder,
null,
[
'module/module.txt',
'module/tests/tests.txt',
'module/code/tests/tests2.txt',
'vendor/myvendor/thismodule/module.txt',
'vendor/myvendor/phpunit5module/code/logic.txt',
'vendor/myvendor/phpunit9module/code/logic.txt',
'vendor/myvendor/phpunit9module/tests/phpunit9tests.txt',
]
);
}
@ -92,6 +145,8 @@ class ManifestFileFinderTest extends SapphireTest
'module/module.txt',
'themes/themes.txt',
'vendor/myvendor/thismodule/module.txt',
'vendor/myvendor/phpunit5module/code/logic.txt',
'vendor/myvendor/phpunit9module/code/logic.txt',
]
);
}

View File

@ -0,0 +1 @@
This file should always be discovered.

View File

@ -0,0 +1,6 @@
{
"type": "silverstripe-vendor-module",
"require-dev": {
"sminnee/phpunit": "^5.7"
}
}

View File

@ -0,0 +1 @@
This file should not be discovered when running PHPUnit 9 tests

View File

@ -0,0 +1 @@
This file should always be discovered.

View File

@ -0,0 +1,6 @@
{
"type": "silverstripe-vendor-module",
"require-dev": {
"phpunit/phpunit": "^9"
}
}

View File

@ -0,0 +1 @@
This file should not be discovered when running PHPUnit 9 tests