From 2922370d81418f6ae8dae3ca1e677981e454ad91 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Tue, 16 Nov 2021 16:52:32 +1300 Subject: [PATCH] API Add Module::getCILibrary function --- composer.json | 1 + src/Core/Manifest/Module.php | 91 +++++++++++++++++++ tests/php/Core/Manifest/ModuleTest.php | 37 ++++++++ .../empty-require-dev/composer.json | 11 +++ .../future-phpunit/composer.json | 13 +++ .../inbetween-phpunit/composer.json | 13 +++ .../no-require-dev/composer.json | 10 ++ .../old-phpunit/composer.json | 13 +++ .../phpunit-five-seven/composer.json | 13 +++ .../phpunit-five-zero/composer.json | 13 +++ .../phpunit-nine-five/composer.json | 13 +++ .../phpunit-nine-x/composer.json | 13 +++ .../phpunit-nine/composer.json | 13 +++ .../recipe-testing-one-two-x/composer.json | 13 +++ .../recipe-testing-one-x/composer.json | 13 +++ .../recipe-testing-one/composer.json | 13 +++ .../recipe-testing-two-x/composer.json | 13 +++ .../recipe-testing-two/composer.json | 13 +++ .../sminnee-five-seven/composer.json | 13 +++ .../sminnee-five/composer.json | 13 +++ 20 files changed, 345 insertions(+) create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/empty-require-dev/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/future-phpunit/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/inbetween-phpunit/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/no-require-dev/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/old-phpunit/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-five-seven/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-five-zero/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine-five/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine-x/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one-two-x/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one-x/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-two-x/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-two/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/sminnee-five-seven/composer.json create mode 100644 tests/php/Core/Manifest/fixtures/phpunit-detection/sminnee-five/composer.json diff --git a/composer.json b/composer.json index 2d77c3929..5619aac8d 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Core/Manifest/Module.php b/src/Core/Manifest/Module.php index 4337ddf25..22bd08e46 100644 --- a/src/Core/Manifest/Module.php +++ b/src/Core/Manifest/Module.php @@ -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)); + } } /** diff --git a/tests/php/Core/Manifest/ModuleTest.php b/tests/php/Core/Manifest/ModuleTest.php index b62a28941..83af14cc6 100644 --- a/tests/php/Core/Manifest/ModuleTest.php +++ b/tests/php/Core/Manifest/ModuleTest.php @@ -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], + ]; + } } diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/empty-require-dev/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/empty-require-dev/composer.json new file mode 100644 index 000000000..6d1ee8cbb --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/empty-require-dev/composer.json @@ -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": {} +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/future-phpunit/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/future-phpunit/composer.json new file mode 100644 index 000000000..276608198 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/future-phpunit/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/inbetween-phpunit/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/inbetween-phpunit/composer.json new file mode 100644 index 000000000..925cea0b0 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/inbetween-phpunit/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/no-require-dev/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/no-require-dev/composer.json new file mode 100644 index 000000000..7ff1d854e --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/no-require-dev/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/old-phpunit/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/old-phpunit/composer.json new file mode 100644 index 000000000..f4fe84a50 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/old-phpunit/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-five-seven/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-five-seven/composer.json new file mode 100644 index 000000000..36ef4a8fa --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-five-seven/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-five-zero/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-five-zero/composer.json new file mode 100644 index 000000000..c3015d1aa --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-five-zero/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine-five/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine-five/composer.json new file mode 100644 index 000000000..eca645ddd --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine-five/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine-x/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine-x/composer.json new file mode 100644 index 000000000..117485132 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine-x/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine/composer.json new file mode 100644 index 000000000..7b4ddcbe8 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/phpunit-nine/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one-two-x/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one-two-x/composer.json new file mode 100644 index 000000000..fb74e6524 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one-two-x/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one-x/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one-x/composer.json new file mode 100644 index 000000000..69a489f53 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one-x/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one/composer.json new file mode 100644 index 000000000..44a07e8dc --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-one/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-two-x/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-two-x/composer.json new file mode 100644 index 000000000..074213b06 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-two-x/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-two/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-two/composer.json new file mode 100644 index 000000000..0cec3ae86 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/recipe-testing-two/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/sminnee-five-seven/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/sminnee-five-seven/composer.json new file mode 100644 index 000000000..2bb3f2e30 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/sminnee-five-seven/composer.json @@ -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" + } +} diff --git a/tests/php/Core/Manifest/fixtures/phpunit-detection/sminnee-five/composer.json b/tests/php/Core/Manifest/fixtures/phpunit-detection/sminnee-five/composer.json new file mode 100644 index 000000000..e656472a1 --- /dev/null +++ b/tests/php/Core/Manifest/fixtures/phpunit-detection/sminnee-five/composer.json @@ -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" + } +}