Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/pest
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use Pest\TestCaseMethodFilters\NotesTestCaseFilter;
use Pest\TestCaseMethodFilters\PrTestCaseFilter;
use Pest\TestCaseMethodFilters\FlakyTestCaseFilter;
use Pest\TestCaseMethodFilters\TodoTestCaseFilter;
use Pest\Support\Worktree;
use Pest\TestSuite;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand Down Expand Up @@ -142,8 +143,7 @@ use Symfony\Component\Console\Output\ConsoleOutput;
$autoloadPath = $localPath;
}

// Get $rootPath based on $autoloadPath
$rootPath = dirname($autoloadPath, 2);
$rootPath = Worktree::resolveRoot($autoloadPath);

$input = new ArgvInput;

Expand Down
5 changes: 3 additions & 2 deletions bin/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pest\Kernel;
use Pest\Plugins\Actions\CallsHandleArguments;
use Pest\Support\Container;
use Pest\Support\Worktree;
use Pest\TestSuite;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand All @@ -15,7 +16,7 @@
$bootPest = (static function (): void {
$workerArgv = new ArgvInput;

$rootPath = dirname(PHPUNIT_COMPOSER_INSTALL, 2);
$rootPath = Worktree::resolveRoot(PHPUNIT_COMPOSER_INSTALL);
$testSuite = TestSuite::getInstance($rootPath, $workerArgv->getParameterOption(
'--test-directory',
'tests'
Expand Down Expand Up @@ -60,7 +61,7 @@
}

$container = Container::getInstance();
$rootPath = dirname(PHPUNIT_COMPOSER_INSTALL, 2);
$rootPath = Worktree::resolveRoot(PHPUNIT_COMPOSER_INSTALL);

foreach (Kernel::RESTARTERS as $restarterClass) {
$restarter = $container->get($restarterClass);
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
]
},
"extra": {
"branch-alias": {
"dev-feature/worktree-symlinked-vendor": "4.7.x-dev"
},
"pest": {
"plugins": [
"Pest\\Mutate\\Plugins\\Mutate",
Expand Down
1 change: 1 addition & 0 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,4 @@ function visit(array|string $url, array $options = []): ArrayablePendingAwaitabl
return test()->visit($url, $options);
}
}

39 changes: 39 additions & 0 deletions src/Support/Worktree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Pest\Support;

/**
* @internal
*/
final class Worktree
{
/**
* Resolve the project root path, accounting for git worktrees
* where vendor is a symlink to another worktree's vendor directory.
*
* When the current working directory has a `vendor` symlink that
* resolves to the same directory as the autoloaded vendor path,
* this means we're inside a git worktree whose vendor directory
* is symlinked to the main worktree. In that case, the project
* root should be the current working directory (the worktree),
* so that the worktree's own `Pest.php`, `tests/`, and
* `phpunit.xml` are found.
*/
public static function resolveRoot(string $autoloadPath): string
{
$rootPath = dirname($autoloadPath, 2);

$cwd = getcwd();
if ($cwd !== false
&& is_link($cwd.'/vendor')
&& realpath($cwd.'/vendor') === dirname($autoloadPath, 1)) {
$rootPath = $cwd;
}

$_ENV['APP_BASE_PATH'] = $rootPath;

return $rootPath;
}
}
124 changes: 124 additions & 0 deletions tests/Unit/Worktree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

use Pest\Support\Worktree;

beforeEach(function () {
$this->originalCwd = getcwd();

// Create the "real project" directory structure with a vendor autoloader
$this->realProject = sys_get_temp_dir().'/pest-test-real-'.bin2hex(random_bytes(4));
mkdir($this->realProject.'/vendor', 0777, true);
touch($this->realProject.'/vendor/autoload.php');
$this->autoloadPath = $this->realProject.'/vendor/autoload.php';

// Create the "worktree" directory with a vendor symlink pointing to the real project
$this->worktree = sys_get_temp_dir().'/pest-test-worktree-'.bin2hex(random_bytes(4));
mkdir($this->worktree, 0777, true);
symlink($this->realProject.'/vendor', $this->worktree.'/vendor');

// Create a "different vendor" for the wrong-symlink scenario
$this->differentVendor = sys_get_temp_dir().'/pest-test-different-'.bin2hex(random_bytes(4));
mkdir($this->differentVendor, 0777, true);
touch($this->differentVendor.'/autoload.php');
});

afterEach(function () {
chdir($this->originalCwd);

// Recursive removal helpers
$removeDir = function (string $path) use (&$removeDir): void {
if (! is_dir($path) && ! is_link($path)) {
return;
}
foreach (scandir($path) as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$itemPath = $path.'/'.$item;
if (is_link($itemPath) || ! is_dir($itemPath)) {
unlink($itemPath);
} else {
$removeDir($itemPath);
}
}
rmdir($path);
};

// Clean up worktree first (symlink and directories)
if (is_link($this->worktree.'/vendor')) {
unlink($this->worktree.'/vendor');
}
if (is_dir($this->worktree)) {
$removeDir($this->worktree);
}

// Clean up real project
if (is_file($this->autoloadPath)) {
unlink($this->autoloadPath);
}
if (is_dir($this->realProject.'/vendor')) {
rmdir($this->realProject.'/vendor');
}
if (is_dir($this->realProject)) {
rmdir($this->realProject);
}

// Clean up different vendor
if (is_file($this->differentVendor.'/autoload.php')) {
unlink($this->differentVendor.'/autoload.php');
}
if (is_dir($this->differentVendor)) {
rmdir($this->differentVendor);
}

unset($this->originalCwd, $this->realProject, $this->autoloadPath, $this->worktree, $this->differentVendor, $removeDir);
});

it('returns the autoload parent directory when not inside a worktree', function () {
chdir($this->realProject);

$root = Worktree::resolveRoot($this->autoloadPath);

expect($root)->toBe($this->realProject);
});

it('returns the worktree root when vendor is a symlink matching the autoload vendor', function () {
chdir($this->worktree);

$root = Worktree::resolveRoot($this->autoloadPath);

expect($root)->toBe($this->worktree);
});

it('returns the autoload parent directory when vendor symlink points elsewhere', function () {
symlink($this->differentVendor, $this->worktree.'/vendor-wrong');
rename($this->worktree.'/vendor', $this->worktree.'/vendor-original');
rename($this->worktree.'/vendor-wrong', $this->worktree.'/vendor');

chdir($this->worktree);

$root = Worktree::resolveRoot($this->autoloadPath);

expect($root)->toBe($this->realProject);
});

it('returns the autoload parent directory when vendor is a real directory, not a symlink', function () {
// Replace the worktree's vendor symlink with a real vendor directory
unlink($this->worktree.'/vendor');
mkdir($this->worktree.'/vendor', 0777, true);
touch($this->worktree.'/vendor/autoload.php');

chdir($this->worktree);

$root = Worktree::resolveRoot($this->autoloadPath);

expect($root)->toBe($this->realProject);
});

it('sets APP_BASE_PATH in the environment', function () {
chdir($this->realProject);

Worktree::resolveRoot($this->autoloadPath);

expect($_ENV['APP_BASE_PATH'])->toBe($this->realProject);
});