Adds output about sharding

This commit is contained in:
Nuno Maduro
2025-07-05 15:43:43 +01:00
parent 73bf579da3
commit 0f1e87c726
2 changed files with 58 additions and 5 deletions

View File

@ -73,9 +73,9 @@
], ],
"scripts": { "scripts": {
"refacto": "rector", "refacto": "rector",
"lint": "pint", "lint": "pint --parallel",
"test:refacto": "rector --dry-run", "test:refacto": "rector --dry-run",
"test:lint": "pint --test", "test:lint": "pint --parallel --test",
"test:profanity": "php bin/pest --profanity --compact --language=en", "test:profanity": "php bin/pest --profanity --compact --language=en",
"test:type:check": "phpstan analyse --ansi --memory-limit=-1 --debug", "test:type:check": "phpstan analyse --ansi --memory-limit=-1 --debug",
"test:type:coverage": "php -d memory_limit=-1 bin/pest --type-coverage --min=100", "test:type:coverage": "php -d memory_limit=-1 bin/pest --type-coverage --min=100",

View File

@ -4,25 +4,43 @@ declare(strict_types=1);
namespace Pest\Plugins; namespace Pest\Plugins;
use Pest\Contracts\Plugins\AddsOutput;
use Pest\Contracts\Plugins\HandlesArguments; use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Exceptions\InvalidOption; use Pest\Exceptions\InvalidOption;
use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
/** /**
* @internal * @internal
*/ */
final class Shard implements HandlesArguments final class Shard implements AddsOutput, HandlesArguments
{ {
use Concerns\HandleArguments; use Concerns\HandleArguments;
private const string SHARD_OPTION = 'shard'; 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} * {@inheritDoc}
@ -47,6 +65,13 @@ final class Shard implements HandlesArguments
$tests = $this->allTests($arguments); $tests = $this->allTests($arguments);
$testsToRun = (array_chunk($tests, max(1, (int) ceil(count($tests) / $total))))[$index - 1] ?? []; $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)]; return [...$arguments, '--filter', $this->buildFilterArgument($testsToRun)];
} }
@ -86,6 +111,34 @@ final class Shard implements HandlesArguments
return addslashes(implode('|', $testsToRun)); 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(
' <fg=gray>Shard:</> <fg=default>%d of %d</> — %d file%s ran, out of %d.',
$index,
$total,
$testsRan,
$testsRan === 1 ? '' : 's',
$testsCount,
));
return $exitCode;
}
/** /**
* Returns the shard information. * Returns the shard information.
* *