1
0
Fork 0

Fix update whitelist pattern resolving and add more tests

pull/7926/head
Sascha Egerer 2018-12-13 13:54:22 +01:00 committed by Jordi Boggiano
parent dc59af555a
commit 1845adcfbd
6 changed files with 165 additions and 16 deletions

View File

@ -1301,11 +1301,6 @@ class Installer
$rootRequires = array_merge($rootRequires, $rootDevRequires); $rootRequires = array_merge($rootRequires, $rootDevRequires);
$requiredPackageNames = array();
foreach ($rootRequires as $require) {
$requiredPackageNames[] = $require->getTarget();
}
$skipPackages = array(); $skipPackages = array();
if (!$this->whitelistAllDependencies) { if (!$this->whitelistAllDependencies) {
foreach ($rootRequires as $require) { foreach ($rootRequires as $require) {
@ -1323,22 +1318,26 @@ class Installer
foreach ($this->updateWhitelist as $packageName => $void) { foreach ($this->updateWhitelist as $packageName => $void) {
$packageQueue = new \SplQueue; $packageQueue = new \SplQueue;
$depPackages = [$pool->whatProvides($packageName)]; $depPackages = $pool->whatProvides($packageName);
$matchesByPattern = [];
$nameMatchesRequiredPackage = in_array($packageName, $requiredPackageNames, true);
// check if the name is a glob pattern that did not match directly // check if the name is a glob pattern that did not match directly
if (!$nameMatchesRequiredPackage) { if (empty($depPackages)) {
$whitelistPatternSearchRegexp = BasePackage::packageNameToRegexp($packageName, '^%s$');
foreach ($localOrLockRepo->search($whitelistPatternSearchRegexp) as $installedPackage) {
$matchesByPattern[] = $pool->whatProvides($installedPackage['name']);
}
$whitelistPatternRegexp = BasePackage::packageNameToRegexp($packageName); $whitelistPatternRegexp = BasePackage::packageNameToRegexp($packageName);
foreach ($rootRequiredPackageNames as $rootRequiredPackageName) { foreach ($rootRequiredPackageNames as $rootRequiredPackageName) {
if (preg_match($whitelistPatternRegexp, $rootRequiredPackageName)) { if (preg_match($whitelistPatternRegexp, $rootRequiredPackageName)) {
$depPackages[] = $pool->whatProvides($rootRequiredPackageName);
$nameMatchesRequiredPackage = true; $nameMatchesRequiredPackage = true;
break;
} }
} }
} }
$depPackages = array_merge(...$depPackages); if (!empty($matchesByPattern)) {
$depPackages = array_merge($depPackages, array_merge(...$matchesByPattern));
}
if (count($depPackages) == 0 && !$nameMatchesRequiredPackage && !in_array($packageName, array('nothing', 'lock', 'mirrors'))) { if (count($depPackages) == 0 && !$nameMatchesRequiredPackage && !in_array($packageName, array('nothing', 'lock', 'mirrors'))) {
$this->io->writeError('<warning>Package "' . $packageName . '" listed for update is not installed. Ignoring.</warning>'); $this->io->writeError('<warning>Package "' . $packageName . '" listed for update is not installed. Ignoring.</warning>');
@ -1371,7 +1370,7 @@ class Installer
continue; continue;
} }
if (isset($skipPackages[$requirePackage->getName()])) { if (isset($skipPackages[$requirePackage->getName()]) && !preg_match(BasePackage::packageNameToRegexp($packageName), $requirePackage->getName())) {
$this->io->writeError('<warning>Dependency "' . $requirePackage->getName() . '" is also a root requirement, but is not explicitly whitelisted. Ignoring.</warning>'); $this->io->writeError('<warning>Dependency "' . $requirePackage->getName() . '" is also a root requirement, but is not explicitly whitelisted. Ignoring.</warning>');
continue; continue;
} }

View File

@ -239,12 +239,13 @@ abstract class BasePackage implements PackageInterface
* Build a regexp from a package name, expanding * globs as required * Build a regexp from a package name, expanding * globs as required
* *
* @param string $whiteListedPattern * @param string $whiteListedPattern
* @param bool $wrap Wrap the cleaned string by the given string
* @return string * @return string
*/ */
public static function packageNameToRegexp($whiteListedPattern) public static function packageNameToRegexp($whiteListedPattern, $wrap = '{^%s$}i')
{ {
$cleanedWhiteListedPattern = str_replace('\\*', '.*', preg_quote($whiteListedPattern)); $cleanedWhiteListedPattern = str_replace('\\*', '.*', preg_quote($whiteListedPattern));
return "{^" . $cleanedWhiteListedPattern . "$}i"; return sprintf($wrap, $cleanedWhiteListedPattern);
} }
} }

View File

@ -0,0 +1,46 @@
--TEST--
Update with a package whitelist pattern and all-dependencies flag updates packages and their dependencies, even if defined as root dependency, matching the pattern
--COMPOSER--
{
"repositories": [
{
"type": "package",
"package": [
{ "name": "fixed", "version": "1.1.0" },
{ "name": "fixed", "version": "1.0.0" },
{ "name": "whitelisted-component1", "version": "1.1.0" },
{ "name": "whitelisted-component1", "version": "1.0.0" },
{ "name": "whitelisted-component2", "version": "1.1.0", "require": { "dependency": "1.*" } },
{ "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.*" } },
{ "name": "dependency", "version": "1.1.0" },
{ "name": "dependency", "version": "1.0.0" },
{ "name": "unrelated", "version": "1.1.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated", "version": "1.0.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated-dependency", "version": "1.1.0" },
{ "name": "unrelated-dependency", "version": "1.0.0" }
]
}
],
"require": {
"fixed": "1.*",
"whitelisted-component1": "1.*",
"whitelisted-component2": "1.*",
"dependency": "1.*",
"unrelated": "1.*"
}
}
--INSTALLED--
[
{ "name": "fixed", "version": "1.0.0" },
{ "name": "whitelisted-component1", "version": "1.0.0" },
{ "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.0.0" } },
{ "name": "dependency", "version": "1.0.0" },
{ "name": "unrelated", "version": "1.0.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated-dependency", "version": "1.0.0" }
]
--RUN--
update whitelisted-* --with-all-dependencies
--EXPECT--
Updating whitelisted-component1 (1.0.0) to whitelisted-component1 (1.1.0)
Updating dependency (1.0.0) to dependency (1.1.0)
Updating whitelisted-component2 (1.0.0) to whitelisted-component2 (1.1.0)

View File

@ -0,0 +1,49 @@
--TEST--
Update with a package whitelist only updates those packages and their dependencies matching the pattern but no dependencies defined as roo package
--COMPOSER--
{
"repositories": [
{
"type": "package",
"package": [
{ "name": "fixed", "version": "1.1.0" },
{ "name": "fixed", "version": "1.0.0" },
{ "name": "whitelisted-component1", "version": "1.1.0" },
{ "name": "whitelisted-component1", "version": "1.0.0" },
{ "name": "whitelisted-component2", "version": "1.1.0", "require": { "dependency": "1.*", "root-dependency": "1.*" } },
{ "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.*", "root-dependency": "1.*" } },
{ "name": "dependency", "version": "1.1.0" },
{ "name": "dependency", "version": "1.0.0" },
{ "name": "root-dependency", "version": "1.1.0" },
{ "name": "root-dependency", "version": "1.0.0" },
{ "name": "unrelated", "version": "1.1.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated", "version": "1.0.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated-dependency", "version": "1.1.0" },
{ "name": "unrelated-dependency", "version": "1.0.0" }
]
}
],
"require": {
"fixed": "1.*",
"whitelisted-component1": "1.*",
"whitelisted-component2": "1.*",
"root-dependency": "1.*",
"unrelated": "1.*"
}
}
--INSTALLED--
[
{ "name": "fixed", "version": "1.0.0" },
{ "name": "whitelisted-component1", "version": "1.0.0" },
{ "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.0.0" } },
{ "name": "root-dependency", "version": "1.0.0" },
{ "name": "dependency", "version": "1.0.0" },
{ "name": "unrelated", "version": "1.0.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated-dependency", "version": "1.0.0" }
]
--RUN--
update whitelisted-* --with-dependencies
--EXPECT--
Updating whitelisted-component1 (1.0.0) to whitelisted-component1 (1.1.0)
Updating dependency (1.0.0) to dependency (1.1.0)
Updating whitelisted-component2 (1.0.0) to whitelisted-component2 (1.1.0)

