refacto: --retry option

This commit is contained in:
Nuno Maduro
2023-02-11 16:48:06 +00:00
parent a8f0b96338
commit d69f61c8d3
18 changed files with 61 additions and 200 deletions

View File

@ -22,10 +22,7 @@ final class BootSubscribers implements Bootstrapper
*/
private const SUBSCRIBERS = [
Subscribers\EnsureConfigurationIsValid::class,
Subscribers\EnsureConfigurationDefaults::class,
Subscribers\EnsureRetryRepositoryExists::class,
Subscribers\EnsureErroredTestsAreRetryable::class,
Subscribers\EnsureFailedTestsAreRetryable::class,
Subscribers\EnsureConfigurationIsAvailable::class,
Subscribers\EnsureTeamCityEnabled::class,
];

View File

@ -8,7 +8,6 @@ use Closure;
use Pest\Contracts\AddsAnnotations;
use Pest\Exceptions\ShouldNotHappen;
use Pest\Factories\Concerns\HigherOrderable;
use Pest\Plugins\Retry;
use Pest\Repositories\DatasetsRepository;
use Pest\Support\Str;
use Pest\TestSuite;
@ -129,12 +128,6 @@ final class TestCaseMethodFactory
$methodName = Str::evaluable($this->description);
$retryRepository = TestSuite::getInstance()->retryRepository;
if (Retry::$retrying && ! $retryRepository->isEmpty() && ! $retryRepository->exists(sprintf('%s::%s', $classFQN, $methodName))) {
return '';
}
$datasetsCode = '';
$annotations = ['@test'];
$attributes = [];

37
src/Plugins/Cache.php Normal file
View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Plugins\Concerns\HandleArguments;
/**
* @internal
*/
final class Cache implements HandlesArguments
{
use HandleArguments;
private const TEMPORARY_FOLDER = __DIR__
.DIRECTORY_SEPARATOR
.'..'
.DIRECTORY_SEPARATOR
.'..'
.DIRECTORY_SEPARATOR
.'.temp';
/**
* {@inheritdoc}
*/
public function handleArguments(array $arguments): array
{
$arguments = $this->pushArgument(
sprintf('--cache-directory=%s', realpath(self::TEMPORARY_FOLDER)),
$arguments
);
return $this->pushArgument('--cache-result', $arguments);
}
}

View File

@ -13,18 +13,18 @@ final class Retry implements HandlesArguments
{
use Concerns\HandleArguments;
/**
* Whether it should show retry or not.
*/
public static bool $retrying = false;
/**
* {@inheritDoc}
*/
public function handleArguments(array $arguments): array
{
self::$retrying = $this->hasArgument('--retry', $arguments);
if ($this->hasArgument('--retry', $arguments)) {
$arguments = $this->popArgument('--retry', $arguments);
return $this->popArgument('--retry', $arguments);
$arguments = $this->pushArgument('--order-by=defects', $arguments);
$arguments = $this->pushArgument(' --stop-on-defect', $arguments);
}
return $arguments;
}
}

View File

@ -1,91 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Repositories;
/**
* @internal
*/
final class RetryRepository
{
private const TEMPORARY_FOLDER = __DIR__
.DIRECTORY_SEPARATOR
.'..'
.DIRECTORY_SEPARATOR
.'..'
.DIRECTORY_SEPARATOR
.'.temp';
/**
* Creates a new Temp Repository instance.
*/
public function __construct(private readonly string $filename)
{
// ..
}
/**
* Adds a new element.
*/
public function add(string $element): void
{
$this->save([...$this->all(), ...[$element]]);
}
/**
* Clears the existing file, if any, and re-creates it.
*/
public function boot(): void
{
@unlink(self::TEMPORARY_FOLDER.'/'.$this->filename.'.json'); // @phpstan-ignore-line
$this->save([]);
}
/**
* Checks if there is any element.
*/
public function isEmpty(): bool
{
return $this->all() === [];
}
/**
* Checks if the given element exists.
*/
public function exists(string $element): bool
{
return in_array($element, $this->all(), true);
}
/**
* Gets all elements.
*
* @return array<int, string>
*/
private function all(): array
{
$path = self::TEMPORARY_FOLDER.'/'.$this->filename.'.json';
$contents = file_exists($path) ? file_get_contents($path) : '{}';
assert(is_string($contents));
$all = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
return is_array($all) ? $all : [];
}
/**
* Save the given elements.
*
* @param array<int, string> $elements
*/
private function save(array $elements): void
{
$contents = json_encode($elements, JSON_THROW_ON_ERROR);
file_put_contents(self::TEMPORARY_FOLDER.'/'.$this->filename.'.json', $contents);
}
}

View File

@ -4,19 +4,21 @@ declare(strict_types=1);
namespace Pest\Subscribers;
use Pest\Support\Container;
use PHPUnit\Event\TestRunner\Configured;
use PHPUnit\Event\TestRunner\ConfiguredSubscriber;
use PHPUnit\TextUI\Configuration\Configuration;
/**
* @internal
*/
final class EnsureConfigurationDefaults implements ConfiguredSubscriber
final class EnsureConfigurationIsAvailable implements ConfiguredSubscriber
{
/**
* Runs the subscriber.
*/
public function notify(Configured $event): void
{
// TODO...
Container::getInstance()->add(Configuration::class, $event->configuration());
}
}

View File

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Subscribers;
use Pest\TestSuite;
use PHPUnit\Event\Test\Errored;
use PHPUnit\Event\Test\ErroredSubscriber;
/**
* @internal
*/
final class EnsureErroredTestsAreRetryable implements ErroredSubscriber
{
/**
* Runs the subscriber.
*/
public function notify(Errored $event): void
{
TestSuite::getInstance()->retryRepository->add($event->test()->id());
}
}

View File

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Subscribers;
use Pest\TestSuite;
use PHPUnit\Event\Test\Failed;
use PHPUnit\Event\Test\FailedSubscriber;
/**
* @internal
*/
final class EnsureFailedTestsAreRetryable implements FailedSubscriber
{
/**
* Runs the subscriber.
*/
public function notify(Failed $event): void
{
TestSuite::getInstance()->retryRepository->add($event->test()->id());
}
}

View File

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Subscribers;
use Pest\TestSuite;
use PHPUnit\Event\TestRunner\Started;
use PHPUnit\Event\TestRunner\StartedSubscriber;
/**
* @internal
*/
final class EnsureRetryRepositoryExists implements StartedSubscriber
{
/**
* Runs the subscriber.
*/
public function notify(Started $event): void
{
TestSuite::getInstance()->retryRepository->boot();
}
}

View File

@ -9,7 +9,6 @@ use Pest\Repositories\AfterAllRepository;
use Pest\Repositories\AfterEachRepository;
use Pest\Repositories\BeforeAllRepository;
use Pest\Repositories\BeforeEachRepository;
use Pest\Repositories\RetryRepository;
use Pest\Repositories\TestRepository;
use PHPUnit\Framework\TestCase;
@ -48,11 +47,6 @@ final class TestSuite
*/
public AfterAllRepository $afterAll;
/**
* Holds the retry repository.
*/
public RetryRepository $retryRepository;
/**
* Holds the root path.
*/
@ -75,7 +69,6 @@ final class TestSuite
$this->tests = new TestRepository();
$this->afterEach = new AfterEachRepository();
$this->afterAll = new AfterAllRepository();
$this->retryRepository = new RetryRepository('retry');
$this->rootPath = (string) realpath($rootPath);
}