From 6338d762fa37fca59ee754609972938227dc3e3f Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 7 Feb 2023 01:06:31 +0000 Subject: [PATCH] wip --- bin/pest-wrapper.php | 14 +++++++------- src/Plugins/Parallel.php | 10 ++++++---- src/Plugins/Parallel/Handlers/Laravel.php | 6 ++---- src/Plugins/Parallel/Handlers/Parallel.php | 16 +++++++++------- src/Plugins/Parallel/Paratest/WrapperRunner.php | 10 +++++----- src/Plugins/Parallel/Support/CompactPrinter.php | 2 ++ 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/bin/pest-wrapper.php b/bin/pest-wrapper.php index 210e7976..49f21966 100644 --- a/bin/pest-wrapper.php +++ b/bin/pest-wrapper.php @@ -45,13 +45,13 @@ $bootPest = (static function (): void { 'phpunit-argv:', ]); - require_once __DIR__ . '/../overrides/Runner/TestSuiteLoader.php'; - require_once __DIR__ . '/../overrides/Runner/Filter/NameFilterIterator.php'; + require_once __DIR__.'/../overrides/Runner/TestSuiteLoader.php'; + require_once __DIR__.'/../overrides/Runner/Filter/NameFilterIterator.php'; $composerAutoloadFiles = [ - dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'autoload.php', - dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php', - dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php', + dirname(__DIR__, 3).DIRECTORY_SEPARATOR.'autoload.php', + dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php', + dirname(__DIR__).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php', ]; foreach ($composerAutoloadFiles as $file) { @@ -69,8 +69,8 @@ $bootPest = (static function (): void { assert(isset($getopt['progress-file']) && is_string($getopt['progress-file'])); assert(isset($getopt['testresult-file']) && is_string($getopt['testresult-file'])); - assert(!isset($getopt['teamcity-file']) || is_string($getopt['teamcity-file'])); - assert(!isset($getopt['testdox-file']) || is_string($getopt['testdox-file'])); + assert(! isset($getopt['teamcity-file']) || is_string($getopt['teamcity-file'])); + assert(! isset($getopt['testdox-file']) || is_string($getopt['testdox-file'])); assert(isset($getopt['phpunit-argv']) && is_string($getopt['phpunit-argv'])); $phpunitArgv = unserialize($getopt['phpunit-argv'], ['allowed_classes' => false]); diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index 76e69b43..a7fb0ea9 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -17,7 +17,7 @@ final class Parallel implements HandlesArguments { use HandleArguments; - private array $handlers = [ + private const HANDLERS = [ \Pest\Plugins\Parallel\Handlers\Parallel::class, \Pest\Plugins\Parallel\Handlers\Laravel::class, ]; @@ -36,8 +36,10 @@ final class Parallel implements HandlesArguments private function argumentsContainParallelFlags(array $arguments): bool { - return $this->hasArgument('--parallel', $arguments) - || $this->hasArgument('-p', $arguments); + if ($this->hasArgument('--parallel', $arguments)) { + return true; + } + return $this->hasArgument('-p', $arguments); } private function runTestSuiteInParallel(array $arguments): int @@ -49,7 +51,7 @@ final class Parallel implements HandlesArguments } $filteredArguments = array_reduce( - $this->handlers, + self::HANDLERS, fn($arguments, $handler) => (new $handler())->handle($arguments), $arguments ); diff --git a/src/Plugins/Parallel/Handlers/Laravel.php b/src/Plugins/Parallel/Handlers/Laravel.php index 50f34950..9d05a8fd 100644 --- a/src/Plugins/Parallel/Handlers/Laravel.php +++ b/src/Plugins/Parallel/Handlers/Laravel.php @@ -27,7 +27,7 @@ final class Laravel $this->setLaravelParallelRunner(); foreach ($args as $value) { - if (str_starts_with($value, '--runner')) { + if (str_starts_with((string) $value, '--runner')) { $args = $this->popArgument($value, $args); } } @@ -41,9 +41,7 @@ final class Laravel exit('Using parallel with Pest requires Laravel v8.55.0 or higher.'); } - ParallelRunner::resolveRunnerUsing(function (Options $options, OutputInterface $output): RunnerInterface { - return new WrapperRunner($options, $output); - }); + ParallelRunner::resolveRunnerUsing(fn(Options $options, OutputInterface $output): RunnerInterface => new WrapperRunner($options, $output)); } private static function isALaravelApplication(): bool diff --git a/src/Plugins/Parallel/Handlers/Parallel.php b/src/Plugins/Parallel/Handlers/Parallel.php index 0e88c3bb..0ff58839 100644 --- a/src/Plugins/Parallel/Handlers/Parallel.php +++ b/src/Plugins/Parallel/Handlers/Parallel.php @@ -14,16 +14,18 @@ use Symfony\Component\Console\Input\ArgvInput; final class Parallel { use HandleArguments; + /** + * @var string[] + */ + private const ARGS_TO_REMOVE = [ + '--parallel', + '-p', + '--no-output', + ]; public function handle(array $args): array { - $argsToRemove = [ - '--parallel', - '-p', - '--no-output', - ]; - - $args = array_reduce($argsToRemove, fn ($args, $arg) => $this->popArgument($arg, $args), $args); + $args = array_reduce(self::ARGS_TO_REMOVE, fn ($args, $arg): array => $this->popArgument($arg, $args), $args); return $this->pushArgument('--runner=' . WrapperRunner::class, $args); } diff --git a/src/Plugins/Parallel/Paratest/WrapperRunner.php b/src/Plugins/Parallel/Paratest/WrapperRunner.php index 1c0d87c8..9d2dd3b2 100644 --- a/src/Plugins/Parallel/Paratest/WrapperRunner.php +++ b/src/Plugins/Parallel/Paratest/WrapperRunner.php @@ -44,7 +44,7 @@ final class WrapperRunner implements RunnerInterface { private const CYCLE_SLEEP = 10000; private readonly ResultPrinter $printer; - private Timer $timer; + private readonly Timer $timer; /** @var non-empty-string[] */ private array $pending = []; @@ -131,7 +131,7 @@ final class WrapperRunner implements RunnerInterface { $batchSize = $this->options->maxBatchSize; - while (count($this->pending) > 0 && count($this->workers) > 0) { + while ($this->pending !== [] && $this->workers !== []) { foreach ($this->workers as $token => $worker) { if (! $worker->isRunning()) { throw $worker->getWorkerCrashedException(); @@ -178,7 +178,7 @@ final class WrapperRunner implements RunnerInterface private function waitForAllToFinish(): void { $stopped = []; - while (count($this->workers) > 0) { + while ($this->workers !== []) { foreach ($this->workers as $index => $worker) { if ($worker->isRunning()) { if (! isset($stopped[$index]) && $worker->isFree()) { @@ -307,7 +307,7 @@ final class WrapperRunner implements RunnerInterface return $exitcode; } - protected function generateCodeCoverageReports(): void + private function generateCodeCoverageReports(): void { if ($this->coverageFiles === []) { return; @@ -361,7 +361,7 @@ final class WrapperRunner implements RunnerInterface $tests = array_filter( $suiteLoader->files, - fn(string $filename) => ! str_ends_with($filename, "eval()'d code") + fn(string $filename): bool => ! str_ends_with($filename, "eval()'d code") ); return [...$tests, ...TestSuite::getInstance()->tests->getFilenames()]; diff --git a/src/Plugins/Parallel/Support/CompactPrinter.php b/src/Plugins/Parallel/Support/CompactPrinter.php index d5f35f17..f6921a4a 100644 --- a/src/Plugins/Parallel/Support/CompactPrinter.php +++ b/src/Plugins/Parallel/Support/CompactPrinter.php @@ -25,6 +25,8 @@ final class CompactPrinter { private readonly Terminal $terminal; + private readonly ConsoleOutputInterface $output; + private readonly Style $style; private int $compactProcessed = 0;