chore(cleanup): Tidy-up and tweaks of Pest Parallel integration.

This commit is contained in:
Luke Downing
2023-02-13 12:13:53 +00:00
parent 2bc33c7cd5
commit 666b2f3fd0
4 changed files with 55 additions and 38 deletions

File diff suppressed because one or more lines are too long

View File

@ -7,7 +7,6 @@ use ParaTest\WrapperRunner\WrapperWorker;
use Pest\ConfigLoader;
use Pest\Kernel;
use Pest\Plugins\Actions\CallsHandleArguments;
use Pest\TestCaseMethodFilters\TodoTestCaseFilter;
use Pest\TestSuite;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
@ -15,7 +14,6 @@ use Symfony\Component\Console\Output\OutputInterface;
$bootPest = (static function (): void {
$workerArgv = new ArgvInput();
$masterArgv = new ArgvInput(json_decode($_SERVER['PEST_PARALLEL_ARGV']));
$rootPath = dirname(PHPUNIT_COMPOSER_INSTALL, 2);
$testSuite = TestSuite::getInstance($rootPath, $workerArgv->getParameterOption(
@ -23,10 +21,6 @@ $bootPest = (static function (): void {
(new ConfigLoader($rootPath))->getTestsDirectory()
));
if ($masterArgv->hasParameterOption('--todo')) {
$testSuite->tests->addTestCaseMethodFilter(new TodoTestCaseFilter());
}
$input = new ArgvInput();
$output = new ConsoleOutput(OutputInterface::VERBOSITY_NORMAL, true);

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Pest\Plugins;
use JsonException;
use ParaTest\ParaTestCommand;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Plugins\Actions\CallsAddsOutput;
@ -30,6 +29,16 @@ final class Parallel implements HandlesArguments
Parallel\Handlers\Laravel::class,
];
/**
* Whether the given command line arguments indicate that the test suite should be run in parallel.
*/
public static function isEnabled(): bool
{
$argv = new ArgvInput();
return $argv->hasParameterOption('--parallel') || $argv->hasParameterOption('-p');
}
/**
* If this code is running in a worker process rather than the main process.
*/
@ -47,7 +56,11 @@ final class Parallel implements HandlesArguments
*/
public function handleArguments(array $arguments): array
{
if ($this->argumentsContainParallelOptions($arguments)) {
if ($this->hasArgumentsThatWouldBeFasterWithoutParallel()) {
return $this->runTestSuiteInSeries($arguments);
}
if (self::isEnabled()) {
exit($this->runTestSuiteInParallel($arguments));
}
@ -58,26 +71,10 @@ final class Parallel implements HandlesArguments
return $arguments;
}
/**
* Whether the given command line arguments indicate that the test suite should be run in parallel.
*
* @param array<int, string> $arguments
*/
private function argumentsContainParallelOptions(array $arguments): bool
{
if ($this->hasArgument('--parallel', $arguments)) {
return true;
}
return $this->hasArgument('-p', $arguments);
}
/**
* Runs the test suite in parallel. This method will exit the process upon completion.
*
* @param array<int, string> $arguments
*
* @throws JsonException
*/
private function runTestSuiteInParallel(array $arguments): int
{
@ -87,8 +84,6 @@ final class Parallel implements HandlesArguments
return Command::FAILURE;
}
$_ENV['PEST_PARALLEL_ARGV'] = json_encode($_SERVER['argv'], JSON_THROW_ON_ERROR);
$handlers = array_filter(
array_map(fn ($handler): object|string => Container::getInstance()->get($handler), self::HANDLERS),
fn ($handler): bool => $handler instanceof HandlesArguments,
@ -154,4 +149,35 @@ final class Parallel implements HandlesArguments
return $command;
}
/**
* Whether the command line arguments contain any arguments that are
* not supported or are suboptimal when running in parallel.
*/
private function hasArgumentsThatWouldBeFasterWithoutParallel(): bool
{
$unsupportedArguments = ['--todo', '--retry'];
$arguments = new ArgvInput();
foreach ($unsupportedArguments as $unsupportedArgument) {
if ($arguments->hasParameterOption($unsupportedArgument)) {
return true;
}
}
return false;
}
/**
* Removes any parallel arguments.
*
* @param array<int, string> $arguments
* @return array<int, string>
*/
private function runTestSuiteInSeries(array $arguments): array
{
$arguments = $this->popArgument('--parallel', $arguments);
return $this->popArgument('-p', $arguments);
}
}

View File

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Exceptions\InvalidOption;
/**
* @internal
@ -19,18 +18,16 @@ final class Retry implements HandlesArguments
*/
public function handleArguments(array $arguments): array
{
if ($this->hasArgument('--retry', $arguments)) {
if ($this->hasArgument('--parallel', $arguments)) {
throw new InvalidOption('The --retry option is not supported when running in parallel.');
}
$arguments = $this->popArgument('--retry', $arguments);
$arguments = $this->pushArgument('--order-by=defects', $arguments);
$arguments = $this->pushArgument('--stop-on-failure', $arguments);
if (! $this->hasArgument('--retry', $arguments)) {
return $arguments;
}
$arguments = $this->popArgument('--retry', $arguments);
$arguments = $this->pushArgument('--order-by=defects', $arguments);
$arguments = $this->pushArgument('--stop-on-failure', $arguments);
return $arguments;
}
}