Skip to content

Commit

Permalink
Merge pull request #5 from bestit/feature/mutiple-configs
Browse files Browse the repository at this point in the history
Added support for multiple config files.
  • Loading branch information
André Varelmann authored Apr 19, 2021
2 parents 80259b3 + e481d8d commit c02190d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 38 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ composer require best-it/license-check --dev --prefer-dist

## Usage

Create a YAML configuration ([license-check.yml](license-check.yml)) like this:
Create one or more YAML configuration files ([license-check.yml](license-check.yml)) like this:
```yaml
allowed-licenses:
- MIT
Expand All @@ -28,6 +28,8 @@ allowed-packages:
The allowed packages must be defined as a regular expression.
If multiple files are passed as an argument they will be merged to a single configuration.
Execute the following command to get a report which includes the information that everything is compatible or that
some dependencies are not compatible with your configuration. In case of problems the error code is 1.
```bash
Expand All @@ -36,4 +38,4 @@ some dependencies are not compatible with your configuration. In case of problem

## Development / Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md).
See [CONTRIBUTING.md](./CONTRIBUTING.md).
11 changes: 7 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions src/Command/LicenseCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ protected function configure(): void
->addOption(
self::OPTION_CONFIGURATION,
'c',
InputOption::VALUE_REQUIRED,
'Path to configuration file.',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'List of configuration files.',
)
->addOption(
self::OPTION_IGNORE_ERRORS,
Expand All @@ -109,6 +109,7 @@ protected function configure(): void
*
* @throws ConfigurationNotFoundException Exception if configuration is not found.
* @throws ConfigurationParseException Exception if configuration cannot be parsed.
* @throws CommandException Thrown if directory is not readable
*
* @return int
*/
Expand All @@ -122,12 +123,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new CommandException('Cannot read working directory.');
}

if (!is_string($configurationPath = $input->getOption(self::OPTION_CONFIGURATION))) {
$configurationPath = $workingDirectory . '/license-check.yml';
$configFiles = $input->getOption(self::OPTION_CONFIGURATION);

assert(is_array($configFiles));
if (count($configFiles) === 0) {
$configFiles[] = $workingDirectory . '/license-check.yml';
}

assert(is_string($configurationPath));
$configuration = $this->configurationLoader->load($configurationPath);
$configuration = $this->configurationLoader->load($configFiles);

$resultSet = $this->checker->validate($configuration, $workingDirectory);

Expand Down
44 changes: 35 additions & 9 deletions src/Configuration/ConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,46 @@ private function getAllowedPackages(array $list): array
/**
* Create a configuration object from a file.
*
* @param string $fileName Path to configuration file.
* @param string[] $configFiles Array of configuration files with path.
*
* @throws ConfigurationNotFoundException Exception if configuration file was not found.
* @throws ConfigurationParseException Exception if configuration cannot be parsed.
*
* @return Configuration
*/
public function load(string $fileName): Configuration
public function load(array $configFiles): Configuration
{
$configArrays = [];
foreach ($configFiles as $configFile) {
$configArrays[] = $this->parseConfigFile($configFile);
}

$mergedConfig = [
'allowed_licenses' => [],
'allowed_packages' => [],
];

foreach ($configArrays as $configArray) {
$mergedConfig = array_merge_recursive($mergedConfig, $configArray);
}

return new Configuration(
$mergedConfig['allowed_licenses'],
$mergedConfig['allowed_packages'],
);
}

/**
* Reads a single config file and returns the config as array.
*
* @param string $fileName Path to config file.
*
* @throws ConfigurationNotFoundException Exception if configuration file was not found.
* @throws ConfigurationParseException Exception if configuration cannot be parsed.
*
* @return array<string, array<array<string>|string>>
*/
private function parseConfigFile(string $fileName): array
{
if (!file_exists($fileName)) {
throw new ConfigurationNotFoundException(sprintf('Configuration file %s not found.', $fileName));
Expand All @@ -99,17 +131,11 @@ public function load(string $fileName): Configuration
$yaml = Yaml::parseFile($fileName);

$allowedLicences = $this->getAllowedLicenses($yaml[self::KEY_ALLOWED_LICENSES] ?? []);

$allowedPackages = $this->getAllowedPackages($yaml[self::KEY_ALLOWED_PACKAGES] ?? []);

$configuration = new Configuration(
$allowedLicences,
$allowedPackages,
);
} catch (Throwable $e) {
throw new ConfigurationParseException('Configuration file cannot be parsed.', 0, $e);
}

return $configuration;
return ['allowed_licenses' => $allowedLicences, 'allowed_packages' => $allowedPackages];
}
}
2 changes: 1 addition & 1 deletion tests/Unit/CheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testValidate(): void
],
]);

