From c2243b20bcd99c3f651018d1447144372f39b4fa Mon Sep 17 00:00:00 2001 From: Stephan Wentz Date: Thu, 25 May 2023 15:47:58 +0200 Subject: [PATCH] Add `--stop-on-error` option to runner (#765) Co-authored-by: Stephan Wentz --- src/Runners/PHPUnit/Options.php | 26 +++++++++++++++++++++++ src/Runners/PHPUnit/Runner.php | 2 +- src/Runners/PHPUnit/WrapperRunner.php | 2 +- test/Unit/Runners/PHPUnit/OptionsTest.php | 4 ++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Runners/PHPUnit/Options.php b/src/Runners/PHPUnit/Options.php index 3d056021..6dfddadf 100644 --- a/src/Runners/PHPUnit/Options.php +++ b/src/Runners/PHPUnit/Options.php @@ -111,6 +111,13 @@ final class Options */ private $stopOnFailure; + /** + * Prevents starting new tests after a test has errored. + * + * @var bool + */ + private $stopOnError; + /** * A collection of post-processed option values. This is the collection * containing ParaTest specific options. @@ -261,6 +268,7 @@ private function __construct( int $processes, string $runner, bool $stopOnFailure, + bool $stopOnError, array $testsuite, string $tmpDir, bool $verbose, @@ -302,6 +310,7 @@ private function __construct( $this->processes = $processes; $this->runner = $runner; $this->stopOnFailure = $stopOnFailure; + $this->stopOnError = $stopOnError; $this->testsuite = $testsuite; $this->tmpDir = $tmpDir; $this->verbose = $verbose; @@ -344,6 +353,7 @@ public static function fromConsoleInput(InputInterface $input, string $cwd, bool assert($options['random-order-seed'] === null || is_string($options['random-order-seed'])); assert(is_string($options['runner'])); assert(is_bool($options['stop-on-failure'])); + assert(is_bool($options['stop-on-error'])); assert(is_string($options['tmp-dir'])); assert($options['whitelist'] === null || is_string($options['whitelist'])); assert($options['repeat'] === null || is_string($options['repeat'])); @@ -456,6 +466,10 @@ public static function fromConsoleInput(InputInterface $input, string $cwd, bool $filtered['stop-on-failure'] = null; } + if ($options['stop-on-error']) { + $filtered['stop-on-error'] = null; + } + $configuration = null; $configurationFile = self::guessConfigurationFile($options['configuration'], $cwd); if ($configurationFile !== null) { @@ -557,6 +571,7 @@ public static function fromConsoleInput(InputInterface $input, string $cwd, bool $options['processes'], $options['runner'], $options['stop-on-failure'], + $options['stop-on-error'], $testsuite, $options['tmp-dir'], $options['verbose'], @@ -789,6 +804,12 @@ public static function setInputDefinition(InputDefinition $inputDefinition): voi 'Runner or WrapperRunner.', 'Runner', ), + new InputOption( + 'stop-on-error', + null, + InputOption::VALUE_NONE, + 'Don\'t start any more processes after an error.', + ), new InputOption( 'stop-on-failure', null, @@ -961,6 +982,11 @@ public function stopOnFailure(): bool return $this->stopOnFailure; } + public function stopOnError(): bool + { + return $this->stopOnError; + } + /** @return array */ public function filtered(): array { diff --git a/src/Runners/PHPUnit/Runner.php b/src/Runners/PHPUnit/Runner.php index 256af19c..b8f48ad3 100644 --- a/src/Runners/PHPUnit/Runner.php +++ b/src/Runners/PHPUnit/Runner.php @@ -91,7 +91,7 @@ private function testIsStillRunning(RunnerWorker $worker): bool } $this->exitcode = max($this->exitcode, (int) $worker->stop()); - if ($this->options->stopOnFailure() && $this->exitcode > 0) { + if (($this->options->stopOnFailure() || $this->options->stopOnError()) && $this->exitcode > 0) { $this->pending = []; } diff --git a/src/Runners/PHPUnit/WrapperRunner.php b/src/Runners/PHPUnit/WrapperRunner.php index 976c30c7..697bb0d8 100644 --- a/src/Runners/PHPUnit/WrapperRunner.php +++ b/src/Runners/PHPUnit/WrapperRunner.php @@ -70,7 +70,7 @@ private function assignAllPendingTests(): void $worker = $this->startWorker($token); } - if ($this->exitcode > 0 && $this->options->stopOnFailure()) { + if ($this->exitcode > 0 && ($this->options->stopOnFailure() || $this->options->stopOnError())) { $this->pending = []; } elseif (($pending = array_shift($this->pending)) !== null) { $worker->assign($pending, $phpunit, $phpunitOptions, $this->options); diff --git a/test/Unit/Runners/PHPUnit/OptionsTest.php b/test/Unit/Runners/PHPUnit/OptionsTest.php index 7a5f05e8..82c86c95 100644 --- a/test/Unit/Runners/PHPUnit/OptionsTest.php +++ b/test/Unit/Runners/PHPUnit/OptionsTest.php @@ -258,6 +258,7 @@ public function testDefaultOptions(): void static::assertSame(PROCESSES_FOR_TESTS, $options->processes()); static::assertSame('Runner', $options->runner()); static::assertFalse($options->stopOnFailure()); + static::assertFalse($options->stopOnError()); static::assertEmpty($options->testsuite()); static::assertSame($this->tmpDir, $options->tmpDir()); static::assertFalse($options->verbose()); @@ -304,6 +305,7 @@ public function testProvidedOptions(): void '--processes' => '999', '--runner' => 'MYRUNNER', '--stop-on-failure' => true, + '--stop-on-error' => true, '--testsuite' => 'TESTSUITE', '--tmp-dir' => ($tmpDir = uniqid($this->tmpDir . DS . 't')), '--verbose' => true, @@ -346,6 +348,7 @@ public function testProvidedOptions(): void static::assertSame(999, $options->processes()); static::assertSame('MYRUNNER', $options->runner()); static::assertTrue($options->stopOnFailure()); + static::assertTrue($options->stopOnError()); static::assertSame(['TESTSUITE'], $options->testsuite()); static::assertSame($tmpDir, $options->tmpDir()); static::assertTrue($options->verbose()); @@ -364,6 +367,7 @@ public function testProvidedOptions(): void 'order-by' => Options::ORDER_RANDOM, 'random-order-seed' => (string) $expected_random_seed, 'repeat' => '2', + 'stop-on-error' => null, 'stop-on-failure' => null, 'whitelist' => 'WHITELIST', ], $options->filtered());