Compare commits

...

2 Commits

Author SHA1 Message Date
Joseph Bielawski
33798ff108 Add simple unit test 2023-09-06 01:00:24 +08:00
Joseph Bielawski
8d348b9e14 Allow setting multiple license in extensions, libraries & sources 2023-09-06 01:00:24 +08:00
5 changed files with 126 additions and 23 deletions

6
.gitignore vendored
View File

@ -17,11 +17,15 @@ composer.lock
# default source build root directory
/buildroot/
# php cs fixer cache file
# tools cache files
.php-cs-fixer.cache
.phpunit.result.cache
# exclude self-runtime
/bin/*
!/bin/spc
!/bin/setup-runtime
!/bin/spc-alpine-docker
# default test directory
/tests/var/

View File

@ -65,5 +65,5 @@ return (new PhpCsFixer\Config())
'phpdoc_var_without_name' => false,
])
->setFinder(
PhpCsFixer\Finder::create()->in(__DIR__ . '/src')
PhpCsFixer\Finder::create()->in([__DIR__ . '/src', __DIR__ . '/tests/SPC'])
);

View File

@ -17,11 +17,12 @@
"zhamao/logger": "^1.0"
},
"require-dev": {
"nunomaduro/collision": "*",
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
"phpstan/phpstan": "^1.1",
"captainhook/captainhook": "^5.10",
"captainhook/plugin-composer": "^5.3"
"captainhook/plugin-composer": "^5.3",
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
"nunomaduro/collision": "*",
"phpstan/phpstan": "^1.1",
"phpunit/phpunit": "^10.3"
},
"autoload": {
"psr-4": {
@ -32,13 +33,18 @@
"src/globals/functions.php"
]
},
"autoload-dev": {
"psr-4": {
"SPC\\Tests\\": "tests/SPC"
}
},
"bin": [
"bin/spc"
],
"scripts": {
"analyse": "phpstan analyse --memory-limit 300M",
"cs-fix": "php-cs-fixer fix",
"test": "bin/phpunit --no-coverage"
"test": "vendor/bin/phpunit tests/ --no-coverage"
},
"config": {
"allow-plugins": {

View File

@ -45,46 +45,58 @@ class LicenseDumper
{
// mkdir first
if (is_dir($target_dir) && !FileSystem::removeDir($target_dir)) {
logger()->warning('Target dump directory is noe empty, be aware!');
logger()->warning('Target dump directory is not empty, be aware!');
}
FileSystem::createDir($target_dir);
foreach ($this->exts as $ext) {
if (Config::getExt($ext, 'type') !== 'external') {
continue;
}
$source_name = Config::getExt($ext, 'source');
$content = $this->getSourceLicense($source_name);
file_put_contents($target_dir . '/ext_' . $ext . '.txt', $content);
foreach ($this->getSourceLicenses($source_name) as $index => $license) {
file_put_contents("{$target_dir}/ext_{$ext}_{$index}.txt", $license);
}
}
foreach ($this->libs as $lib) {
$source_name = Config::getLib($lib, 'source');
$content = $this->getSourceLicense($source_name);
file_put_contents($target_dir . '/lib_' . $lib . '.txt', $content);
foreach ($this->getSourceLicenses($source_name) as $index => $license) {
file_put_contents("{$target_dir}/lib_{$lib}_{$index}.txt", $license);
}
}
foreach ($this->sources as $source) {
file_put_contents($target_dir . '/src_' . $source . '.txt', $this->getSourceLicense($source));
foreach ($this->getSourceLicenses($source) as $index => $license) {
file_put_contents("{$target_dir}/src_{$source}_{$index}.txt", $license);
}
}
return true;
}
/**
* @return string[]
* @throws FileSystemException
* @throws RuntimeException
*/
private function getSourceLicense(string $source_name): ?string
private function getSourceLicenses(string $source_name): iterable
{
$src = Config::getSource($source_name)['license'] ?? null;
if ($src === null) {
throw new RuntimeException('source [' . $source_name . '] license meta is not exist');
$licenses = Config::getSource($source_name)['license'] ?? [];
if ($licenses === []) {
throw new RuntimeException('source [' . $source_name . '] license meta not exist');
}
return match ($src['type']) {
'text' => $src['text'],
'file' => $this->loadSourceFile($source_name, $src['path'], Config::getSource($source_name)['path'] ?? null),
default => throw new RuntimeException('source [' . $source_name . '] license type is not allowed'),
};
if (!array_is_list($licenses)) {
$licenses = [$licenses];
}
foreach ($licenses as $license) {
yield match ($license['type']) {
'text' => $license['text'],
'file' => $this->loadSourceFile($source_name, $license['path'], Config::getSource($source_name)['path'] ?? null),
default => throw new RuntimeException('source [' . $source_name . '] license type is not allowed'),
};
}
}
/**
@ -93,8 +105,9 @@ class LicenseDumper
private function loadSourceFile(string $source_name, string $in_path, ?string $custom_base_path = null): string
{
if (!file_exists(SOURCE_PATH . '/' . ($custom_base_path ?? $source_name) . '/' . $in_path)) {
throw new RuntimeException('source [' . $source_name . '] license file [' . $in_path . '] is not exist');
throw new RuntimeException('source [' . $source_name . '] license file [' . $in_path . '] not exist');
}
return file_get_contents(SOURCE_PATH . '/' . ($custom_base_path ?? $source_name) . '/' . $in_path);
}
}

View File

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace SPC\Tests\util;
use PHPUnit\Framework\TestCase;
use SPC\store\Config;
use SPC\util\LicenseDumper;
/**
* @internal
*/
final class LicenseDumperTest extends TestCase
{
private const DIRECTORY = '../../var/license-dump';
protected function setUp(): void
{
@rmdir(self::DIRECTORY);
}
protected function tearDown(): void
{
array_map('unlink', glob(self::DIRECTORY . '/*.txt'));
}
public function testDumpWithSingleLicense(): void
{
Config::$lib = [
'fake_lib' => [
'source' => 'fake_lib',
],
];
Config::$source = [
'fake_lib' => [
'license' => [
'type' => 'text',
'text' => 'license',
],
],
];
$dumper = new LicenseDumper();
$dumper->addLibs(['fake_lib']);
$dumper->dump(self::DIRECTORY);
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_0.txt');
}
public function testDumpWithMultipleLicenses(): void
{
Config::$lib = [
'fake_lib' => [
'source' => 'fake_lib',
],
];
Config::$source = [
'fake_lib' => [
'license' => [
[
'type' => 'text',
'text' => 'license',
],
[
'type' => 'text',
'text' => 'license',
],
],
],
];
$dumper = new LicenseDumper();
$dumper->addLibs(['fake_lib']);
$dumper->dump(self::DIRECTORY);
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_0.txt');
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_1.txt');
}
}