Skip to content

Commit

Permalink
Add error when composer show --direct <transient-dependency> is used …
Browse files Browse the repository at this point in the history
…to show a dependency which is not direct, fixes #11728
  • Loading branch information
Seldaek committed Jan 11, 2024
1 parent 3427bee commit 55db88f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Composer/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} elseif (null !== $packageFilter && !str_contains($packageFilter, '*')) {
[$package, $versions] = $this->getPackage($installedRepo, $repos, $packageFilter, $input->getArgument('version'));

if (isset($package) && $input->getOption('direct')) {
if (!in_array($package->getName(), $this->getRootRequires(), true)) {
throw new \InvalidArgumentException('Package "' . $package->getName() . '" is installed but not a direct dependent of the root package.');
}
}

if (!isset($package)) {
$options = $input->getOptions();
$hint = '';
Expand Down
18 changes: 18 additions & 0 deletions tests/Composer/Test/Command/ShowCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Composer\Repository\PlatformRepository;
use Composer\Test\TestCase;
use DateTimeImmutable;
use InvalidArgumentException;

class ShowCommandTest extends TestCase
{
Expand Down Expand Up @@ -298,6 +299,23 @@ public function testOutdatedFiltersAccordingToPlatformReqsWithoutWarningForHighe
vendor/package 1.1.0 <highlight>! 1.2.0</highlight>", trim($appTester->getDisplay(true)));
}

public function testShowDirectWithNameOnlyShowsDirectDependents(): void

This comment has been minimized.

Copy link
@stof

stof Jan 12, 2024

Contributor

we should add other tests for the positive case, showing that the package is properly display with --direct when it is a requirement or a dev requirement (so 2 new tests)

This comment has been minimized.

Copy link
@Seldaek

Seldaek Mar 21, 2024

Author Member

Done in 54870a7

{
self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage('Package "vendor/package" is installed but not a direct dependent of the root package.');

$this->initTempComposer([
'repositories' => [],
]);

$this->createInstalledJson([
self::getPackage('vendor/package', '1.0.0'),
]);

$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'show', '--direct' => true, 'package' => 'vendor/package']);
}

public function testShowPlatformOnlyShowsPlatformPackages(): void
{
$this->initTempComposer([
Expand Down

0 comments on commit 55db88f

Please sign in to comment.