fix: ensures --retry works with errored tests

This commit is contained in:
Nuno Maduro
2023-01-02 20:59:59 +00:00
parent 43a7df2cc1
commit 330b4f0171
7 changed files with 44 additions and 14 deletions

View File

@ -23,7 +23,8 @@ final class BootSubscribers implements Bootstrapper
Subscribers\EnsureConfigurationIsValid::class,
Subscribers\EnsureConfigurationDefaults::class,
Subscribers\EnsureRetryRepositoryExists::class,
Subscribers\EnsureFailedTestsAreStoredForRetry::class,
Subscribers\EnsureErroredTestsAreRetryable::class,
Subscribers\EnsureFailedTestsAreRetryable::class,
];
/**

View File

@ -123,7 +123,7 @@ final class TestCaseMethodFactory
$methodName = Str::evaluable($this->description);
$retryRepository = TestSuite::getInstance()->retryTempRepository;
$retryRepository = TestSuite::getInstance()->retryRepository;
if (Retry::$retrying && ! $retryRepository->isEmpty() && ! $retryRepository->exists(sprintf('%s::%s', $classFQN, $methodName))) {
return '';

View File

@ -7,9 +7,15 @@ namespace Pest\Repositories;
/**
* @internal
*/
final class TempRepository
final class RetryRepository
{
private const FOLDER = __DIR__.'/../../.temp';
private const TEMPORARY_FOLDER = __DIR__
.DIRECTORY_SEPARATOR
.'..'
.DIRECTORY_SEPARATOR
.'..'
.DIRECTORY_SEPARATOR
.'.temp';
/**
* Creates a new Temp Repository instance.
@ -32,7 +38,7 @@ final class TempRepository
*/
public function boot(): void
{
@unlink(self::FOLDER.'/'.$this->filename.'.json'); // @phpstan-ignore-line
@unlink(self::TEMPORARY_FOLDER.'/'.$this->filename.'.json'); // @phpstan-ignore-line
$this->save([]);
}
@ -60,7 +66,7 @@ final class TempRepository
*/
private function all(): array
{
$path = self::FOLDER.'/'.$this->filename.'.json';
$path = self::TEMPORARY_FOLDER.'/'.$this->filename.'.json';
$contents = file_exists($path) ? file_get_contents($path) : '{}';
@ -80,6 +86,6 @@ final class TempRepository
{
$contents = json_encode($elements, JSON_THROW_ON_ERROR);
file_put_contents(self::FOLDER.'/'.$this->filename.'.json', $contents);
file_put_contents(self::TEMPORARY_FOLDER.'/'.$this->filename.'.json', $contents);
}
}

View File

@ -0,0 +1,23 @@
<?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

@ -11,13 +11,13 @@ use PHPUnit\Event\Test\FailedSubscriber;
/**
* @internal
*/
final class EnsureFailedTestsAreStoredForRetry implements FailedSubscriber
final class EnsureFailedTestsAreRetryable implements FailedSubscriber
{
/**
* Runs the subscriber.
*/
public function notify(Failed $event): void
{
TestSuite::getInstance()->retryTempRepository->add($event->test()->id());
TestSuite::getInstance()->retryRepository->add($event->test()->id());
}
}

View File

@ -18,6 +18,6 @@ final class EnsureRetryRepositoryExists implements StartedSubscriber
*/
public function notify(Started $event): void
{
TestSuite::getInstance()->retryTempRepository->boot();
TestSuite::getInstance()->retryRepository->boot();
}
}

View File

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