From 0f1e87c726c74659c673ac652c7923d4bd8b7607 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sat, 5 Jul 2025 15:43:43 +0100 Subject: [PATCH] Adds output about sharding --- composer.json | 4 +-- src/Plugins/Shard.php | 59 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index df8febf7..21f36c73 100644 --- a/composer.json +++ b/composer.json @@ -73,9 +73,9 @@ ], "scripts": { "refacto": "rector", - "lint": "pint", + "lint": "pint --parallel", "test:refacto": "rector --dry-run", - "test:lint": "pint --test", + "test:lint": "pint --parallel --test", "test:profanity": "php bin/pest --profanity --compact --language=en", "test:type:check": "phpstan analyse --ansi --memory-limit=-1 --debug", "test:type:coverage": "php -d memory_limit=-1 bin/pest --type-coverage --min=100", diff --git a/src/Plugins/Shard.php b/src/Plugins/Shard.php index 9ac2c436..f48260bb 100644 --- a/src/Plugins/Shard.php +++ b/src/Plugins/Shard.php @@ -4,25 +4,43 @@ declare(strict_types=1); namespace Pest\Plugins; +use Pest\Contracts\Plugins\AddsOutput; use Pest\Contracts\Plugins\HandlesArguments; use Pest\Exceptions\InvalidOption; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Process; /** * @internal */ -final class Shard implements HandlesArguments +final class Shard implements AddsOutput, HandlesArguments { use Concerns\HandleArguments; private const string SHARD_OPTION = 'shard'; /** - * The total number of tests. + * The shard index and total number of shards. + * + * @var array{ + * index: int, + * total: int, + * testsRan: int, + * testsCount: int + * }|null */ - public static int $testsCount = 0; + private static ?array $shard = null; + + /** + * Creates a new Plugin instance. + */ + public function __construct( + private readonly OutputInterface $output, + ) { + // + } /** * {@inheritDoc} @@ -47,6 +65,13 @@ final class Shard implements HandlesArguments $tests = $this->allTests($arguments); $testsToRun = (array_chunk($tests, max(1, (int) ceil(count($tests) / $total))))[$index - 1] ?? []; + self::$shard = [ + 'index' => $index, + 'total' => $total, + 'testsRan' => count($testsToRun), + 'testsCount' => count($tests), + ]; + return [...$arguments, '--filter', $this->buildFilterArgument($testsToRun)]; } @@ -86,6 +111,34 @@ final class Shard implements HandlesArguments return addslashes(implode('|', $testsToRun)); } + /** + * Adds output after the Test Suite execution. + */ + public function addOutput(int $exitCode): int + { + if (self::$shard === null) { + return $exitCode; + } + + [ + 'index' => $index, + 'total' => $total, + 'testsRan' => $testsRan, + 'testsCount' => $testsCount, + ] = self::$shard; + + $this->output->writeln(sprintf( + ' Shard: %d of %d — %d file%s ran, out of %d.', + $index, + $total, + $testsRan, + $testsRan === 1 ? '' : 's', + $testsCount, + )); + + return $exitCode; + } + /** * Returns the shard information. *