API Add Module::getCILibrary function

This commit is contained in:
Maxime Rainville 2021-11-16 16:52:32 +13:00
parent 1bc1e85f68
commit 2922370d81
20 changed files with 345 additions and 0 deletions

View File

@ -24,6 +24,7 @@
"require": {
"bramus/monolog-colored-line-formatter": "^2",
"composer/installers": "^1 || ^2",
"composer/semver": "^1 || ^3",
"embed/embed": "^3",
"league/csv": "^8 || ^9",
"m1/env": "^2.1",

View File

@ -2,8 +2,10 @@
namespace SilverStripe\Core\Manifest;
use Composer\Semver\Semver;
use Exception;
use InvalidArgumentException;
use RuntimeException;
use Serializable;
use SilverStripe\Core\Path;
use SilverStripe\Dev\Deprecation;
@ -19,6 +21,23 @@ class Module implements Serializable
*/
const TRIM_CHARS = ' /\\';
/**
* Return value of getCILibrary() when module uses PHPUNit 9
*/
const CI_PHPUNIT_NINE = 'PHPUnit9';
/**
* Return value of getCILibrary() when module uses PHPUNit 5
*/
const CI_PHPUNIT_FIVE = 'PHPUnit5';
/**
* Return value of getCILibrary() when module does not use any CI
*/
const CI_PHPUNIT_UNKNOWN = 'NoPHPUnit';
/**
* Full directory path to this module with no trailing slash
*
@ -275,6 +294,78 @@ class Module implements Serializable
->getResource($path)
->exists();
}
/**
* Determine what CI library the module is using.
* @internal
*/
public function getCILibrary(): string
{
if (empty($this->composerData)) {
throw new RuntimeException('No composer data at all');
}
// We don't have any dev dependencies
if (empty($this->composerData['require-dev']) || !is_array($this->composerData['require-dev'])) {
return self::CI_PHPUNIT_UNKNOWN;
}
// We are assuming a typical setup where the CI lib is defined in require-dev rather than require
$requireDev = $this->composerData['require-dev'];
// Try to pick which CI we are using based on phpunit constraint
$phpUnitConstraint = $this->requireDevConstraint(['sminnee/phpunit', 'phpunit/phpunit']);
if ($phpUnitConstraint) {
if ($this->satisfiesAtLeastOne(['5.7.0', '5.0.0', '5.x-dev', '5.7.x-dev'], $phpUnitConstraint)) {
return self::CI_PHPUNIT_FIVE;
}
if ($this->satisfiesAtLeastOne(['9.0.0', '9.5.0', '9.x-dev', '9.5.x-dev'], $phpUnitConstraint)) {
return self::CI_PHPUNIT_NINE;
}
}
// Try to pick which CI we are using based on recipe-testing constraint
$recipeTestingConstraint = $this->requireDevConstraint(['silverstripe/recipe-testing']);
if ($recipeTestingConstraint) {
if ($this->satisfiesAtLeastOne(['1.0.0', '1.1.0', '1.2.0', '1.1.x-dev', '1.2.x-dev', '1.x-dev'], $recipeTestingConstraint)) {
return self::CI_PHPUNIT_FIVE;
}
if ($this->satisfiesAtLeastOne(['2.0.0', '2.0.x-dev', '2.x-dev'], $recipeTestingConstraint)) {
return self::CI_PHPUNIT_NINE;
}
}
return self::CI_PHPUNIT_UNKNOWN;
}
/**
* Retrieve the constraint for the first module that is found in the require-dev section
* @param string[] $modules
* @return false|string
*/
private function requireDevConstraint(array $modules)
{
if (empty($this->composerData['require-dev']) || !is_array($this->composerData['require-dev'])) {
return false;
}
$requireDev = $this->composerData['require-dev'];
foreach ($modules as $module) {
if (isset($requireDev[$module])) {
return $requireDev[$module];
}
}
return false;
}
/**
* Determines if the provided constraint allows at least one of the version provided
*/
private function satisfiesAtLeastOne(array $versions, string $constraint): bool
{
return !empty(Semver::satisfiedBy($versions, $constraint));
}
}
/**

View File

@ -21,4 +21,41 @@ class ModuleTest extends SapphireTest
$module = new Module($path, $path);
$this->assertEquals('customised-resources-dir', $module->getResourcesDir());
}
/**
* @dataProvider ciLibraryProvider
*/
public function testGetCILibrary($fixture, $expected)
{
$path = __DIR__ . '/fixtures/phpunit-detection/' . $fixture;
$module = new Module($path, $path);
$this->assertEquals($expected, $module->getCILibrary());
}
public function ciLibraryProvider()
{
return [
'empty require-dev' => ['empty-require-dev', Module::CI_PHPUNIT_UNKNOWN],
'no require-dev' => ['no-require-dev', Module::CI_PHPUNIT_UNKNOWN],
'older version of phpunit' => ['old-phpunit', Module::CI_PHPUNIT_UNKNOWN],
'phpunit between 5 and 9' => ['inbetween-phpunit', Module::CI_PHPUNIT_UNKNOWN],
'phpunit beyond 9' => ['future-phpunit', Module::CI_PHPUNIT_UNKNOWN],
'phpunit 5.0' => ['phpunit-five-zero', Module::CI_PHPUNIT_FIVE],
'phpunit 5.7' => ['phpunit-five-seven', Module::CI_PHPUNIT_FIVE],
'sminnee 5.7' => ['sminnee-five-seven', Module::CI_PHPUNIT_FIVE],
'sminnee 5' => ['sminnee-five-seven', Module::CI_PHPUNIT_FIVE],
'phpunit 9' => ['phpunit-nine', Module::CI_PHPUNIT_NINE],
'phpunit 9.5' => ['phpunit-nine-five', Module::CI_PHPUNIT_NINE],
'future phpunit 9' => ['phpunit-nine-x', Module::CI_PHPUNIT_NINE],
'recipe-testing 1' => ['recipe-testing-one', Module::CI_PHPUNIT_FIVE],
'recipe-testing 1.x' => ['recipe-testing-one-x', Module::CI_PHPUNIT_FIVE],
'recipe-testing 1.2.x' => ['recipe-testing-one-two-x', Module::CI_PHPUNIT_FIVE],
'recipe-testing 2' => ['recipe-testing-two', Module::CI_PHPUNIT_NINE],
'recipe-testing 2.x' => ['recipe-testing-two-x', Module::CI_PHPUNIT_NINE],
];
}
}