View File

@ -10,8 +10,14 @@ Update with a package whitelist only updates those packages and their dependenci
{ "name": "fixed", "version": "1.0.0" }, { "name": "fixed", "version": "1.0.0" },
{ "name": "whitelisted-component1", "version": "1.1.0", "require": { "whitelisted-component2": "1.1.0" } }, { "name": "whitelisted-component1", "version": "1.1.0", "require": { "whitelisted-component2": "1.1.0" } },
{ "name": "whitelisted-component1", "version": "1.0.0", "require": { "whitelisted-component2": "1.0.0" } }, { "name": "whitelisted-component1", "version": "1.0.0", "require": { "whitelisted-component2": "1.0.0" } },
{ "name": "whitelisted-component2", "version": "1.1.0", "require": { "dependency": "1.1.0" } }, { "name": "whitelisted-component2", "version": "1.1.0", "require": { "dependency": "1.1.0", "whitelisted-component5": "1.0.0" } },
{ "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.0.0" } }, { "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.0.0" } },
{ "name": "whitelisted-component3", "version": "1.1.0", "require": { "whitelisted-component4": "1.1.0" } },
{ "name": "whitelisted-component3", "version": "1.0.0", "require": { "whitelisted-component4": "1.0.0" } },
{ "name": "whitelisted-component4", "version": "1.1.0" },
{ "name": "whitelisted-component4", "version": "1.0.0" },
{ "name": "whitelisted-component5", "version": "1.1.0" },
{ "name": "whitelisted-component5", "version": "1.0.0" },
{ "name": "dependency", "version": "1.1.0" }, { "name": "dependency", "version": "1.1.0" },
{ "name": "dependency", "version": "1.0.0" }, { "name": "dependency", "version": "1.0.0" },
{ "name": "unrelated", "version": "1.1.0", "require": { "unrelated-dependency": "1.*" } }, { "name": "unrelated", "version": "1.1.0", "require": { "unrelated-dependency": "1.*" } },
@ -25,6 +31,7 @@ Update with a package whitelist only updates those packages and their dependenci
"fixed": "1.*", "fixed": "1.*",
"whitelisted-component1": "1.*", "whitelisted-component1": "1.*",
"whitelisted-component2": "1.*", "whitelisted-component2": "1.*",
"whitelisted-component3": "1.0.0",
"unrelated": "1.*" "unrelated": "1.*"
} }
} }
@ -33,6 +40,9 @@ Update with a package whitelist only updates those packages and their dependenci
{ "name": "fixed", "version": "1.0.0" }, { "name": "fixed", "version": "1.0.0" },
{ "name": "whitelisted-component1", "version": "1.0.0", "require": { "whitelisted-component2": "1.0.0" } }, { "name": "whitelisted-component1", "version": "1.0.0", "require": { "whitelisted-component2": "1.0.0" } },
{ "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.0.0" } }, { "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.0.0" } },
{ "name": "whitelisted-component3", "version": "1.0.0", "require": { "whitelisted-component4": "1.0.0" } },
{ "name": "whitelisted-component4", "version": "1.0.0" },
{ "name": "whitelisted-component5", "version": "1.0.0" },
{ "name": "dependency", "version": "1.0.0" }, { "name": "dependency", "version": "1.0.0" },
{ "name": "unrelated", "version": "1.0.0", "require": { "unrelated-dependency": "1.*" } }, { "name": "unrelated", "version": "1.0.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated-dependency", "version": "1.0.0" } { "name": "unrelated-dependency", "version": "1.0.0" }

