From dc73ec009e5729b856c77d1d520ace518472ff64 Mon Sep 17 00:00:00 2001 From: Jhon Farias Date: Thu, 26 Nov 2026 15:04:03 +0100 Subject: [PATCH] fix: ignore packages bundled in a PHAR when detecting installed packages (#2059) Co-authored-by: Punyapal Shah --- src/Plugins/Init.php | 4 +- src/Plugins/Parallel/Handlers/Laravel.php | 4 +- src/Plugins/Tia/WatchDefaults/Browser.php | 5 +- src/Plugins/Tia/WatchDefaults/Inertia.php | 7 +-- src/Plugins/Tia/WatchDefaults/Laravel.php | 5 +- src/Plugins/Tia/WatchDefaults/Livewire.php | 5 +- src/Plugins/Tia/WatchDefaults/Symfony.php | 5 +- src/Support/InstalledPackages.php | 55 +++++++++++++++++ tests/.snapshots/success.txt | 11 +++- tests/Features/Init.php | 64 +++++++++++++++++++- tests/Unit/Support/InstalledPackages.php | 68 ++++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 12 files changed, 212 insertions(+), 23 deletions(-) create mode 100644 src/Support/InstalledPackages.php create mode 100644 tests/Unit/Support/InstalledPackages.php diff --git a/src/Plugins/Init.php b/src/Plugins/Init.php index d8bbafc0..047da3de 100644 --- a/src/Plugins/Init.php +++ b/src/Plugins/Init.php @@ -4,9 +4,9 @@ declare(strict_types=1); namespace Pest\Plugins; -use Composer\InstalledVersions; use Pest\Console\Thanks; use Pest\Contracts\Plugins\HandlesArguments; +use Pest\Support\InstalledPackages; use Pest\Support\View; use Pest\TestSuite; use Symfony\Component\Console\Input\InputInterface; @@ -101,6 +101,6 @@ final readonly class Init implements HandlesArguments private function isLaravelInstalled(): bool { - return InstalledVersions::isInstalled('laravel/framework'); + return InstalledPackages::has('laravel/framework'); } } diff --git a/src/Plugins/Parallel/Handlers/Laravel.php b/src/Plugins/Parallel/Handlers/Laravel.php index 7fce40e6..7ea01696 100644 --- a/src/Plugins/Parallel/Handlers/Laravel.php +++ b/src/Plugins/Parallel/Handlers/Laravel.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Pest\Plugins\Parallel\Handlers; use Closure; -use Composer\InstalledVersions; use Illuminate\Testing\ParallelRunner; use Orchestra\Testbench\TestCase; use ParaTest\Options; @@ -13,6 +12,7 @@ use ParaTest\RunnerInterface; use Pest\Contracts\Plugins\HandlesArguments; use Pest\Plugins\Concerns\HandleArguments; use Pest\Plugins\Parallel\Paratest\WrapperRunner; +use Pest\Support\InstalledPackages; use Symfony\Component\Console\Output\OutputInterface; /** @@ -43,7 +43,7 @@ final class Laravel implements HandlesArguments */ private function whenUsingLaravel(array $arguments, Closure $closure): array { - $isLaravelApplication = InstalledVersions::isInstalled('laravel/framework', false); + $isLaravelApplication = InstalledPackages::has('laravel/framework', false); $isLaravelPackage = class_exists(TestCase::class); if ($isLaravelApplication && ! $isLaravelPackage) { diff --git a/src/Plugins/Tia/WatchDefaults/Browser.php b/src/Plugins/Tia/WatchDefaults/Browser.php index b03d60c4..37e08015 100644 --- a/src/Plugins/Tia/WatchDefaults/Browser.php +++ b/src/Plugins/Tia/WatchDefaults/Browser.php @@ -4,10 +4,10 @@ declare(strict_types=1); namespace Pest\Plugins\Tia\WatchDefaults; -use Composer\InstalledVersions; use Pest\Browser\Support\BrowserTestIdentifier; use Pest\Factories\TestCaseFactory; use Pest\Plugins\Tia\Contracts\WatchDefault; +use Pest\Support\InstalledPackages; use Pest\TestSuite; /** @@ -17,8 +17,7 @@ final readonly class Browser implements WatchDefault { public function applicable(): bool { - return class_exists(InstalledVersions::class) - && InstalledVersions::isInstalled('pestphp/pest-plugin-browser'); + return InstalledPackages::has('pestphp/pest-plugin-browser'); } public function defaults(string $projectRoot, string $testPath): array diff --git a/src/Plugins/Tia/WatchDefaults/Inertia.php b/src/Plugins/Tia/WatchDefaults/Inertia.php index 77151f3d..fad2653d 100644 --- a/src/Plugins/Tia/WatchDefaults/Inertia.php +++ b/src/Plugins/Tia/WatchDefaults/Inertia.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Pest\Plugins\Tia\WatchDefaults; -use Composer\InstalledVersions; use Pest\Plugins\Tia\Contracts\WatchDefault; +use Pest\Support\InstalledPackages; /** * @internal @@ -14,9 +14,8 @@ final readonly class Inertia implements WatchDefault { public function applicable(): bool { - return class_exists(InstalledVersions::class) - && (InstalledVersions::isInstalled('inertiajs/inertia-laravel') - || InstalledVersions::isInstalled('rompetomp/inertia-bundle')); + return InstalledPackages::has('inertiajs/inertia-laravel') + || InstalledPackages::has('rompetomp/inertia-bundle'); } public function defaults(string $projectRoot, string $testPath): array diff --git a/src/Plugins/Tia/WatchDefaults/Laravel.php b/src/Plugins/Tia/WatchDefaults/Laravel.php index 5e16cde9..09b5a423 100644 --- a/src/Plugins/Tia/WatchDefaults/Laravel.php +++ b/src/Plugins/Tia/WatchDefaults/Laravel.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Pest\Plugins\Tia\WatchDefaults; -use Composer\InstalledVersions; use Pest\Plugins\Tia\Contracts\WatchDefault; +use Pest\Support\InstalledPackages; /** * @internal @@ -14,8 +14,7 @@ final readonly class Laravel implements WatchDefault { public function applicable(): bool { - return class_exists(InstalledVersions::class) - && InstalledVersions::isInstalled('laravel/framework'); + return InstalledPackages::has('laravel/framework'); } public function defaults(string $projectRoot, string $testPath): array diff --git a/src/Plugins/Tia/WatchDefaults/Livewire.php b/src/Plugins/Tia/WatchDefaults/Livewire.php index 5487a450..121544d5 100644 --- a/src/Plugins/Tia/WatchDefaults/Livewire.php +++ b/src/Plugins/Tia/WatchDefaults/Livewire.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Pest\Plugins\Tia\WatchDefaults; -use Composer\InstalledVersions; use Pest\Plugins\Tia\Contracts\WatchDefault; +use Pest\Support\InstalledPackages; /** * @internal @@ -14,8 +14,7 @@ final readonly class Livewire implements WatchDefault { public function applicable(): bool { - return class_exists(InstalledVersions::class) - && InstalledVersions::isInstalled('livewire/livewire'); + return InstalledPackages::has('livewire/livewire'); } public function defaults(string $projectRoot, string $testPath): array diff --git a/src/Plugins/Tia/WatchDefaults/Symfony.php b/src/Plugins/Tia/WatchDefaults/Symfony.php index bcbd3ddd..259e3502 100644 --- a/src/Plugins/Tia/WatchDefaults/Symfony.php +++ b/src/Plugins/Tia/WatchDefaults/Symfony.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Pest\Plugins\Tia\WatchDefaults; -use Composer\InstalledVersions; use Pest\Plugins\Tia\Contracts\WatchDefault; +use Pest\Support\InstalledPackages; /** * @internal @@ -14,8 +14,7 @@ final readonly class Symfony implements WatchDefault { public function applicable(): bool { - return class_exists(InstalledVersions::class) - && InstalledVersions::isInstalled('symfony/framework-bundle'); + return InstalledPackages::has('symfony/framework-bundle'); } public function defaults(string $projectRoot, string $testPath): array diff --git a/src/Support/InstalledPackages.php b/src/Support/InstalledPackages.php new file mode 100644 index 00000000..d96c2dff --- /dev/null +++ b/src/Support/InstalledPackages.php @@ -0,0 +1,55 @@ +}> $installedSets + */ + public function __construct(private array $installedSets) {} + + public static function fromComposer(): self + { + if (! class_exists(InstalledVersions::class)) { + return new self([]); + } + + /** @var array}> $installedSets */ + $installedSets = InstalledVersions::getAllRawData(); + + return new self($installedSets); + } + + public static function has(string $package, bool $includeDevRequirements = true): bool + { + return self::fromComposer()->contains($package, $includeDevRequirements); + } + + public function contains(string $package, bool $includeDevRequirements = true): bool + { + foreach ($this->installedSets as $installedSet) { + if ($this->isBundledInPhar($installedSet)) { + continue; + } + + if (isset($installedSet['versions'][$package])) { + return $includeDevRequirements || ($installedSet['versions'][$package]['dev_requirement'] ?? false) === false; + } + } + + return false; + } + + /** + * @param array{root: array{install_path?: string}} $installedSet + */ + private function isBundledInPhar(array $installedSet): bool + { + return str_starts_with($installedSet['root']['install_path'] ?? '', 'phar://'); + } +} diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index e5ce6cbf..28134166 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1326,6 +1326,7 @@ ✓ init creates a suite when --init is not the first argument ✓ init creates a suite when --init is passed with a value ✓ a missing tests directory does not crash while resolving tia state + ✓ init writes the plain stubs when only a tool bundled in a PHAR installs Laravel PASS Tests\Features\Issue ✓ it may be associated with an issue #1, #2 @@ -2441,6 +2442,14 @@ PASS Tests\Unit\Support\HigherOrderMessage ✓ undefined method exceptions + PASS Tests\Unit\Support\InstalledPackages + ✓ it finds a package that the project installs + ✓ it ignores a package that only a tool bundled in a PHAR installs + ✓ it leaves out a development requirement when asked + ✓ it reads an installed set that has no install path + ✓ it reads the packages that Composer loaded + ✓ it is the only class of src that reads Composer packages + PASS Tests\Unit\Support\Reflection ✓ it gets file name from closure ✓ it gets property values @@ -2681,4 +2690,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, 39 skipped, 1970 passed (4460 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1977 passed (4474 assertions) \ No newline at end of file diff --git a/tests/Features/Init.php b/tests/Features/Init.php index 9fa04ae8..b6e27fde 100644 --- a/tests/Features/Init.php +++ b/tests/Features/Init.php @@ -74,6 +74,23 @@ test('a missing tests directory does not crash while resolving tia state', funct } }); +test('init writes the plain stubs when only a tool bundled in a PHAR installs Laravel', function (): void { + $root = makeInitProject(); + + try { + $toolLoader = makeLaravelToolPhar($root); + + $process = runInitProjectWithPhpOptions(['-d', 'auto_prepend_file='.$toolLoader], $root, '--init'); + + expect($process->getExitCode())->toBe(0) + ->and(file_get_contents($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')) + ->toContain("require_once __DIR__.'/TestCase.php';") + ->not->toContain('RefreshDatabase'); + } finally { + removeInitProject($root); + } +})->skipOnWindows(); + function makeInitProject(): string { $root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-init-'.bin2hex(random_bytes(4)); @@ -134,10 +151,55 @@ PHP); return $root; } +function makeLaravelToolPhar(string $root): string +{ + $phar = $root.DIRECTORY_SEPARATOR.'tool.phar'; + + $build = new Process([ + PHP_BINARY, + '-d', + 'phar.readonly=0', + '-r', + <<<'PHP' + $phar = new Phar($argv[1]); + $phar->addFromString('vendor/composer/installed.php', <<<'INSTALLED' + ['name' => 'tool/dumper', 'install_path' => __DIR__.'/../../'], + 'versions' => ['laravel/framework' => ['pretty_version' => 'v12.0.0', 'version' => '12.0.0.0', 'dev_requirement' => false]], + ]; + INSTALLED); + $phar->setStub('mustRun(); + + $loader = $root.DIRECTORY_SEPARATOR.'tool-loader.php'; + + file_put_contents($loader, sprintf( + "register();\n", + var_export(dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'composer'.DIRECTORY_SEPARATOR.'ClassLoader.php', true), + var_export('phar://'.$phar.'/vendor', true), + )); + + return $loader; +} + function runInitProject(string $root, string ...$command): Process +{ + return runInitProjectWithPhpOptions([], $root, ...$command); +} + +/** + * @param array $phpOptions + */ +function runInitProjectWithPhpOptions(array $phpOptions, string $root, string ...$command): Process { $process = new Process( - [PHP_BINARY, 'run.php', ...$command], + [PHP_BINARY, ...$phpOptions, 'run.php', ...$command], $root, [ 'PEST_NO_SUPPORT' => 'true', diff --git a/tests/Unit/Support/InstalledPackages.php b/tests/Unit/Support/InstalledPackages.php new file mode 100644 index 00000000..fb007a12 --- /dev/null +++ b/tests/Unit/Support/InstalledPackages.php @@ -0,0 +1,68 @@ + ['install_path' => '/project/vendor/composer/../../'], + 'versions' => [ + 'pestphp/pest' => ['dev_requirement' => true], + 'symfony/console' => ['dev_requirement' => false], + ], +]; + +$pharTool = [ + 'root' => ['install_path' => 'phar:///tools/dump.phar/vendor/composer/../../'], + 'versions' => [ + 'laravel/framework' => ['dev_requirement' => false], + ], +]; + +it('finds a package that the project installs', function () use ($project, $pharTool): void { + $packages = new InstalledPackages([$pharTool, $project]); + + expect($packages->contains('symfony/console'))->toBeTrue() + ->and($packages->contains('pestphp/pest'))->toBeTrue(); +}); + +it('ignores a package that only a tool bundled in a PHAR installs', function () use ($project, $pharTool): void { + $packages = new InstalledPackages([$pharTool, $project]); + + expect($packages->contains('laravel/framework'))->toBeFalse() + ->and($packages->contains('laravel/framework', false))->toBeFalse(); +}); + +it('leaves out a development requirement when asked', function () use ($project): void { + $packages = new InstalledPackages([$project]); + + expect($packages->contains('pestphp/pest', false))->toBeFalse() + ->and($packages->contains('symfony/console', false))->toBeTrue(); +}); + +it('reads an installed set that has no install path', function (): void { + $packages = new InstalledPackages([ + ['root' => [], 'versions' => ['laravel/framework' => ['dev_requirement' => false]]], + ]); + + expect($packages->contains('laravel/framework'))->toBeTrue() + ->and($packages->contains('symfony/console'))->toBeFalse(); +}); + +it('reads the packages that Composer loaded', function (): void { + expect(InstalledPackages::has('pestphp/pest'))->toBeTrue() + ->and(InstalledPackages::has('pestphp/pest-plugin-that-does-not-exist'))->toBeFalse(); +}); + +it('is the only class of src that reads Composer packages', function (): void { + $source = dirname(__DIR__, 3).DIRECTORY_SEPARATOR.'src'; + $readers = []; + + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS)) as $file) { + if ($file->getExtension() === 'php' && str_contains((string) file_get_contents($file->getPathname()), 'InstalledVersions')) { + $readers[] = substr($file->getPathname(), strlen($source) + 1); + } + } + + expect($readers)->toBe(['Support'.DIRECTORY_SEPARATOR.'InstalledPackages.php']); +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 51a766d7..a0299c48 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -21,7 +21,7 @@ test('parallel', function () use ($run): void { $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); $profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile'); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1843 passed (3935 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1850 passed (3949 assertions)'; expect($output) ->toContain("Tests: {$expected}")