If pinned to an exact version, keep the pin when bumping (#3736)
diff --git a/lib/src/command/dependency_services.dart b/lib/src/command/dependency_services.dart index 309fb1b..00cbe2e 100644 --- a/lib/src/command/dependency_services.dart +++ b/lib/src/command/dependency_services.dart
@@ -130,7 +130,7 @@ ? null : upgradeType == _UpgradeType.compatible ? originalConstraint.toString() - : VersionConstraint.compatibleWith(p.version).toString(), + : _bumpConstraint(originalConstraint, p.version).toString(), 'constraintWidened': originalConstraint == null ? null : upgradeType == _UpgradeType.compatible @@ -143,7 +143,7 @@ ? originalConstraint.toString() : originalConstraint.allows(p.version) ? originalConstraint.toString() - : VersionConstraint.compatibleWith(p.version) + : _bumpConstraint(originalConstraint, p.version) .toString(), 'previousVersion': currentPackage?.versionOrHash(), 'previousConstraint': originalConstraint?.toString(), @@ -606,6 +606,30 @@ : null; } +/// Return a constraint compatible with [newVersion]. +/// +/// By convention if the original constraint is pinned we return [newVersion]. Otherwise use [VersionConstraint.compatibleWith]. +VersionConstraint _bumpConstraint( + VersionConstraint original, + Version newVersion, +) { + if (original.isEmpty) return newVersion; + if (original is VersionRange) { + if (original.min == original.max) return newVersion; + + return VersionConstraint.compatibleWith(newVersion); + } + + throw ArgumentError.value( + original, + 'original', + 'Must be a Version range or empty', + ); +} + +/// Return a constraint compatible with [newVersion], but including [original] as well. +/// +/// By convention if the original constraint is pinned, we don't widen the constraint but return [newVersion] instead. VersionConstraint _widenConstraint( VersionConstraint original, Version newVersion, @@ -614,6 +638,7 @@ if (original is VersionRange) { final min = original.min; final max = original.max; + if (min == max) return newVersion; if (max != null && newVersion >= max) { return _compatibleWithIfPossible( VersionRange(
diff --git a/test/testdata/goldens/dependency_services/dependency_services_test/Can update a git package.txt b/test/testdata/goldens/dependency_services/dependency_services_test/Can update a git package.txt index 17f7096..c34c46f 100644 --- a/test/testdata/goldens/dependency_services/dependency_services_test/Can update a git package.txt +++ b/test/testdata/goldens/dependency_services/dependency_services_test/Can update a git package.txt
@@ -179,7 +179,7 @@ "path": "." } }, - "constraintBumped": "^2.0.0", + "constraintBumped": "2.0.0", "constraintWidened": "any", "constraintBumpedIfNeeded": "any", "previousVersion": "9e8b8ad5091bec2c4724b1e0c8ff9d6e32a7eaa4",
diff --git a/test/testdata/goldens/dependency_services/dependency_services_test/multibreaking.txt b/test/testdata/goldens/dependency_services/dependency_services_test/multibreaking.txt index aa053e5..a65da00 100644 --- a/test/testdata/goldens/dependency_services/dependency_services_test/multibreaking.txt +++ b/test/testdata/goldens/dependency_services/dependency_services_test/multibreaking.txt
@@ -188,9 +188,9 @@ "sha256": "f1a51ba864d7ef5702590e96167b812443ace67e15a82a0e064e735ac1376ec5" } }, - "constraintBumped": "^1.1.0", - "constraintWidened": "^1.0.0", - "constraintBumpedIfNeeded": "^1.1.0", + "constraintBumped": "1.1.0", + "constraintWidened": "1.1.0", + "constraintBumpedIfNeeded": "1.1.0", "previousVersion": "1.0.0", "previousConstraint": "1.0.0", "previousSource": { @@ -216,9 +216,9 @@ "sha256": "f1a51ba864d7ef5702590e96167b812443ace67e15a82a0e064e735ac1376ec5" } }, - "constraintBumped": "^1.1.0", - "constraintWidened": "^1.0.0", - "constraintBumpedIfNeeded": "^1.1.0", + "constraintBumped": "1.1.0", + "constraintWidened": "1.1.0", + "constraintBumpedIfNeeded": "1.1.0", "previousVersion": "1.0.0", "previousConstraint": "1.0.0", "previousSource": {