From 62aabc6ae1fec53da75eaac032eedb26838eba08 Mon Sep 17 00:00:00 2001 From: luke Date: Thu, 5 Aug 2021 17:09:45 +0100 Subject: [PATCH] =?UTF-8?q?Bugfix.=20The=20TestCase=20is=20now=20aware=20o?= =?UTF-8?q?f=20if=20it=20is=20running=20in=20parallel=20or=20not=20?= =?UTF-8?q?=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/pest | 9 +++++++++ src/Console/Paratest/PestRunnerWorker.php | 18 +++++++++--------- src/TestSuite.php | 7 +++++++ tests/Hooks/BeforeAllTest.php | 3 ++- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/bin/pest b/bin/pest index 75faf289..2223cffd 100755 --- a/bin/pest +++ b/bin/pest @@ -35,12 +35,21 @@ use Symfony\Component\Console\Output\OutputInterface; $argv = new ArgvInput(); $testSuite = TestSuite::getInstance($rootPath, $argv->getParameterOption('--test-directory', 'tests')); + $shouldExecuteInParallel = $argv->hasParameterOption('--parallel'); // Let's remove the parallel option now we've retrieved its value if (($parallelKey = array_search('--parallel', $_SERVER['argv'])) !== false) { unset($_SERVER['argv'][$parallelKey]); } + if ($argv->hasParameterOption('--isInParallel')) { + $testSuite->isInParallel = true; + } + // Let's remove the parallel flag now we've retrieved its value + if (($parallelKey = array_search('--isInParallel', $_SERVER['argv'])) !== false) { + unset($_SERVER['argv'][$parallelKey]); + } + $isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never'; $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated); diff --git a/src/Console/Paratest/PestRunnerWorker.php b/src/Console/Paratest/PestRunnerWorker.php index decd2130..b1a4adc9 100644 --- a/src/Console/Paratest/PestRunnerWorker.php +++ b/src/Console/Paratest/PestRunnerWorker.php @@ -40,10 +40,11 @@ final class PestRunnerWorker $args = array_merge( $args, $this->executableTest->commandArguments( - $this->getPestBinary(), + $this->getPestBinary($options), $options->filtered(), $options->passthru() - ) + ), + ['--isInParallel'], ); $this->process = new Process($args, $options->cwd(), $options->fillEnvWithTokens($token)); @@ -109,15 +110,14 @@ final class PestRunnerWorker } } - private function getPestBinary(): string + private function getPestBinary(Options $options): string { - // Used when Pest is required using composer. - $vendorPath = dirname(__DIR__, 7) . '/bin/pest'; + $paths = [ + implode(DIRECTORY_SEPARATOR, [$options->cwd(), 'bin', 'pest']), + implode(DIRECTORY_SEPARATOR, [$options->cwd(), 'vendor', 'bin', 'pest']), + ]; - // Used when Pest maintainers are running Pest tests. - $localPath = dirname(__DIR__, 3) . '/bin/pest'; - - return file_exists($vendorPath) ? $vendorPath : $localPath; + return file_exists($paths[0]) ? $paths[0] : $paths[1]; } public function getWorkerCrashedException(?Throwable $previousException = null): WorkerCrashedException diff --git a/src/TestSuite.php b/src/TestSuite.php index f2b5ccde..2fb0c7de 100644 --- a/src/TestSuite.php +++ b/src/TestSuite.php @@ -73,6 +73,13 @@ final class TestSuite */ public $testPath; + /** + * Determines if this test is running in parallel. + * + * @var bool + */ + public $isInParallel = false; + /** * Holds an instance of the test suite. * diff --git a/tests/Hooks/BeforeAllTest.php b/tests/Hooks/BeforeAllTest.php index 05e57252..c6925103 100644 --- a/tests/Hooks/BeforeAllTest.php +++ b/tests/Hooks/BeforeAllTest.php @@ -1,13 +1,14 @@ calls baseline. This is because // two other tests are executed before this one due to filename ordering. $args = $_SERVER['argv'] ?? []; -$single = isset($args[1]) && Str::endsWith(__FILE__, $args[1]); +$single = isset($args[1]) && Str::endsWith(__FILE__, $args[1]) || TestSuite::getInstance()->isInParallel; $offset = $single ? 0 : 2; uses()->beforeAll(function () use ($globalHook, $offset) {