1
0
Fork 0

Update VersionSelector to take all platform requirements into account when selecting packages

pull/8873/head
Jordi Boggiano 2020-05-04 21:12:21 +02:00
parent b11f43f59e
commit 419567ba6d
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
3 changed files with 105 additions and 23 deletions

View File

@ -27,6 +27,7 @@
- packages now contain an `"installed-path"` key which lists where they were installed - packages now contain an `"installed-path"` key which lists where they were installed
- there is a top level `"dev"` key which stores whether dev requirements were installed or not - there is a top level `"dev"` key which stores whether dev requirements were installed or not
- `PreFileDownloadEvent` now receives an `HttpDownloader` instance instead of `RemoteFilesystem`, and that instance can not be overridden by listeners anymore - `PreFileDownloadEvent` now receives an `HttpDownloader` instance instead of `RemoteFilesystem`, and that instance can not be overridden by listeners anymore
- `VersionSelector::findBestCandidate`'s third argument (phpVersion) was removed in favor of passing in a complete PlatformRepository instance into the constructor
- `IOInterface` now extends PSR-3's `LoggerInterface`, and has new `writeRaw` + `writeErrorRaw` methods - `IOInterface` now extends PSR-3's `LoggerInterface`, and has new `writeRaw` + `writeErrorRaw` methods
- `RepositoryInterface` changes: - `RepositoryInterface` changes:
- A new `loadPackages(array $packageNameMap, array $acceptableStabilities, array $stabilityFlags)` function was added for use during pool building - A new `loadPackages(array $packageNameMap, array $acceptableStabilities, array $stabilityFlags)` function was added for use during pool building

View File

