From caebfe14ee0c507a8bc676943547de95c13d3b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Mon, 12 Dec 2022 12:59:26 +0100 Subject: [PATCH 1/3] Fix: Add test case for not dropping patch version --- tests/Composer/Test/Package/Version/VersionBumperTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Composer/Test/Package/Version/VersionBumperTest.php b/tests/Composer/Test/Package/Version/VersionBumperTest.php index 3beace12e..cde90c2d6 100644 --- a/tests/Composer/Test/Package/Version/VersionBumperTest.php +++ b/tests/Composer/Test/Package/Version/VersionBumperTest.php @@ -46,6 +46,8 @@ class VersionBumperTest extends TestCase yield 'upgrade caret' => ['^1.0', '1.2.1', '^1.2.1']; yield 'skip trailing .0s' => ['^1.0', '1.0.0', '^1.0']; yield 'skip trailing .0s/2' => ['^1.2', '1.2.0', '^1.2']; + yield 'preserve major.minor.patch format when installed minor is 0' => ['^1.0.0', '1.2.0', '^1.2.0']; + yield 'preserve major.minor.patch format when installed minor is 1' => ['^1.0.0', '1.2.1', '^1.2.1']; yield 'preserve multi constraints' => ['^1.2 || ^2.3', '1.3.2', '^1.3.2 || ^2.3']; yield 'preserve multi constraints/2' => ['^1.2 || ^2.3', '2.4.0', '^1.2 || ^2.4']; yield 'preserve multi constraints/3' => ['^1.2 || ^2.3 || ^2', '2.4.0', '^1.2 || ^2.4 || ^2.4']; From 7f9638f65a404e4ad3b69eda320575649377f758 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 17 Dec 2022 22:49:59 +0100 Subject: [PATCH 2/3] Fix implementation to fix tests, fixes #11220 --- src/Composer/Package/Version/VersionBumper.php | 7 ++++++- tests/Composer/Test/Package/Version/VersionBumperTest.php | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Composer/Package/Version/VersionBumper.php b/src/Composer/Package/Version/VersionBumper.php index a554d8c55..bf9ed1fca 100644 --- a/src/Composer/Package/Version/VersionBumper.php +++ b/src/Composer/Package/Version/VersionBumper.php @@ -34,6 +34,7 @@ class VersionBumper * For example: * * ^1.0 + 1.2.1 -> ^1.2.1 * * ^1.2 + 1.2.0 -> ^1.2 + * * ^1.2.0 + 1.3.0 -> ^1.3.0 * * ^1.2 || ^2.3 + 1.3.0 -> ^1.3 || ^2.3 * * ^1.2 || ^2.3 + 2.4.0 -> ^1.2 || ^2.4 * * ^3@dev + 3.2.99999-dev -> ^3.2@dev @@ -89,7 +90,11 @@ class VersionBumper if (Preg::isMatchAllWithOffsets($pattern, $prettyConstraint, $matches)) { $modified = $prettyConstraint; foreach (array_reverse($matches['constraint']) as $match) { - $modified = substr_replace($modified, $newPrettyConstraint, $match[1], Platform::strlen((string) $match[0])); + $suffix = ''; + if (substr_count($match[0], '.') === 2 && substr_count($newPrettyConstraint, '.') === 1) { + $suffix = '.0'; + } + $modified = substr_replace($modified, $newPrettyConstraint.$suffix, $match[1], Platform::strlen((string) $match[0])); } // if it is strictly equal to the previous one then no need to change anything diff --git a/tests/Composer/Test/Package/Version/VersionBumperTest.php b/tests/Composer/Test/Package/Version/VersionBumperTest.php index cde90c2d6..3c55a42ff 100644 --- a/tests/Composer/Test/Package/Version/VersionBumperTest.php +++ b/tests/Composer/Test/Package/Version/VersionBumperTest.php @@ -51,13 +51,15 @@ class VersionBumperTest extends TestCase yield 'preserve multi constraints' => ['^1.2 || ^2.3', '1.3.2', '^1.3.2 || ^2.3']; yield 'preserve multi constraints/2' => ['^1.2 || ^2.3', '2.4.0', '^1.2 || ^2.4']; yield 'preserve multi constraints/3' => ['^1.2 || ^2.3 || ^2', '2.4.0', '^1.2 || ^2.4 || ^2.4']; + yield 'preserve multi constraints/4' => ['^1.2 || ^2.3.3 || ^2', '2.4.0', '^1.2 || ^2.4.0 || ^2.4']; yield '@dev is preserved' => ['^3@dev', '3.2.x-dev', '^3.2@dev']; yield 'non-stable versions abort upgrades' => ['~2', '2.1-beta.1', '~2']; yield 'dev reqs are skipped' => ['dev-main', 'dev-foo', 'dev-main']; yield 'dev version does not upgrade' => ['^3.2', 'dev-main', '^3.2']; yield 'upgrade dev version if aliased' => ['^3.2', 'dev-main', '^3.3', '3.3.x-dev']; yield 'upgrade major wildcard to caret' => ['2.*', '2.4.0', '^2.4']; - yield 'upgrade major wildcard as x to caret' => ['2.x.x', '2.4.0', '^2.4']; + yield 'upgrade major wildcard as x to caret' => ['2.x', '2.4.0', '^2.4']; + yield 'upgrade major wildcard as x to caret/2' => ['2.x.x', '2.4.0', '^2.4.0']; yield 'leave minor wildcard alone' => ['2.4.*', '2.4.3', '2.4.*']; yield 'leave patch wildcard alone' => ['2.4.3.*', '2.4.3.2', '2.4.3.*']; yield 'upgrade tilde to caret when compatible' => ['~2.2', '2.4.3', '^2.4.3']; From ef7ba73a6ab0dc048e31cd93a1c894216c25b3a1 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 17 Dec 2022 22:58:35 +0100 Subject: [PATCH 3/3] Add support for bumping >=x to >=latest, fixes #11179 --- src/Composer/Package/Version/VersionBumper.php | 14 +++++++++++--- .../Test/Package/Version/VersionBumperTest.php | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Composer/Package/Version/VersionBumper.php b/src/Composer/Package/Version/VersionBumper.php index bf9ed1fca..1129efaa0 100644 --- a/src/Composer/Package/Version/VersionBumper.php +++ b/src/Composer/Package/Version/VersionBumper.php @@ -71,7 +71,8 @@ class VersionBumper } $major = Preg::replace('{^(\d+).*}', '$1', $version); - $newPrettyConstraint = '^'.Preg::replace('{(?:\.(?:0|9999999))+(-dev)?$}', '', $version); + $versionWithoutSuffix = Preg::replace('{(?:\.(?:0|9999999))+(-dev)?$}', '', $version); + $newPrettyConstraint = '^'.$versionWithoutSuffix; // not a simple stable version, abort if (!Preg::isMatch('{^\^\d+(\.\d+)*$}', $newPrettyConstraint)) { @@ -84,17 +85,24 @@ class VersionBumper \^'.$major.'(?:\.\d+)* # e.g. ^2.anything | ~'.$major.'(?:\.\d+)? # e.g. ~2 or ~2.2 but no more | '.$major.'(?:\.[*x])+ # e.g. 2.* or 2.*.* or 2.x.x.x etc + | >=\d(?:\.\d+)* # e.g. >=2 or >=1.2 etc ) (?=,|$|\ |\||@) # trailing separator }x'; if (Preg::isMatchAllWithOffsets($pattern, $prettyConstraint, $matches)) { $modified = $prettyConstraint; foreach (array_reverse($matches['constraint']) as $match) { + assert(is_string($match[0])); $suffix = ''; - if (substr_count($match[0], '.') === 2 && substr_count($newPrettyConstraint, '.') === 1) { + if (substr_count($match[0], '.') === 2 && substr_count($versionWithoutSuffix, '.') === 1) { $suffix = '.0'; } - $modified = substr_replace($modified, $newPrettyConstraint.$suffix, $match[1], Platform::strlen((string) $match[0])); + if (str_starts_with($match[0], '>=')) { + $replacement = '>='.$versionWithoutSuffix.$suffix; + } else { + $replacement = $newPrettyConstraint.$suffix; + } + $modified = substr_replace($modified, $replacement, $match[1], Platform::strlen($match[0])); } // if it is strictly equal to the previous one then no need to change anything diff --git a/tests/Composer/Test/Package/Version/VersionBumperTest.php b/tests/Composer/Test/Package/Version/VersionBumperTest.php index 3c55a42ff..1a53a898d 100644 --- a/tests/Composer/Test/Package/Version/VersionBumperTest.php +++ b/tests/Composer/Test/Package/Version/VersionBumperTest.php @@ -64,5 +64,7 @@ class VersionBumperTest extends TestCase yield 'leave patch wildcard alone' => ['2.4.3.*', '2.4.3.2', '2.4.3.*']; yield 'upgrade tilde to caret when compatible' => ['~2.2', '2.4.3', '^2.4.3']; yield 'leave patch-only-tilde alone' => ['~2.2.3', '2.2.6', '~2.2.3']; + yield 'upgrade bigger-or-eq to latest' => ['>=3.0', '3.4.5', '>=3.4.5']; + yield 'leave bigger-than untouched' => ['>2.2.3', '2.2.6', '>2.2.3']; } }