From 4164b302367ca890066dd4a67c032b129704972a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 13 Sep 2022 14:49:25 +0200 Subject: [PATCH] Fix show --platform regression failing if no composer.json exists, fixes #11046 --- src/Composer/Command/ShowCommand.php | 7 ++++++- tests/Composer/Test/Command/ShowCommandTest.php | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index 2ec21c33fcb6..47ef25de9bb7 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -677,7 +677,12 @@ private function printPackages(IOInterface $io, array $packages, string $indent, */ protected function getRootRequires(): array { - $rootPackage = $this->requireComposer()->getPackage(); + $composer = $this->tryComposer(); + if ($composer === null) { + return []; + } + + $rootPackage = $composer->getPackage(); return array_map( 'strtolower', diff --git a/tests/Composer/Test/Command/ShowCommandTest.php b/tests/Composer/Test/Command/ShowCommandTest.php index 9c9880203448..540faeabab7f 100644 --- a/tests/Composer/Test/Command/ShowCommandTest.php +++ b/tests/Composer/Test/Command/ShowCommandTest.php @@ -192,6 +192,20 @@ public function testShowPlatformOnlyShowsPlatformPackages(): void } } + public function testShowPlatformWorksWithoutComposerJson(): void + { + $this->initTempComposer([]); + unlink('./composer.json'); + unlink('./auth.json'); + + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'show', '-p' => true]); + $output = trim($appTester->getDisplay(true)); + foreach (Regex::matchAll('{^(\w+)}m', $output)->matches as $m) { + self::assertTrue(PlatformRepository::isPlatformPackage((string) $m[1])); + } + } + public function testOutdatedWithZeroMajor(): void { $this->initTempComposer([