introduced argument mapping, added pest coverage

This commit is contained in:
Adrian Nürnberger
2021-08-06 15:25:28 +02:00
parent 7621247bb7
commit 221248e691
3 changed files with 114 additions and 19 deletions

View File

@ -4,9 +4,9 @@
use NunoMaduro\Collision\Provider; use NunoMaduro\Collision\Provider;
use ParaTest\Console\Commands\ParaTestCommand; use ParaTest\Console\Commands\ParaTestCommand;
use Pest\Actions\LoadStructure; use Pest\Actions\LoadStructure;
use Pest\Actions\MapArguments;
use Pest\Actions\ValidatesEnvironment; use Pest\Actions\ValidatesEnvironment;
use Pest\Console\Command; use Pest\Console\Command;
use Pest\Console\Paratest\Runner;
use Pest\Support\Container; use Pest\Support\Container;
use Pest\TestSuite; use Pest\TestSuite;
use Symfony\Component\Console\Input\ArgvInput; 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')); $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'; $isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never';
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated); $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated);
@ -68,13 +54,13 @@ use Symfony\Component\Console\Output\OutputInterface;
} }
} }
if ($shouldExecuteInParallel) { if ($argv->hasParameterOption('--parallel')) {
$_SERVER['argv'][] = '--runner';
$_SERVER['argv'][] = Runner::class;
LoadStructure::in($testSuite->rootPath); LoadStructure::in($testSuite->rootPath);
MapArguments::toParatest($testSuite);
exit(ParaTestCommand::applicationFactory($testSuite->rootPath)->run(new ArgvInput())); exit(ParaTestCommand::applicationFactory($testSuite->rootPath)->run(new ArgvInput()));
} }
MapArguments::toPest($testSuite);
exit($container->get(Command::class)->run($_SERVER['argv'])); exit($container->get(Command::class)->run($_SERVER['argv']));
})(); })();

View File

@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace Pest\Actions;
use Pest\Console\Paratest\Runner;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Plugin\Loader;
use Pest\Support\Container;
use Pest\Support\Coverage;
use Pest\TestSuite;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\OutputInterface;
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
{
self::inParallel($testSuite);
}
private static function registerPlugins(): void
{
$plugins = Loader::getPlugins(HandlesArguments::class);
/** @var HandlesArguments $plugin */
foreach ($plugins as $plugin) {
$_SERVER['argv'] = $plugin->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 <fg=white;bg=red;options=bold> 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;
}
}
}

View File

@ -14,6 +14,7 @@ use ParaTest\Runners\PHPUnit\ResultPrinter;
use ParaTest\Runners\PHPUnit\RunnerInterface; use ParaTest\Runners\PHPUnit\RunnerInterface;
use ParaTest\Runners\PHPUnit\SuiteLoader; use ParaTest\Runners\PHPUnit\SuiteLoader;
use Pest\Factories\TestCaseFactory; use Pest\Factories\TestCaseFactory;
use Pest\Plugins\Coverage;
use Pest\TestSuite; use Pest\TestSuite;
use PHPUnit\TextUI\TestRunner; use PHPUnit\TextUI\TestRunner;
use SebastianBergmann\Timer\Timer; use SebastianBergmann\Timer\Timer;
@ -227,6 +228,12 @@ final class Runner implements RunnerInterface
$this->output->writeln( $this->output->writeln(
sprintf('done [%s]', $timer->stop()->asString()) 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 private function hasCoverage(): bool