Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Do not ask for the same package if it is already prepared to be insta…
Browse files Browse the repository at this point in the history
…lled
  • Loading branch information
michalbundyra committed Nov 13, 2017
1 parent e09d76a commit 0a17a77
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ private function andDependencies(array $extra)
$packages = array_flip($deps);

foreach ($packages as $package => &$constraint) {
if ($this->isPackageReadyToInstall($package)) {
unset($packages[$package]);
continue;
}

if ($this->hasPackage($package)) {
unset($packages[$package]);
continue;
Expand Down Expand Up @@ -169,6 +174,11 @@ private function orDependencies(array $extra)
}

foreach ($options as $package) {
if ($this->isPackageReadyToInstall($package)) {
// Package has been already prepared to be installed, skipping.
continue 2;
}

if ($this->hasPackage($package)) {
// Package from this group has been found in root composer, skipping.
continue 2;
Expand Down Expand Up @@ -319,10 +329,25 @@ private function createInstaller(Composer $composer, IOInterface $io, RootPackag

private function hasPackage($package)
{
$lower = strtolower($package);

$rootPackage = $this->composer->getPackage();
$requires = $rootPackage->getRequires() + $rootPackage->getDevRequires();
foreach ($requires as $name => $link) {
if (strtolower($name) === strtolower($package)) {
if (strtolower($name) === $lower) {
return true;
}
}

return false;
}

private function isPackageReadyToInstall($package)
{
$lower = strtolower($package);

foreach ($this->packagesToInstall as $name => $version) {
if (strtolower($name) === $lower) {
return true;
}
}
Expand Down
69 changes: 69 additions & 0 deletions test/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,36 @@ public function testUpdateComposerWithCurrentlyInstalledVersion($operation)
$this->assertPackagesToInstall(['extra-dependency-foo' => '^0.5.1']);
}

/**
* @dataProvider operation
*
* @param string $operation
*/
public function testDoNotInstallAskTwiceForTheSamePackage($operation)
{
$this->injectPackages([
'extra-package' => '^1.0.1',
]);

$event = $this->getPackageEvent('some/component', [
'dependency' => [
'extra-package',
],
], $operation);

$this->composer->getPackage()->shouldNotBeCalled();

$this->io->isInteractive()->willReturn(true);
$this->io
->askAndValidate(Argument::any(), Argument::type('callable'))
->shouldNotBeCalled();

$this->assertNull($this->plugin->onPostPackage($event));
$this->assertPackagesToInstall([
'extra-package' => '^1.0.1',
]);
}

/**
* @dataProvider operation
*
Expand Down Expand Up @@ -881,6 +911,45 @@ public function testIntegrationHandleDependencyAndDependencyOr($operation)
]);
}

/**
* @dataProvider operation
*
* @param string $operation
*/
public function testIntegrationDoNotAskWhenAlreadyChosen($operation)
{
$event = $this->getPackageEvent('some/component', [
'dependency' => [
'extra-package-required',
],
'dependency-or' => [
'Choose something' => [
'extra-choose-one',
'extra-choose-two',
'extra-choose-three',
'extra-package-required',
],
],
], $operation);

$this->setUpRootPackage();

$this->io->isInteractive()->willReturn(true);
$this->io
->askAndValidate(
'Enter the version of <info>extra-package-required</info> to require'
. ' (or leave blank to use the latest version): ',
Argument::type('callable')
)
->willReturn('1.8.3')
->shouldBeCalledTimes(1);

$this->assertNull($this->plugin->onPostPackage($event));
$this->assertPackagesToInstall([
'extra-package-required' => '1.8.3',
]);
}

private function assertSetRequiresArgument($name, $version, array $arguments)
{
if (! isset($arguments[$name])) {
Expand Down

0 comments on commit 0a17a77

Please sign in to comment.