$configuration = (new ConfigurationLoader())->load(__DIR__ . '/../fixtures/configuration/config1.yml');
$configuration = (new ConfigurationLoader())->load([__DIR__ . '/../fixtures/configuration/config1.yml']);

$result = $this->fixture->validate($configuration, $path);

Expand Down
33 changes: 17 additions & 16 deletions tests/Unit/Command/LicenseCheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author best it AG <[email protected]>
* @package BestIt\LicenseCheck\Command
*/
class CommandExceptionTest extends TestCase
class LicenseCheckCommandTest extends TestCase
{
/**
* The test fixture.
Expand Down Expand Up @@ -55,15 +55,15 @@ public function getExecutionDataProvider(): array
[],
0,
getcwd(),
getcwd() . '/license-check.yml',
[getcwd() . '/license-check.yml'],
[],
],
// Actual working directory. Errors not ignores. No custom config. Result has violations.
[
[],
1,
getcwd(),
getcwd() . '/license-check.yml',
[getcwd() . '/license-check.yml'],
['VIOLATION'],
],
// Actual working directory. Errors are ignores. No custom config. Result has violations.
Expand All @@ -73,7 +73,7 @@ public function getExecutionDataProvider(): array
],
0,
getcwd(),
getcwd() . '/license-check.yml',
[getcwd() . '/license-check.yml'],
['VIOLATION'],
],
// Custom working directory. Errors not ignores. No custom config. Result has no violations.
Expand All @@ -83,7 +83,7 @@ public function getExecutionDataProvider(): array
],
0,
'/test-directory',
'/test-directory/license-check.yml',
['/test-directory/license-check.yml'],
[],
],
// Custom working directory. Errors not ignores. No custom config. Result has violations.
Expand All @@ -93,7 +93,7 @@ public function getExecutionDataProvider(): array
],
1,
'/test-directory',
'/test-directory/license-check.yml',
['/test-directory/license-check.yml'],
['VIOLATION'],
],
// Custom working directory. Errors are ignores. No custom config. Result has violations.
Expand All @@ -104,41 +104,41 @@ public function getExecutionDataProvider(): array
],
0,
'/test-directory',
'/test-directory/license-check.yml',
['/test-directory/license-check.yml'],
['VIOLATION'],
],
// Custom working directory. Errors not ignores. Custom config. Result has no violations.
[
[
'directory' => '/test-directory',
'--configuration' => '/testconfig.yml',
'--configuration' => ['/testconfig.yml'],
],
0,
'/test-directory',
'/testconfig.yml',
['/testconfig.yml'],
[],
],
// Custom working directory. Errors not ignores. No custom config. Result has violations.
[
[
'directory' => '/test-directory',
'--configuration' => '/testconfig.yml',
'--configuration' => ['/testconfig.yml'],
],
1,
'/test-directory',
'/testconfig.yml',
['/testconfig.yml'],
['VIOLATION'],
],
// Custom working directory. Errors are ignores. No custom config. Result has violations.
[
[
'directory' => '/test-directory',
'--ignore-errors' => true,
'--configuration' => '/testconfig.yml',
'--configuration' => ['/testconfig.yml'],
],
0,
'/test-directory',
'/testconfig.yml',
['/testconfig.yml'],
['VIOLATION'],
],
];
Expand Down Expand Up @@ -171,6 +171,7 @@ public function testDefinition(): void

self::assertTrue($this->fixture->getDefinition()->hasOption('configuration'));
self::assertTrue($this->fixture->getDefinition()->getOption('configuration')->isValueRequired());
self::assertTrue($this->fixture->getDefinition()->getOption('configuration')->isArray());

self::assertTrue($this->fixture->getDefinition()->hasOption('ignore-errors'));
self::assertFalse($this->fixture->getDefinition()->getOption('ignore-errors')->isValueOptional());
Expand All @@ -185,7 +186,7 @@ public function testDefinition(): void
* @param string[] $input
* @param int $resultCode
* @param string $directory
* @param string $configPath
* @param array $configFiles
* @param string[] $violations
*
* @return void
Expand All @@ -194,13 +195,13 @@ public function testExecution(
array $input,
int $resultCode,
string $directory,
string $configPath,
array $configFiles,
array $violations,
): void {
$this
->configurationLoader
->method('load')
->with($configPath)
->with($configFiles)
->willReturn($configuration = $this->createMock(Configuration::class));

$resultSet = new Result();
Expand Down

0 comments on commit c02190d

Please sign in to comment.