mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
fix: --dirty when no tests found
This commit is contained in:
2
bin/pest
2
bin/pest
@ -84,7 +84,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
$kernel = Kernel::boot();
|
||||
|
||||
$result = $kernel->handle($args);
|
||||
$result = $kernel->handle($output, $args);
|
||||
|
||||
$kernel->shutdown();
|
||||
|
||||
|
||||
26
src/Exceptions/NoTestsFound.php
Normal file
26
src/Exceptions/NoTestsFound.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use NunoMaduro\Collision\Contracts\RenderlessEditor;
|
||||
use NunoMaduro\Collision\Contracts\RenderlessTrace;
|
||||
use Symfony\Component\Console\Exception\ExceptionInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class NoTestsFound extends InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace
|
||||
{
|
||||
/**
|
||||
* Creates a new Exception instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('No tests found.');
|
||||
|
||||
// ...
|
||||
}
|
||||
}
|
||||
@ -5,12 +5,14 @@ declare(strict_types=1);
|
||||
namespace Pest;
|
||||
|
||||
use Pest\Contracts\Bootstrapper;
|
||||
use Pest\Exceptions\NoTestsFound;
|
||||
use Pest\Plugins\Actions\CallsAddsOutput;
|
||||
use Pest\Plugins\Actions\CallsBoot;
|
||||
use Pest\Plugins\Actions\CallsShutdown;
|
||||
use Pest\Support\Container;
|
||||
use PHPUnit\TextUI\Application;
|
||||
use PHPUnit\TextUI\Exception;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -69,11 +71,19 @@ final class Kernel
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handle(array $argv): int
|
||||
public function handle(OutputInterface $output, array $argv): int
|
||||
{
|
||||
$argv = (new Plugins\Actions\CallsHandleArguments())->__invoke($argv);
|
||||
|
||||
try {
|
||||
$this->application->run($argv, false);
|
||||
} catch (NoTestsFound) { // @phpstan-ignore-line
|
||||
$output->writeln([
|
||||
'',
|
||||
' <fg=white;options=bold;bg=blue> INFO </> No tests found.',
|
||||
'',
|
||||
]);
|
||||
}
|
||||
|
||||
return (new CallsAddsOutput())->__invoke(
|
||||
Result::exitCode(),
|
||||
|
||||
@ -6,22 +6,30 @@ namespace Pest\TestCaseFilters;
|
||||
|
||||
use Pest\Contracts\TestCaseFilter;
|
||||
use Pest\Exceptions\MissingDependency;
|
||||
use Pest\Exceptions\NoTestsFound;
|
||||
use Pest\TestSuite;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
final class GitDirtyTestCaseFilter implements TestCaseFilter
|
||||
{
|
||||
/**
|
||||
* @var array<string>
|
||||
* @var string[]|null
|
||||
*/
|
||||
private array $changedFiles = [];
|
||||
private array|null $changedFiles = null;
|
||||
|
||||
public function __construct(private readonly string $projectRoot)
|
||||
{
|
||||
$this->loadDiff();
|
||||
// ...
|
||||
}
|
||||
|
||||
public function accept(string $testCaseFilename): bool
|
||||
{
|
||||
if ($this->changedFiles === null) {
|
||||
$this->loadChangedFiles();
|
||||
}
|
||||
|
||||
assert(is_array($this->changedFiles));
|
||||
|
||||
$relativePath = str_replace($this->projectRoot, '', $testCaseFilename);
|
||||
|
||||
if (str_starts_with($relativePath, '/')) {
|
||||
@ -31,7 +39,7 @@ final class GitDirtyTestCaseFilter implements TestCaseFilter
|
||||
return in_array($relativePath, $this->changedFiles, true);
|
||||
}
|
||||
|
||||
private function loadDiff(): void
|
||||
private function loadChangedFiles(): void
|
||||
{
|
||||
$process = new Process(['git', 'status', '--short', '--', '*.php']);
|
||||
$process->run();
|
||||
@ -53,6 +61,14 @@ final class GitDirtyTestCaseFilter implements TestCaseFilter
|
||||
|
||||
$dirtyFiles = array_map(fn ($file, $status): string => in_array($status, ['R', 'RM'], true) ? explode(' -> ', $file)[1] : $file, array_keys($dirtyFiles), $dirtyFiles);
|
||||
|
||||
$this->changedFiles = $dirtyFiles = array_values($dirtyFiles);
|
||||
$dirtyFiles = array_filter($dirtyFiles, fn ($file): bool => str_starts_with('.'.DIRECTORY_SEPARATOR.$file, TestSuite::getInstance()->testPath));
|
||||
|
||||
$dirtyFiles = array_values($dirtyFiles);
|
||||
|
||||
if ($dirtyFiles === []) {
|
||||
throw new NoTestsFound();
|
||||
}
|
||||
|
||||
$this->changedFiles = $dirtyFiles;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user