From 221248e691b91e61d9d5bf948b0738ae6a962d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Fri, 6 Aug 2021 15:25:28 +0200 Subject: [PATCH 1/5] introduced argument mapping, added pest coverage --- bin/pest | 24 ++------ src/Actions/MapArguments.php | 102 ++++++++++++++++++++++++++++++++ src/Console/Paratest/Runner.php | 7 +++ 3 files changed, 114 insertions(+), 19 deletions(-) create mode 100644 src/Actions/MapArguments.php diff --git a/bin/pest b/bin/pest index 2223cffd..f2e14f32 100755 --- a/bin/pest +++ b/bin/pest @@ -4,9 +4,9 @@ use NunoMaduro\Collision\Provider; use ParaTest\Console\Commands\ParaTestCommand; use Pest\Actions\LoadStructure; +use Pest\Actions\MapArguments; use Pest\Actions\ValidatesEnvironment; use Pest\Console\Command; -use Pest\Console\Paratest\Runner; use Pest\Support\Container; use Pest\TestSuite; use Symfony\Component\Console\Input\ArgvInput; @@ -36,20 +36,6 @@ use Symfony\Component\Console\Output\OutputInterface; $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); @@ -68,13 +54,13 @@ use Symfony\Component\Console\Output\OutputInterface; } } - if ($shouldExecuteInParallel) { - $_SERVER['argv'][] = '--runner'; - $_SERVER['argv'][] = Runner::class; - + if ($argv->hasParameterOption('--parallel')) { LoadStructure::in($testSuite->rootPath); + MapArguments::toParatest($testSuite); exit(ParaTestCommand::applicationFactory($testSuite->rootPath)->run(new ArgvInput())); } + MapArguments::toPest($testSuite); + exit($container->get(Command::class)->run($_SERVER['argv'])); })(); diff --git a/src/Actions/MapArguments.php b/src/Actions/MapArguments.php new file mode 100644 index 00000000..11886f46 --- /dev/null +++ b/src/Actions/MapArguments.php @@ -0,0 +1,102 @@ +handleArguments($_SERVER['argv']); + } + } + + private static function parallel(): void + { + if (self::unsetArgument('--parallel')) { + self::setArgument('--runner', Runner::class); + } + } + + private static function inParallel(TestSuite $testSuite): void + { + if (self::unsetArgument('--isInParallel')) { + $testSuite->isInParallel = true; + } + } + + private static function color(): void + { + $argv = new ArgvInput(); + $isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never'; + + self::unsetArgument('--colors'); + //refactor later + self::unsetArgument('--colors=always'); + self::unsetArgument('--colors=auto'); + self::unsetArgument('--colors=never'); + + if ($isDecorated) { + self::setArgument('--colors'); + } + } + + private static function coverage(): void + { + if (! Coverage::isAvailable()) { + Container::getInstance()->get(OutputInterface::class)->writeln( + "\n ERROR No code coverage driver is available.", + ); + exit(1); + } + } + + private static function unsetArgument(string $argument): bool + { + if (($key = array_search($argument, $_SERVER['argv'])) !== false) { + unset($_SERVER['argv'][$key]); + + return true; + } + + return false; + } + + private static function setArgument(string $argument, string $value = null): void + { + $_SERVER['argv'][] = $argument; + + if ($value !== null) { + $_SERVER['argv'][] = $value; + } + } +} diff --git a/src/Console/Paratest/Runner.php b/src/Console/Paratest/Runner.php index 2a19e050..138b5e3b 100644 --- a/src/Console/Paratest/Runner.php +++ b/src/Console/Paratest/Runner.php @@ -14,6 +14,7 @@ use ParaTest\Runners\PHPUnit\ResultPrinter; use ParaTest\Runners\PHPUnit\RunnerInterface; use ParaTest\Runners\PHPUnit\SuiteLoader; use Pest\Factories\TestCaseFactory; +use Pest\Plugins\Coverage; use Pest\TestSuite; use PHPUnit\TextUI\TestRunner; use SebastianBergmann\Timer\Timer; @@ -227,6 +228,12 @@ final class Runner implements RunnerInterface $this->output->writeln( sprintf('done [%s]', $timer->stop()->asString()) ); + + if ($this->options->coveragePhp() && file_exists(\Pest\Support\Coverage::getPath())) { + $coveragePlugin = new Coverage($this->output); + $coveragePlugin->coverage = true; + $coveragePlugin->addOutput(0); + } } private function hasCoverage(): bool From 8b295b5e9d53a71115e2ed10704851a77fb11a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Fri, 6 Aug 2021 15:38:59 +0200 Subject: [PATCH 2/5] remove debug statements --- src/Actions/MapArguments.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Actions/MapArguments.php b/src/Actions/MapArguments.php index 11886f46..16b1e0f7 100644 --- a/src/Actions/MapArguments.php +++ b/src/Actions/MapArguments.php @@ -17,12 +17,10 @@ final class MapArguments { public static function toParatest(TestSuite $testSuite): void { - var_dump($_SERVER['argv']); self::coverage(); self::registerPlugins(); self::parallel(); self::color(); - var_dump($_SERVER['argv']); } public static function toPest(TestSuite $testSuite): void From c86058fed1da4f924eda8c843f44d46b4bfb4c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Fri, 6 Aug 2021 15:39:19 +0200 Subject: [PATCH 3/5] use support class --- src/Console/Paratest/Runner.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Console/Paratest/Runner.php b/src/Console/Paratest/Runner.php index 138b5e3b..7c7b4b28 100644 --- a/src/Console/Paratest/Runner.php +++ b/src/Console/Paratest/Runner.php @@ -14,7 +14,7 @@ use ParaTest\Runners\PHPUnit\ResultPrinter; use ParaTest\Runners\PHPUnit\RunnerInterface; use ParaTest\Runners\PHPUnit\SuiteLoader; use Pest\Factories\TestCaseFactory; -use Pest\Plugins\Coverage; +use Pest\Support\Coverage; use Pest\TestSuite; use PHPUnit\TextUI\TestRunner; use SebastianBergmann\Timer\Timer; @@ -229,10 +229,8 @@ final class Runner implements RunnerInterface sprintf('done [%s]', $timer->stop()->asString()) ); - if ($this->options->coveragePhp() && file_exists(\Pest\Support\Coverage::getPath())) { - $coveragePlugin = new Coverage($this->output); - $coveragePlugin->coverage = true; - $coveragePlugin->addOutput(0); + if ($this->options->coveragePhp() && file_exists(Coverage::getPath())) { + Coverage::report($this->output); } } From 0b5321fdd7ecdafc19e44bdb1c93fbf62279c5c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Fri, 6 Aug 2021 15:48:38 +0200 Subject: [PATCH 4/5] only check for coverage driver if option is present --- src/Actions/MapArguments.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Actions/MapArguments.php b/src/Actions/MapArguments.php index 16b1e0f7..57127325 100644 --- a/src/Actions/MapArguments.php +++ b/src/Actions/MapArguments.php @@ -17,8 +17,8 @@ final class MapArguments { public static function toParatest(TestSuite $testSuite): void { - self::coverage(); self::registerPlugins(); + self::coverage(); self::parallel(); self::color(); } @@ -26,6 +26,7 @@ final class MapArguments public static function toPest(TestSuite $testSuite): void { self::inParallel($testSuite); + // we could add coverage here too, so we stop before even running tests if there is no coverage driver } private static function registerPlugins(): void @@ -70,7 +71,7 @@ final class MapArguments private static function coverage(): void { - if (! Coverage::isAvailable()) { + if (self::needsCoverage() && ! Coverage::isAvailable()) { Container::getInstance()->get(OutputInterface::class)->writeln( "\n ERROR No code coverage driver is available.", ); @@ -78,6 +79,17 @@ final class MapArguments } } + private static function needsCoverage(): bool + { + foreach ($_SERVER['argv'] as $argument) { + if(str_starts_with($argument, '--coverage')) { + return true; + } + } + + return false; + } + private static function unsetArgument(string $argument): bool { if (($key = array_search($argument, $_SERVER['argv'])) !== false) { From 721d5134b7dbebdccc5f856121ed5f0a7a300de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Fri, 6 Aug 2021 16:00:03 +0200 Subject: [PATCH 5/5] replace `str_starts_with` to support pre php 8.0 --- src/Actions/MapArguments.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Actions/MapArguments.php b/src/Actions/MapArguments.php index 57127325..f38d6668 100644 --- a/src/Actions/MapArguments.php +++ b/src/Actions/MapArguments.php @@ -82,7 +82,7 @@ final class MapArguments private static function needsCoverage(): bool { foreach ($_SERVER['argv'] as $argument) { - if(str_starts_with($argument, '--coverage')) { + if(strpos($argument, '--coverage',) === 0) { return true; } }