Skip to content

Commit

Permalink
Update (#68)
Browse files Browse the repository at this point in the history
* Add Castor tasks and update Infection workflow

This commit introduces a new Castor PHP script with different tasks for test execution, mutation testing, coding standards check, PHPStan, and others. The Infection GitHub workflow was also updated to use the new Castor 'infect' task instead of 'make ci-mu'. The 'Infection' workflow name was also corrected.

* Refactor castor validation and integrate tasks in workflows

The code refactors the validation of the composer configuration and autoload dumping in the 'castor.php' file. It also modifies the '.github/workflows/integrate.yml' to replace previous tasks with equivalent 'castor' commands for validation, testing, static analysis, code style checking, Deptrac analysis, and make rector. Additionally, '.castor.stub.php' is added to the '.gitignore' file to prevent it from being tracked.

* Add license check functionality to castor

A new function has been introduced in `castor.php` to check licenses and ensure compliance with defined acceptable licenses. This function has also been integrated into the CI process via the `integrate.yml` GitHub workflow file. It checks license compliance for each dependency before a pull request is merged.
  • Loading branch information
Spomky authored Apr 26, 2024
1 parent 4de637a commit bb73f49
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
/CODE_OF_CONDUCT.md export-ignore
/deptrac.yaml export-ignore
/ecs.php export-ignore
/infection.json.dist export-ignore
/infection.json export-ignore
/Makefile export-ignore
/phpstan.neon export-ignore
/phpstan-baseline.neon export-ignore
/phpunit.xml.dist export-ignore
/rector.php export-ignore
/castor.php export-ignore
5 changes: 3 additions & 2 deletions .github/workflows/infection.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow

name: "Integrate"
name: "Infection"

on:
push:
Expand All @@ -18,6 +18,7 @@ jobs:
php-version: "8.3"
extensions: "ctype, dom, json, libxml, mbstring, openssl, phar, simplexml, tokenizer, xml, xmlwriter"
coverage: "xdebug"
tools: castor

- name: "Checkout code"
uses: "actions/checkout@v3"
Expand All @@ -32,4 +33,4 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Execute Infection"
run: "make ci-mu"
run: "castor infect --ci"
46 changes: 36 additions & 10 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
with:
php-version: "${{ matrix.php-version }}"
extensions: "ctype, dom, json, libxml, mbstring, openssl, phar, simplexml, tokenizer, xml, xmlwriter"
tools: castor
coverage: "xdebug"

- name: "Checkout code"
Expand All @@ -80,7 +81,7 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Execute tests (PHP)"
run: "make ci-cc"
run: "castor test --coverage-text"

# - name: Send coverage to Coveralls
# if: "matrix.php-version == '8.1' && matrix.dependencies == 'highest'"
Expand All @@ -102,25 +103,23 @@ jobs:
with:
php-version: "8.3"
extensions: "ctype, dom, json, libxml, mbstring, openssl, phar, simplexml, tokenizer, xml, xmlwriter"
tools: castor
coverage: "none"

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Validate Composer configuration"
run: "composer validate --strict"
run: "castor validate"

- name: "Install dependencies"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "highest"
composer-options: "--optimize-autoloader"

- name: "Check PSR-4 mapping"
run: "composer dump-autoload --optimize --strict-psr"

- name: "Execute static analysis"
run: "make st"
run: "castor stan"

coding_standards:
name: "4️⃣ Coding Standards"
Expand All @@ -134,6 +133,7 @@ jobs:
with:
php-version: "8.3"
extensions: "ctype, dom, json, libxml, mbstring, openssl, phar, simplexml, tokenizer, xml, xmlwriter"
tools: castor
coverage: "none"

- name: "Checkout code"
Expand All @@ -146,11 +146,36 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Check coding style"
run: "make ci-cs"
run: "castor cs"

- name: "Deptrac"
run: |
vendor/bin/deptrac analyse --fail-on-uncovered --no-cache
run: 'castor deptrac'

check_licenses:
name: "5️⃣ Check Licenses"
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.3"
extensions: "ctype, dom, json, libxml, mbstring, openssl, phar, simplexml, tokenizer, xml, xmlwriter"
tools: castor

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Install dependencies"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "highest"
composer-options: "--optimize-autoloader"

- name: "Execute license check"
run: "castor check-licenses"

rector_checkstyle:
name: "6️⃣ Rector Checkstyle"
Expand All @@ -164,6 +189,7 @@ jobs:
with:
php-version: "8.3"
extensions: "ctype, dom, json, libxml, mbstring, openssl, phar, simplexml, tokenizer, xml, xmlwriter"
tools: castor
coverage: "xdebug"

- name: "Checkout code"
Expand All @@ -179,7 +205,7 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Execute Rector"
run: "make rector"
run: "castor rector"

exported_files:
name: "7️⃣ Exported files"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ yarn-error.log
/composer.lock
/vendor
infection.txt
/.castor.stub.php
178 changes: 178 additions & 0 deletions castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

declare(strict_types=1);

use Castor\Attribute\AsTask;
use function Castor\io;
use function Castor\run;

#[AsTask(description: 'Run mutation testing')]
function infect(int $minMsi = 0, int $minCoveredMsi = 0, bool $ci = false): void
{
io()->title('Running infection');
$nproc = run('nproc', quiet: true);
if (! $nproc->isSuccessful()) {
io()->error('Cannot determine the number of processors');
return;
}
$threads = (int) $nproc->getOutput();
$command = [
'php',
'vendor/bin/infection',
sprintf('--min-msi=%s', $minMsi),
sprintf('--min-covered-msi=%s', $minCoveredMsi),
sprintf('--threads=%s', $threads),
];
if ($ci) {
$command[] = '--logger-github';
$command[] = '-s';
}
$environment = [
'XDEBUG_MODE' => 'coverage',
];
run($command, environment: $environment);
}

#[AsTask(description: 'Run tests')]
function test(bool $coverageHtml = false, bool $coverageText = false, null|string $group = null): void
{
io()->title('Running tests');
$command = ['php', 'vendor/bin/phpunit', '--color'];
$environment = [
'XDEBUG_MODE' => 'off',
];
if ($coverageHtml) {
$command[] = '--coverage-html=build/coverage';
$environment['XDEBUG_MODE'] = 'coverage';
}
if ($coverageText) {
$command[] = '--coverage-text';
$environment['XDEBUG_MODE'] = 'coverage';
}
if ($group !== null) {
$command[] = sprintf('--group=%s', $group);
}
run($command, environment: $environment);
}

#[AsTask(description: 'Coding standards check')]
function cs(bool $fix = false): void
{
io()->title('Running coding standards check');
$command = ['php', 'vendor/bin/ecs', 'check'];
$environment = [
'XDEBUG_MODE' => 'off',
];
if ($fix) {
$command[] = '--fix';
}
run($command, environment: $environment);
}

#[AsTask(description: 'Running PHPStan')]
function stan(): void
{
io()->title('Running PHPStan');
$command = ['php', 'vendor/bin/phpstan', 'analyse'];
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);
}