@ -20,6 +20,7 @@ use Composer\Composer;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\Package\Dumper\ArrayDumper; use Composer\Package\Dumper\ArrayDumper;
use Composer\Repository\RepositorySet; use Composer\Repository\RepositorySet;
use Composer\Repository\PlatformRepository;
use Composer\Semver\Constraint\Constraint; use Composer\Semver\Constraint\Constraint;
/** /**
@ -32,11 +33,22 @@ class VersionSelector
{ {
private $repositorySet; private $repositorySet;
private $platformConstraints;
private $parser; private $parser;
public function __construct(RepositorySet $repositorySet) /**
* @param PlatformRepository $platformRepo If passed in, the versions found will be filtered against their requirements to eliminate any not matching the current platform packages
*/
public function __construct(RepositorySet $repositorySet, PlatformRepository $platformRepo = null)
{ {
$this->repositorySet = $repositorySet; $this->repositorySet = $repositorySet;
if ($platformRepo) {
$this->platformConstraints = array();
foreach ($platformRepo->getPackages() as $package) {
$this->platformConstraints[$package->getName()][] = new Constraint('==', $package->getVersion());
}
}
} }
/** /**
@ -45,25 +57,38 @@ class VersionSelector
* *
* @param string $packageName * @param string $packageName
* @param string $targetPackageVersion * @param string $targetPackageVersion
* @param string $targetPhpVersion
* @param string $preferredStability * @param string $preferredStability
* @param bool $ignorePlatformReqs
* @return PackageInterface|false * @return PackageInterface|false
*/ */
public function findBestCandidate($packageName, $targetPackageVersion = null, $targetPhpVersion = null, $preferredStability = 'stable') public function findBestCandidate($packageName, $targetPackageVersion = null, $preferredStability = 'stable', $ignorePlatformReqs = false)
{ {
if (!isset(BasePackage::$stabilities[$preferredStability])) {
// If you get this, maybe you are still relying on the Composer 1.x signature where the 3rd arg was the php version
throw new \UnexpectedValueException('Expected a valid stability name as 3rd argument, got '.$preferredStability);
}
$constraint = $targetPackageVersion ? $this->getParser()->parseConstraints($targetPackageVersion) : null; $constraint = $targetPackageVersion ? $this->getParser()->parseConstraints($targetPackageVersion) : null;
$candidates = $this->repositorySet->findPackages(strtolower($packageName), $constraint); $candidates = $this->repositorySet->findPackages(strtolower($packageName), $constraint);
if ($targetPhpVersion) { if ($this->platformConstraints && !$ignorePlatformReqs) {
$phpConstraint = new Constraint('==', $this->getParser()->normalize($targetPhpVersion)); $platformConstraints = $this->platformConstraints;
$composerRuntimeConstraint = new Constraint('==', $this->getParser()->normalize(Composer::RUNTIME_API_VERSION)); $candidates = array_filter($candidates, function ($pkg) use ($platformConstraints) {
$composerPluginConstraint = new Constraint('==', $this->getParser()->normalize(PluginInterface::PLUGIN_API_VERSION));
$candidates = array_filter($candidates, function ($pkg) use ($phpConstraint, $composerPluginConstraint, $composerRuntimeConstraint) {
$reqs = $pkg->getRequires(); $reqs = $pkg->getRequires();
return (!isset($reqs['php']) || $reqs['php']->getConstraint()->matches($phpConstraint)) foreach ($reqs as $name => $link) {
&& (!isset($reqs['composer-plugin-api']) || $reqs['composer-plugin-api']->getConstraint()->matches($composerPluginConstraint)) if (isset($platformConstraints[$name])) {
&& (!isset($reqs['composer-runtime-api']) || $reqs['composer-runtime-api']->getConstraint()->matches($composerRuntimeConstraint)); foreach ($platformConstraints[$name] as $constraint) {
if ($link->getConstraint()->matches($constraint)) {
continue 2;
}
}
return false;
}
}
return true;
}); });
} }

View File

@ -15,6 +15,7 @@ namespace Composer\Test\Package\Version;
use Composer\Package\Version\VersionSelector; use Composer\Package\Version\VersionSelector;
use Composer\Package\Package; use Composer\Package\Package;
use Composer\Package\Link; use Composer\Package\Link;
use Composer\Repository\PlatformRepository;
use Composer\Semver\VersionParser; use Composer\Semver\VersionParser;
use Composer\Test\TestCase; use Composer\Test\TestCase;
@ -46,27 +47,82 @@ class VersionSelectorTest extends TestCase
$this->assertSame($package2, $best, 'Latest version should be 1.2.2'); $this->assertSame($package2, $best, 'Latest version should be 1.2.2');
} }
public function testLatestVersionIsReturnedThatMatchesPhpRequirement() public function testLatestVersionIsReturnedThatMatchesPhpRequirements()
{ {
$packageName = 'foobar'; $packageName = 'foobar';
$platform = new PlatformRepository(array(), array('php' => '5.5.0'));
$repositorySet = $this->createMockRepositorySet();
$versionSelector = new VersionSelector($repositorySet, $platform);
$parser = new VersionParser; $parser = new VersionParser;
$package1 = $this->createPackage('1.0.0'); $package1 = $this->createPackage('1.0.0');
$package2 = $this->createPackage('2.0.0');
$package1->setRequires(array('php' => new Link($packageName, 'php', $parser->parseConstraints('>=5.4'), 'requires', '>=5.4'))); $package1->setRequires(array('php' => new Link($packageName, 'php', $parser->parseConstraints('>=5.4'), 'requires', '>=5.4')));
$package2 = $this->createPackage('2.0.0');
$package2->setRequires(array('php' => new Link($packageName, 'php', $parser->parseConstraints('>=5.6'), 'requires', '>=5.6'))); $package2->setRequires(array('php' => new Link($packageName, 'php', $parser->parseConstraints('>=5.6'), 'requires', '>=5.6')));
$packages = array($package1, $package2); $packages = array($package1, $package2);
$repositorySet = $this->createMockRepositorySet(); $repositorySet->expects($this->any())
$repositorySet->expects($this->once())
->method('findPackages') ->method('findPackages')
->with($packageName, null) ->with($packageName, null)
->will($this->returnValue($packages)); ->will($this->returnValue($packages));
$versionSelector = new VersionSelector($repositorySet); $best = $versionSelector->findBestCandidate($packageName);
$best = $versionSelector->findBestCandidate($packageName, null, '5.5.0');
$this->assertSame($package1, $best, 'Latest version supporting php 5.5 should be returned (1.0.0)'); $this->assertSame($package1, $best, 'Latest version supporting php 5.5 should be returned (1.0.0)');
$best = $versionSelector->findBestCandidate($packageName, null, 'stable', true);
$this->assertSame($package2, $best, 'Latest version should be returned when ignoring platform reqs (2.0.0)');
}
public function testLatestVersionIsReturnedThatMatchesExtRequirements()
{
$packageName = 'foobar';
$platform = new PlatformRepository(array(), array('ext-zip' => '5.3.0'));
$repositorySet = $this->createMockRepositorySet();
$versionSelector = new VersionSelector($repositorySet, $platform);
$parser = new VersionParser;
$package1 = $this->createPackage('1.0.0');
$package1->setRequires(array('ext-zip' => new Link($packageName, 'ext-zip', $parser->parseConstraints('^5.2'), 'requires', '^5.2')));
$package2 = $this->createPackage('2.0.0');
$package2->setRequires(array('ext-zip' => new Link($packageName, 'ext-zip', $parser->parseConstraints('^5.4'), 'requires', '^5.4')));
$packages = array($package1, $package2);
$repositorySet->expects($this->any())
->method('findPackages')
->with($packageName, null)
->will($this->returnValue($packages));
$best = $versionSelector->findBestCandidate($packageName);
$this->assertSame($package1, $best, 'Latest version supporting ext-zip 5.3.0 should be returned (1.0.0)');
$best = $versionSelector->findBestCandidate($packageName, null, 'stable', true);
$this->assertSame($package2, $best, 'Latest version should be returned when ignoring platform reqs (2.0.0)');
}
public function testLatestVersionIsReturnedThatMatchesComposerRequirements()
{
$packageName = 'foobar';
$platform = new PlatformRepository(array(), array('composer-runtime-api' => '1.0.0'));
$repositorySet = $this->createMockRepositorySet();
$versionSelector = new VersionSelector($repositorySet, $platform);
$parser = new VersionParser;
$package1 = $this->createPackage('1.0.0');
$package1->setRequires(array('composer-runtime-api' => new Link($packageName, 'composer-runtime-api', $parser->parseConstraints('^1.0'), 'requires', '^1.0')));
$package2 = $this->createPackage('1.1.0');
$package2->setRequires(array('composer-runtime-api' => new Link($packageName, 'composer-runtime-api', $parser->parseConstraints('^2.0'), 'requires', '^2.0')));
$packages = array($package1, $package2);
$repositorySet->expects($this->any())
->method('findPackages')
->with($packageName, null)
->will($this->returnValue($packages));
$best = $versionSelector->findBestCandidate($packageName);
$this->assertSame($package1, $best, 'Latest version supporting composer 1 should be returned (1.0.0)');
$best = $versionSelector->findBestCandidate($packageName, null, 'stable', true);
$this->assertSame($package2, $best, 'Latest version should be returned when ignoring platform reqs (1.1.0)');
} }
public function testMostStableVersionIsReturned() public function testMostStableVersionIsReturned()
@ -109,10 +165,10 @@ class VersionSelectorTest extends TestCase
->will($this->returnValue(array_reverse($packages))); ->will($this->returnValue(array_reverse($packages)));
$versionSelector = new VersionSelector($repositorySet); $versionSelector = new VersionSelector($repositorySet);
$best = $versionSelector->findBestCandidate($packageName, null, null); $best = $versionSelector->findBestCandidate($packageName);
$this->assertSame($package2, $best, 'Expecting 2.0.0-beta3, cause beta is more stable than dev'); $this->assertSame($package2, $best, 'Expecting 2.0.0-beta3, cause beta is more stable than dev');
$best = $versionSelector->findBestCandidate($packageName, null, null); $best = $versionSelector->findBestCandidate($packageName);
$this->assertSame($package2, $best, 'Expecting 2.0.0-beta3, cause beta is more stable than dev'); $this->assertSame($package2, $best, 'Expecting 2.0.0-beta3, cause beta is more stable than dev');
} }
@ -131,7 +187,7 @@ class VersionSelectorTest extends TestCase
->will($this->returnValue($packages)); ->will($this->returnValue($packages));
$versionSelector = new VersionSelector($repositorySet); $versionSelector = new VersionSelector($repositorySet);
$best = $versionSelector->findBestCandidate($packageName, null, null, 'dev'); $best = $versionSelector->findBestCandidate($packageName, null, 'dev');
$this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)'); $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
} }
@ -152,7 +208,7 @@ class VersionSelectorTest extends TestCase
->will($this->returnValue($packages)); ->will($this->returnValue($packages));
$versionSelector = new VersionSelector($repositorySet); $versionSelector = new VersionSelector($repositorySet);
$best = $versionSelector->findBestCandidate($packageName, null, null, 'beta'); $best = $versionSelector->findBestCandidate($packageName, null, 'beta');
$this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)'); $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
} }
@ -172,7 +228,7 @@ class VersionSelectorTest extends TestCase
->will($this->returnValue($packages)); ->will($this->returnValue($packages));
$versionSelector = new VersionSelector($repositorySet); $versionSelector = new VersionSelector($repositorySet);
$best = $versionSelector->findBestCandidate($packageName, null, null, 'stable'); $best = $versionSelector->findBestCandidate($packageName, null, 'stable');
$this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)'); $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
} }