View File

@ -0,0 +1,44 @@
--TEST--
Update with a package whitelist only updates those packages matching the pattern
--COMPOSER--
{
"repositories": [
{
"type": "package",
"package": [
{ "name": "fixed", "version": "1.1.0" },
{ "name": "fixed", "version": "1.0.0" },
{ "name": "whitelisted-component1", "version": "1.1.0" },
{ "name": "whitelisted-component1", "version": "1.0.0" },
{ "name": "whitelisted-component2", "version": "1.1.0", "require": { "dependency": "1.*" } },
{ "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.*" } },
{ "name": "dependency", "version": "1.1.0" },
{ "name": "dependency", "version": "1.0.0" },
{ "name": "unrelated", "version": "1.1.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated", "version": "1.0.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated-dependency", "version": "1.1.0" },
{ "name": "unrelated-dependency", "version": "1.0.0" }
]
}
],
"require": {
"fixed": "1.*",
"whitelisted-component1": "1.*",
"whitelisted-component2": "1.*",
"unrelated": "1.*"
}
}
--INSTALLED--
[
{ "name": "fixed", "version": "1.0.0" },
{ "name": "whitelisted-component1", "version": "1.0.0" },
{ "name": "whitelisted-component2", "version": "1.0.0", "require": { "dependency": "1.0.0" } },
{ "name": "dependency", "version": "1.0.0" },
{ "name": "unrelated", "version": "1.0.0", "require": { "unrelated-dependency": "1.*" } },
{ "name": "unrelated-dependency", "version": "1.0.0" }
]
--RUN--
update whitelisted-*
--EXPECT--
Updating whitelisted-component1 (1.0.0) to whitelisted-component1 (1.1.0)
Updating whitelisted-component2 (1.0.0) to whitelisted-component2 (1.1.0)