#[AsTask(description: 'Validate Composer configuration')]
function validate(): void
{
io()->title('Validating Composer configuration');
$command = ['composer', 'validate', '--strict'];
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);

$command = ['composer', 'dump-autoload', '--optimize', '--strict-psr'];
run($command, environment: $environment);
}

/**
* @param array<string> $allowedLicenses
*/
#[AsTask(description: 'Check licenses')]
function checkLicenses(
array $allowedLicenses = ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'ISC', 'MIT', 'MPL-2.0', 'OSL-3.0']
): void {
io()->title('Checking licenses');
$allowedExceptions = [];
$command = ['composer', 'licenses', '-f', 'json'];
$environment = [
'XDEBUG_MODE' => 'off',
];
$result = run($command, environment: $environment, quiet: true);
if (! $result->isSuccessful()) {
io()->error('Cannot determine licenses');
exit(1);
}
$licenses = json_decode($result->getOutput(), true);
$disallowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => ! in_array($name, $allowedExceptions, true)
&& count(array_diff($info['license'], $allowedLicenses)) === 1,
\ARRAY_FILTER_USE_BOTH
);
$allowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => in_array($name, $allowedExceptions, true)
|| count(array_diff($info['license'], $allowedLicenses)) === 0,
\ARRAY_FILTER_USE_BOTH
);
if (count($disallowed) > 0) {
io()
->table(
['Package', 'License'],
array_map(
static fn ($name, $info) => [$name, implode(', ', $info['license'])],
array_keys($disallowed),
$disallowed
)
);
io()
->error('Disallowed licenses found');
exit(1);
}
io()
->table(
['Package', 'License'],
array_map(
static fn ($name, $info) => [$name, implode(', ', $info['license'])],
array_keys($allowed),
$allowed
)
);
io()
->success('All licenses are allowed');
}

#[AsTask(description: 'Run Rector')]
function rector(bool $fix = false): void
{
io()->title('Running Rector');
$command = ['php', 'vendor/bin/rector', 'process', '--ansi'];
if (! $fix) {
$command[] = '--dry-run';
}
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);
}

#[AsTask(description: 'Run Rector')]
function deptrac(): void
{
io()->title('Running Rector');
$command = ['php', 'vendor/bin/deptrac', 'analyse', '--fail-on-uncovered', '--no-cache'];
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);
}
8 changes: 7 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
$config->parallel();
$config->paths([__DIR__]);
$config->skip(
[__DIR__ . '/.github', __DIR__ . '/build', __DIR__ . '/vendor', PhpUnitTestClassRequiresCoversFixer::class]
[
__DIR__ . '/.github',
__DIR__ . '/build',
__DIR__ . '/vendor',
__DIR__ . '/.castor.stub.php',
PhpUnitTestClassRequiresCoversFixer::class,
]
);
};
File renamed without changes.

0 comments on commit bb73f49

Please sign in to comment.