fix: ignore packages bundled in a PHAR when detecting installed packages (#2059)

Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
Jhon Farias
2026-11-26 15:04:03 +01:00
committed by GitHub
parent 5bb349dcf2
commit dc73ec009e
12 changed files with 212 additions and 23 deletions
+2 -2
View File
@@ -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');
}
}
+2 -2
View File
@@ -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) {
+2 -3
View File
@@ -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
+3 -4
View File
@@ -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
+2 -3
View File
@@ -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
+2 -3
View File
@@ -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
+2 -3
View File
@@ -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
+55
View File
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Composer\InstalledVersions;
final readonly class InstalledPackages
{
/**
* @param array<int, array{root: array{install_path?: string}, versions: array<string, array{dev_requirement?: bool}>}> $installedSets
*/
public function __construct(private array $installedSets) {}
public static function fromComposer(): self
{
if (! class_exists(InstalledVersions::class)) {
return new self([]);
}
/** @var array<int, array{root: array{install_path?: string}, versions: array<string, array{dev_requirement?: bool}>}> $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://');
}
}
+10 -1
View File
@@ -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)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1977 passed (4474 assertions)
+63 -1
View File
@@ -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'
<?php
return [
'root' => ['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('<?php __HALT_COMPILER();');
PHP,
$phar,
]);
$build->mustRun();
$loader = $root.DIRECTORY_SEPARATOR.'tool-loader.php';
file_put_contents($loader, sprintf(
"<?php\n\nrequire_once %s;\n\n(new Composer\\Autoload\\ClassLoader(%s))->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<int, string> $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',
+68
View File
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
use Pest\Support\InstalledPackages;
$project = [
'root' => ['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']);
});
+1 -1
View File
@@ -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}")