View File

@ -0,0 +1,11 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module does not define a CI library in it's dev dependency",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses a future version of phpunit",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0.0"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses a version of phpunit in between 5 and 9",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"phpunit/phpunit": "^6"
}
}

View File

@ -0,0 +1,10 @@
{
"name": "silverstripe/no-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module does not define any dev dependency",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/old-phpunit",
"type": "silverstripe-vendor-module",
"description": "This fake module uses a version of phpunit prior to 5",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses any version of sminnee/PHPUnit 5",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"sminnee/phpunit": "^5"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses any version of PHPUnit 5",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses version of phpunit 9.5",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses a future version of phpunit 9",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"phpunit/phpunit": "^9.999999"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses any version of phpunit 9",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"phpunit/phpunit": "^9"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses unstable version of recipe testing 1.2",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"silverstripe/recipe-testing": "1.2.x-dev"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses unstable version of recipe testing 1",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"silverstripe/recipe-testing": "1.x-dev"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses any version of recipe testing 1",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"silverstripe/recipe-testing": "^1"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses unreleased version recipe testing 2",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"silverstripe/recipe-testing": "2.x-dev"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses any version of recipe testing 2",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"silverstripe/recipe-testing": "^2.0.0"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses any version of PHPUnit 5.7",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
}
}

View File

@ -0,0 +1,13 @@
{
"name": "silverstripe/empty-require-dev",
"type": "silverstripe-vendor-module",
"description": "This fake module uses any version of sminnee/PHPUnit 5.7",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"require-dev": {
"sminnee/phpunit": "^5"
}
}