fix: Pest.php file not loaded in certain environments

This commit is contained in:
Nuno Maduro
2023-03-20 17:34:42 +00:00
parent 6071d86ac6
commit b887116e5c
5 changed files with 3 additions and 137 deletions

View File

@ -1,7 +1,6 @@
#!/usr/bin/env php
<?php declare(strict_types=1);
use Pest\ConfigLoader;
use Pest\Kernel;
use Pest\Panic;
use Pest\TestCaseFilters\GitDirtyTestCaseFilter;
@ -71,7 +70,7 @@ use Symfony\Component\Console\Output\ConsoleOutput;
$testSuite = TestSuite::getInstance(
$rootPath,
$input->getParameterOption('--test-directory', (new ConfigLoader($rootPath))->getTestsDirectory()),
$input->getParameterOption('--test-directory', 'tests'),
);
if ($dirty) {

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
use ParaTest\WrapperRunner\ApplicationForWrapperWorker;
use ParaTest\WrapperRunner\WrapperWorker;
use Pest\ConfigLoader;
use Pest\Kernel;
use Pest\Plugins\Actions\CallsHandleArguments;
use Pest\TestSuite;
@ -18,7 +17,7 @@ $bootPest = (static function (): void {
$rootPath = dirname(PHPUNIT_COMPOSER_INSTALL, 2);
$testSuite = TestSuite::getInstance($rootPath, $workerArgv->getParameterOption(
'--test-directory',
(new ConfigLoader($rootPath))->getTestsDirectory()
'tests'
));
$input = new ArgvInput();

View File

@ -1,113 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest;
use Pest\Support\Str;
use SimpleXMLElement;
use Throwable;
/**
* @internal
*/
final class ConfigLoader
{
/**
* Default path if config loading went wrong.
*
* @var string
*/
public const DEFAULT_TESTS_PATH = 'tests';
/**
* XML tree of the PHPUnit configuration file.
*/
private ?SimpleXMLElement $config = null;
/**
* Creates a new instance of the config loader.
*/
public function __construct(private readonly string $rootPath)
{
$this->loadConfiguration();
}
/**
* Get the tests directory or fallback to default path.
*/
public function getTestsDirectory(): string
{
$suiteDirectory = [];
if (is_null($this->config)) {
return self::DEFAULT_TESTS_PATH;
}
$suiteDirectory = $this->config->xpath('/phpunit/testsuites/testsuite/directory');
if ($suiteDirectory === []) {
return self::DEFAULT_TESTS_PATH;
}
$directory = (string) ($suiteDirectory[0] ?? '');
if ($directory === '') {
return self::DEFAULT_TESTS_PATH;
}
// Return the whole directory if only a separator found (e.g. `./tests`)
if (substr_count($directory, DIRECTORY_SEPARATOR) === 1) {
return is_dir($directory) ? $directory : self::DEFAULT_TESTS_PATH;
}
$basePath = Str::beforeLast($directory, DIRECTORY_SEPARATOR);
return is_dir($basePath) ? $basePath : self::DEFAULT_TESTS_PATH;
}
/**
* Get the configuration file path.
*/
public function getConfigurationFilePath(): string|bool
{
$candidates = [
$this->rootPath.'/phpunit.xml',
$this->rootPath.'/phpunit.dist.xml',
$this->rootPath.'/phpunit.xml.dist',
];
foreach ($candidates as $candidate) {
if (is_file($candidate)) {
return realpath($candidate);
}
}
return false;
}
/**
* Load the configuration file.
*/
private function loadConfiguration(): void
{
$configPath = $this->getConfigurationFilePath();
if (is_bool($configPath)) {
return;
}
$oldReportingLevel = error_reporting(0);
$content = file_get_contents($configPath);
if ($content !== false) {
try {
$this->config = new SimpleXMLElement($content);
} catch (Throwable) { // @phpstan-ignore-line
// @ignoreException
}
}
// Restore the correct error reporting
error_reporting($oldReportingLevel);
}
}

View File

@ -1,19 +0,0 @@
<?php
use Pest\ConfigLoader;
use Pest\Support\Reflection;
it('fallbacks to default path if no phpunit file is found', function () {
$instance = new ConfigLoader('fake-path');
expect(Reflection::getPropertyValue($instance, 'config'))->toBeNull();
expect($instance->getConfigurationFilePath())->toBeFalse();
expect($instance->getTestsDirectory())->toBe(ConfigLoader::DEFAULT_TESTS_PATH);
});
it('fallbacks to default path if phpunit is not a valid XML')->skip();
it('fallbacks to default path if failing to read phpunit content')->skip();
it('fallbacks to default path if there is no test suites directory')->skip();
it('fallbacks to default path if test suite directory has no value')->skip();
it('fallbacks to default path if test suite directory does not exist')->skip();
it('returns the parent folder of first test suite directory')->skip();

View File

@ -15,6 +15,6 @@ $run = function () {
};
test('parallel', function () use ($run) {
expect($run())->toContain('Tests: 2 deprecated, 3 warnings, 4 incomplete, 1 notice, 4 todos, 15 skipped, 641 passed (1579 assertions)')
expect($run())->toContain('Tests: 2 deprecated, 3 warnings, 4 incomplete, 1 notice, 4 todos, 9 skipped, 640 passed (1576 assertions)')
->toContain('Parallel: 3 processes');
})->skip(PHP_OS_FAMILY === 'Windows');