From 607dcd07980154e3d82e2ee09005d522663c4a76 Mon Sep 17 00:00:00 2001 From: Chris Wells <8843-cwells@users.noreply.drupalcode.org> Date: Wed, 18 Oct 2023 01:28:29 +0000 Subject: [PATCH] Issue #3349966 by phenaproxima, chrisfromredfin, tedbow, rkoller: ComposerInspector should account for the way `composer show` displays versions of packages installed from dev snapshots, to avoid angering Semver::satisfies() --- package_manager/src/ComposerInspector.php | 9 ++++++ .../src/Kernel/ComposerInspectorTest.php | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/package_manager/src/ComposerInspector.php b/package_manager/src/ComposerInspector.php index ed90560377..70347ccc9d 100644 --- a/package_manager/src/ComposerInspector.php +++ b/package_manager/src/ComposerInspector.php @@ -315,6 +315,15 @@ class ComposerInspector implements LoggerAwareInterface { foreach ($packages_data as $name => $package) { $path = $package['path']; + // For packages installed as dev snapshots from certain version control + // systems, `composer show` displays the version like `1.0.x-dev 0a1b2c`, + // which will cause an exception if we try to parse it as a legitimate + // semantic version. Since we don't need the abbreviated commit hash, just + // remove it. + if (str_contains($package['version'], '-dev ')) { + $packages_data[$name]['version'] = explode(' ', $package['version'], 2)[0]; + } + // We expect Composer to report that metapackages' install paths are the // same as the working directory, in which case InstalledPackage::$path // should be NULL. For all other package types, we consider it invalid diff --git a/package_manager/tests/src/Kernel/ComposerInspectorTest.php b/package_manager/tests/src/Kernel/ComposerInspectorTest.php index c38e5fc00c..835a76d1d7 100644 --- a/package_manager/tests/src/Kernel/ComposerInspectorTest.php +++ b/package_manager/tests/src/Kernel/ComposerInspectorTest.php @@ -417,6 +417,36 @@ class ComposerInspectorTest extends PackageManagerKernelTestBase { $this->assertSame($is_metapackage, is_null($list['test/package']->path)); } + /** + * Tests that the commit hash of a dev snapshot package is ignored. + */ + public function testPackageDevSnapshotCommitHashIsRemoved(): void { + $inspector = new class ( + $this->container->get(ComposerProcessRunnerInterface::class), + $this->container->get(ComposerIsAvailableInterface::class), + $this->container->get(PathFactoryInterface::class), + ) extends ComposerInspector { + + /** + * {@inheritdoc} + */ + protected function show(string $working_dir): array { + return [ + 'test/package' => [ + 'name' => 'test/package', + 'path' => __DIR__, + 'version' => '1.0.x-dev 0a1b2c3d', + ], + ]; + } + + }; + $project_root = $this->container->get(PathLocator::class) + ->getProjectRoot(); + $list = $inspector->getInstalledPackagesList($project_root); + $this->assertSame('1.0.x-dev', $list['test/package']->version); + } + /** * Data provider for ::testAllowedPlugins(). * -- GitLab