From b6fb81e506a4c752c0ac9c26b8a764e9c1ffb8ba Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 13 Feb 2023 03:08:36 +0000 Subject: [PATCH] refactor: parallel --- src/Plugins/Cache.php | 5 +- src/Plugins/Parallel/Handlers/Parallel.php | 5 +- src/Plugins/Parallel/Handlers/Pest.php | 3 ++ .../Parallel/Paratest/CleanConsoleOutput.php | 3 +- .../Parallel/Paratest/ResultPrinter.php | 21 ++++----- .../Parallel/Paratest/WrapperRunner.php | 3 +- .../Parallel/Support/CompactPrinter.php | 47 ++++++++++++------- 7 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/Plugins/Cache.php b/src/Plugins/Cache.php index 363ce20c..f1929b9d 100644 --- a/src/Plugins/Cache.php +++ b/src/Plugins/Cache.php @@ -14,6 +14,9 @@ final class Cache implements HandlesArguments { use HandleArguments; + /** + * The temporary folder. + */ private const TEMPORARY_FOLDER = __DIR__ .DIRECTORY_SEPARATOR .'..' @@ -23,7 +26,7 @@ final class Cache implements HandlesArguments .'.temp'; /** - * {@inheritdoc} + * Handles the arguments, adding the cache directory and the cache result arguments. */ public function handleArguments(array $arguments): array { diff --git a/src/Plugins/Parallel/Handlers/Parallel.php b/src/Plugins/Parallel/Handlers/Parallel.php index 71f6d4d6..76829b60 100644 --- a/src/Plugins/Parallel/Handlers/Parallel.php +++ b/src/Plugins/Parallel/Handlers/Parallel.php @@ -16,7 +16,7 @@ final class Parallel implements HandlesArguments use HandleArguments; /** - * @var string[] + * The list of arguments to remove. */ private const ARGS_TO_REMOVE = [ '--parallel', @@ -24,6 +24,9 @@ final class Parallel implements HandlesArguments '--no-output', ]; + /** + * Handles the arguments, removing the ones that are not needed, and adding the "runner" argument. + */ public function handleArguments(array $arguments): array { $args = array_reduce(self::ARGS_TO_REMOVE, fn ($args, $arg): array => $this->popArgument($arg, $args), $arguments); diff --git a/src/Plugins/Parallel/Handlers/Pest.php b/src/Plugins/Parallel/Handlers/Pest.php index 6dd64b36..eb9132b7 100644 --- a/src/Plugins/Parallel/Handlers/Pest.php +++ b/src/Plugins/Parallel/Handlers/Pest.php @@ -11,6 +11,9 @@ final class Pest implements HandlersWorkerArguments { use HandleArguments; + /** + * Handles the arguments, adding the "PEST_PARALLEL" environment variable to the global $_SERVER. + */ public function handleWorkerArguments(array $arguments): array { $_SERVER['PEST_PARALLEL'] = '1'; diff --git a/src/Plugins/Parallel/Paratest/CleanConsoleOutput.php b/src/Plugins/Parallel/Paratest/CleanConsoleOutput.php index 90858267..2e91ac4c 100644 --- a/src/Plugins/Parallel/Paratest/CleanConsoleOutput.php +++ b/src/Plugins/Parallel/Paratest/CleanConsoleOutput.php @@ -21,8 +21,7 @@ final class CleanConsoleOutput extends ConsoleOutput } /** - * Determines if the given message is the descriptive message - * that Paratest outputs when it starts. + * Removes the opening headline, witch is not needed. */ private function isOpeningHeadline(string $message): bool { diff --git a/src/Plugins/Parallel/Paratest/ResultPrinter.php b/src/Plugins/Parallel/Paratest/ResultPrinter.php index 49f83ec3..c34e5616 100644 --- a/src/Plugins/Parallel/Paratest/ResultPrinter.php +++ b/src/Plugins/Parallel/Paratest/ResultPrinter.php @@ -28,12 +28,16 @@ use Symfony\Component\Console\Output\OutputInterface; /** @internal */ final class ResultPrinter { + /** + * The "native" printer. + */ public readonly Printer $printer; + /** + * The "compact" printer. + */ private readonly CompactPrinter $compactPrinter; - private int $totalCases = 0; - /** @var resource|null */ private $teamcityLogFileHandle; @@ -61,7 +65,7 @@ final class ResultPrinter } }; - $this->compactPrinter = new CompactPrinter(); + $this->compactPrinter = CompactPrinter::default(); if (! $this->options->configuration->hasLogfileTeamcity()) { return; @@ -72,17 +76,12 @@ final class ResultPrinter $this->teamcityLogFileHandle = $teamcityLogFileHandle; } - public function setTestCount(int $testCount): void - { - $this->totalCases = $testCount; - } - - public function start(): void + public function start(int $numberOfTests): void { $this->compactPrinter->line(sprintf( 'Running %d test%s using %d process%s', - $this->totalCases, - $this->totalCases === 1 ? '' : 's', + $numberOfTests, + $numberOfTests === 1 ? '' : 's', $this->options->processes, $this->options->processes === 1 ? '' : 'es') ); diff --git a/src/Plugins/Parallel/Paratest/WrapperRunner.php b/src/Plugins/Parallel/Paratest/WrapperRunner.php index 8d100f98..e3426363 100644 --- a/src/Plugins/Parallel/Paratest/WrapperRunner.php +++ b/src/Plugins/Parallel/Paratest/WrapperRunner.php @@ -121,8 +121,7 @@ final class WrapperRunner implements RunnerInterface $result = TestResultFacade::result(); - $this->printer->setTestCount($suiteLoader->testCount); - $this->printer->start(); + $this->printer->start($suiteLoader->testCount); $this->timer->start(); diff --git a/src/Plugins/Parallel/Support/CompactPrinter.php b/src/Plugins/Parallel/Support/CompactPrinter.php index f6921a4a..122af350 100644 --- a/src/Plugins/Parallel/Support/CompactPrinter.php +++ b/src/Plugins/Parallel/Support/CompactPrinter.php @@ -13,7 +13,7 @@ use PHPUnit\Event\Telemetry\Snapshot; use PHPUnit\TestRunner\TestResult\TestResult as PHPUnitTestResult; use SebastianBergmann\Timer\Duration; use Symfony\Component\Console\Output\ConsoleOutput; -use Symfony\Component\Console\Output\ConsoleOutputInterface; +use Symfony\Component\Console\Output\OutputInterface; use function Termwind\render; use Termwind\Terminal; use function Termwind\terminal; @@ -23,15 +23,10 @@ use function Termwind\terminal; */ final class CompactPrinter { - private readonly Terminal $terminal; - - private readonly ConsoleOutputInterface $output; - - private readonly Style $style; - - private int $compactProcessed = 0; - - private readonly int $compactSymbolsPerLine; + /** + * The number of processed tests. + */ + private int $processed = 0; /** * @var array> @@ -47,13 +42,29 @@ final class CompactPrinter 'F' => ['red', 'тип'], ]; - public function __construct() - { - $this->terminal = terminal(); - $this->output = new ConsoleOutput(decorated: true); - $this->style = new Style($this->output); + /** + * Creates a new instance of the Compact Printer. + */ + public function __construct( + private readonly Terminal $terminal, + private readonly OutputInterface $output, + private readonly Style $style, + private readonly int $compactSymbolsPerLine, + ) { + // .. + } - $this->compactSymbolsPerLine = $this->terminal->width() - 4; + /** + * Creates a new instance of the Compact Printer. + */ + public static function default(): self + { + return new self( + terminal(), + new ConsoleOutput(decorated: true), + new Style(new ConsoleOutput(decorated: true)), + terminal()->width() - 4, + ); } /** @@ -79,7 +90,7 @@ final class CompactPrinter { [$color, $icon] = self::LOOKUP_TABLE[$item] ?? self::LOOKUP_TABLE['.']; - $symbolsOnCurrentLine = $this->compactProcessed % $this->compactSymbolsPerLine; + $symbolsOnCurrentLine = $this->processed % $this->compactSymbolsPerLine; if ($symbolsOnCurrentLine >= $this->terminal->width() - 4) { $symbolsOnCurrentLine = 0; @@ -92,7 +103,7 @@ final class CompactPrinter $this->output->write(sprintf('%s', $color, $icon)); - $this->compactProcessed++; + $this->processed++; } /**