master
Anton Smirnov 12 months ago
parent bc35773000
commit d45ae9e982

1
.gitattributes vendored

@ -1,4 +1,5 @@
/sandbox export-ignore
/tests export-ignore
/.git* export-ignore
/*.yml export-ignore
/*.xml export-ignore

1
.gitignore vendored

@ -1,3 +1,4 @@
/.idea
/reports
vendor
composer.lock

@ -19,6 +19,7 @@
"php": "7.1.99"
},
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true
@ -26,11 +27,19 @@
"extra": {
"class": "Arokettu\\Composer\\LicenseManager\\LicenseManagerPlugin"
},
"scripts": {
"cov": "php74 -d xdebug.mode=coverage vendor/bin/phpunit --coverage-html reports/coverage --whitelist src"
},
"autoload": {
"psr-4": {
"Arokettu\\Composer\\LicenseManager\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Arokettu\\Composer\\LicenseManager\\Tests\\": "tests/"
}
},
"require": {
"php": "^7.1 || ^8",
"composer-plugin-api": "^2",
@ -38,6 +47,7 @@
},
"require-dev": {
"composer/composer": "^2.0",
"phpunit/phpunit": "^7.5 || ^9.5",
"sandfox.dev/code-standard": "^10@dev",
"squizlabs/php_codesniffer": "*",
"vimeo/psalm": "^4.23"

@ -5,4 +5,5 @@
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">
<rule ref="SandFox_PHP71"/>
<file>src</file>
<file>tests</file>
</ruleset>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
convertDeprecationsToExceptions="true"
executionOrder="random">
<testsuites>
<testsuite name="all">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
@ -10,6 +10,7 @@
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
<directory name="tests" />
</ignoreFiles>
</projectFiles>
</psalm>

@ -13,7 +13,8 @@
"require": {
"arokettu/composer-license-manager": "dev-master",
"laminas/laminas-code": "^4.5",
"symfony/string": "^6.1"
"symfony/string": "^6.1",
"npm-asset/jquery-form": "^4.3"
},
"config": {
"allow-plugins": {
@ -25,10 +26,10 @@
"licenses": {
"allowed": ["MIT", "BSD-*", "ISC", "artistic-2.0", "apache-2.0", "(wtfpl*", "*"],
"forbidden": ["BSD-3-Clause"],
"allow-empty": true
"allow-empty": false
},
"packages": {
"allowed": ["laminas/laminas-code"]
"allowed": ["laminas/laminas-code", "npm-asset/deep-extend"]
}
}
}

@ -31,6 +31,11 @@ final class Config
throw new \RuntimeException('Invalid config format: License Manager config must be an array');
}
return self::fromArray($config);
}
public static function fromArray(array $config): self
{
return new self(
ConfigHelper::valueToArray($config['licenses']['allowed'] ?? ['*']),
$config['licenses']['allow-empty'] ?? false,
@ -39,7 +44,7 @@ final class Config
);
}
public function __construct(
private function __construct(
array $licensesAllowed,
bool $allowEmptyLicense,
array $licensesForbidden,

@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
namespace Arokettu\Composer\LicenseManager\Tests;
use Arokettu\Composer\LicenseManager\Config\Config;
use Arokettu\Composer\LicenseManager\Helpers\LicenseHelper;
use Arokettu\Composer\LicenseManager\Tests\Models\MockPackage;
use PHPUnit\Framework\TestCase;
class LicenseTest extends TestCase
{
public function testDefaultConfig(): void
{
$config = Config::fromArray([]);
// any license is allowed
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['MIT']), $config));
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['(MIT AND LGPL-2.1)']), $config));
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['proprietary']), $config));
// except for no license set
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', []), $config));
}
public function testBasicAllow(): void
{
$config = Config::fromArray(['licenses' => ['allowed' => 'MIT']]);
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['MIT']), $config));
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['BSD-2-Clause']), $config));
}
public function testBasicForbidden(): void
{
$config = Config::fromArray(['licenses' => ['forbidden' => 'MIT']]);
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['MIT']), $config));
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['BSD-2-Clause']), $config));
}
public function testGlobAllow(): void
{
$config = Config::fromArray(['licenses' => ['allowed' => 'LGPL-*']]);
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['LGPL-2.1']), $config));
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['AGPL-3.0']), $config));
}
public function testGlobForbidden(): void
{
$config = Config::fromArray(['licenses' => ['forbidden' => 'AGPL-*']]);
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['LGPL-2.1']), $config));
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['AGPL-3.0']), $config));
}
public function testGlobDoesNotAllowExpressions(): void
{
// with bracket, allow
$config = Config::fromArray(['licenses' => ['allowed' => '(MIT*']]);
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['(MIT AND LGPL-2.1)']), $config));
// no bracket, allow
$config = Config::fromArray(['licenses' => ['allowed' => 'MIT*']]);
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['MIT AND LGPL-2.1']), $config));
// with bracket, forbid
$config = Config::fromArray(['licenses' => ['forbidden' => '(MIT*']]);
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['(MIT AND LGPL-2.1)']), $config));
// no bracket, forbid
$config = Config::fromArray(['licenses' => ['forbidden' => 'MIT*']]);
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['MIT AND LGPL-2.1']), $config));
// but exact is ok
$config = Config::fromArray(['licenses' => ['allowed' => 'MIT AND LGPL-2.1']]);
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['MIT AND LGPL-2.1']), $config));
$config = Config::fromArray(['licenses' => ['forbidden' => 'MIT AND LGPL-2.1']]);
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['MIT AND LGPL-2.1']), $config));
}
public function testEmptyLicense(): void
{
$config = Config::fromArray(['licenses' => ['allow-empty' => true]]);
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', []), $config));
$config = Config::fromArray(['licenses' => ['allow-empty' => false]]);
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/bar', []), $config));
}
public function testPackageExceptions(): void
{
$config = Config::fromArray([
'licenses' => [
'allow-empty' => false,
'forbidden' => ['LGPL-*', 'MIT'],
],
'packages' => [
'allowed' => ['foo/bar', 'mypackage/*'],
],
]);
// overrides empty
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', []), $config));
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/baz', []), $config));
// overrides exact
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['MIT']), $config));
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/baz', ['MIT']), $config));
// overrides glob
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('foo/bar', ['LGPL-2.1']), $config));
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('foo/baz', ['LGPL-2.1']), $config));
// by package glob too
self::assertTrue(LicenseHelper::isPermitted(new MockPackage('mypackage/demo', ['MIT']), $config));
self::assertFalse(LicenseHelper::isPermitted(new MockPackage('notmine/demo', ['MIT']), $config));
}
}

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Arokettu\Composer\LicenseManager\Tests\Models;
use Composer\Package\CompletePackage;
class MockPackage extends CompletePackage
{
public function __construct(string $name, array $licenses)
{
$this->name = $name;
$this->license = $licenses;
}
}
Loading…
Cancel
Save