Support nested Composer vendor directories (#2053)

Co-authored-by: haranishi <223786623+haranishi@users.noreply.github.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
Ikki Nishihara
2026-11-20 06:10:22 +09:00
committed by GitHub
parent a865244b92
commit fabc818ba9
5 changed files with 112 additions and 17 deletions
+8 -12
View File
@@ -6,6 +6,7 @@ declare(strict_types=1);
use Pest\Contracts\Restarter;
use Pest\Kernel;
use Pest\Panic;
use Pest\Support\ComposerProject;
use Pest\Support\Container;
use Pest\TestCaseFilters\GitDirtyTestCaseFilter;
use Pest\TestCaseMethodFilters\AssigneeTestCaseFilter;
@@ -129,21 +130,16 @@ use Symfony\Component\Console\Output\ConsoleOutput;
}
// Used when Pest is required using composer.
$vendorPath = dirname(__DIR__, 4).'/vendor/autoload.php';
$autoloadPath = $GLOBALS['_composer_autoload_path'] ?? dirname(__DIR__, 3).'/autoload.php';
// Used when Pest maintainers are running Pest tests.
$localPath = dirname(__DIR__).'/vendor/autoload.php';
if (file_exists($vendorPath)) {
include_once $vendorPath;
$autoloadPath = $vendorPath;
} else {
include_once $localPath;
$autoloadPath = $localPath;
if (! is_file($autoloadPath)) {
$autoloadPath = dirname(__DIR__).'/vendor/autoload.php';
}
// Get $rootPath based on $autoloadPath
$rootPath = dirname($autoloadPath, 2);
include_once $autoloadPath;
$GLOBALS['_composer_bin_dir'] ??= dirname($autoloadPath).'/bin';
$rootPath = ComposerProject::rootPath($autoloadPath);
$input = new ArgvInput;
+5 -4
View File
@@ -7,6 +7,7 @@ use ParaTest\WrapperRunner\WrapperWorker;
use Pest\Kernel;
use Pest\Plugins\Actions\CallsHandleArguments;
use Pest\Plugins\Parallel\Paratest\StopConditions;
use Pest\Support\ComposerProject;
use Pest\Support\Container;
use Pest\TestSuite;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
@@ -14,10 +15,9 @@ use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
$bootPest = (static function (): void {
$bootPest = (static function (string $rootPath): void {
$workerArgv = new ArgvInput;
$rootPath = dirname(PHPUNIT_COMPOSER_INSTALL, 2);
$testSuite = TestSuite::getInstance($rootPath, $workerArgv->getParameterOption(
'--test-directory',
'tests'
@@ -56,13 +56,14 @@ $bootPest = (static function (): void {
if (file_exists($file)) {
require_once $file;
define('PHPUNIT_COMPOSER_INSTALL', $file);
$GLOBALS['_composer_bin_dir'] ??= dirname($file).'/bin';
break;
}
}
$container = Container::getInstance();
$rootPath = dirname(PHPUNIT_COMPOSER_INSTALL, 2);
$rootPath = ComposerProject::rootPath(PHPUNIT_COMPOSER_INSTALL);
foreach (Kernel::RESTARTERS as $restarterClass) {
$restarter = $container->get($restarterClass);
@@ -85,7 +86,7 @@ $bootPest = (static function (): void {
$phpunitArgv = unserialize($getopt['phpunit-argv'], ['allowed_classes' => false]);
assert(is_array($phpunitArgv));
$bootPest();
$bootPest($rootPath);
$phpunitArgv = CallsHandleArguments::execute($phpunitArgv);
+28
View File
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
/**
* @internal
*/
final class ComposerProject
{
public static function rootPath(string $autoloadPath): string
{
$vendorPath = dirname($autoloadPath);
$vendorPath = realpath($vendorPath) ?: $vendorPath;
$rootPath = dirname($vendorPath);
$metadataPath = $vendorPath.'/composer/installed.php';
if (! is_file($metadataPath)) {
return $rootPath;
}
/** @var array{root: array{install_path: string}} $installed */
$installed = require $metadataPath;
return realpath($installed['root']['install_path']) ?: $rootPath;
}
}
+6 -1
View File
@@ -2431,6 +2431,11 @@
✓ junit output
✓ junit with parallel
PASS Tests\Visual\NestedVendorDirectory
✓ runs with a nested Composer vendor directory with dataset "Composer proxy"
✓ runs with a nested Composer vendor directory with dataset "Composer proxy with workers"
✓ runs with a nested Composer vendor directory with dataset "direct execution with workers"
PASS Tests\Visual\Parallel
✓ parallel
✓ a parallel test can extend another test with same name
@@ -2557,4 +2562,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1853 passed (4263 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1856 passed (4269 assertions)
+65
View File
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
use Symfony\Component\Process\Process;
use Tests\Fixtures\Tia\Project;
test('runs with a nested Composer vendor directory', function (string $binary, array $arguments, int $assertions): void {
$project = Project::withoutGit();
try {
$vendor = 'build/dependencies';
mkdir($project->path('build'));
rename($project->path('vendor'), $project->path($vendor));
$project->write($vendor.'/composer/installed.php', <<<'METADATA'
<?php
return ['root' => ['install_path' => __DIR__.'/../../..']];
METADATA);
$project->write($vendor.'/bin/pest', <<<'PROXY'
<?php
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__.'/../autoload.php';
return include __DIR__.'/../pestphp/pest/bin/pest';
PROXY);
$project->write($vendor.'/pestphp/pest/.temp/.gitkeep', '');
$project->write('phpunit.xml', <<<'CONFIGURATION'
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="build/dependencies/autoload.php" cacheDirectory=".phpunit.cache"/>
CONFIGURATION);
$test = 'tests/NestedVendorTest.php';
$project->write($test, <<<'FIXTURE'
<?php
test('uses the project root', function (): void {
expect(Pest\TestSuite::getInstance()->rootPath)->toBe(realpath(__DIR__.'/..'));
if (($_SERVER['PARATEST'] ?? null) === '1') {
expect($_SERVER['PEST_PARALLEL'] ?? null)->toBe('1');
}
});
FIXTURE);
$process = new Process(
[PHP_BINARY, $project->path($vendor.'/'.$binary), '--configuration='.$project->path('phpunit.xml'), '--colors=never', $project->path($test), ...$arguments],
dirname($project->path),
['PARATEST' => '0', 'PAO_DISABLE' => '1'],
);
expect($process->run())->toBe(0)
->and($process->getOutput())->toContain("1 passed ($assertions assertions)");
} finally {
Project::destroyAll();
}
})->with([
'Composer proxy' => ['bin/pest', [], 1],
'Composer proxy with workers' => ['bin/pest', ['--parallel', '--processes=2'], 2],
'direct execution with workers' => ['pestphp/pest/bin/pest', ['--parallel', '--processes=2'], 2],
]);