Fix another pre-release version bug (#24)
I think at this point we're testing this edge case in all our public
APIs.
Closes #17
Closes #23
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 53de841..207ba29 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+# 1.3.5
+
+* Fix a bug where `VersionRange.intersect()` would return incorrect results for
+ pre-release versions with the same base version number as release versions.
+
# 1.3.4
* Fix a bug where `VersionRange.allowsAll()`, `VersionRange.allowsAny()`, and
diff --git a/lib/src/version_range.dart b/lib/src/version_range.dart
index b615c6f..ae19002 100644
--- a/lib/src/version_range.dart
+++ b/lib/src/version_range.dart
@@ -137,29 +137,26 @@
if (other is VersionRange) {
// Intersect the two ranges.
- var intersectMin = min;
- var intersectIncludeMin = includeMin;
- var intersectMax = max;
- var intersectIncludeMax = includeMax;
-
- if (other.min == null) {
- // Do nothing.
- } else if (intersectMin == null || intersectMin < other.min) {
+ Version intersectMin;
+ bool intersectIncludeMin;
+ if (allowsLower(this, other)) {
+ if (strictlyLower(this, other)) return VersionConstraint.empty;
intersectMin = other.min;
intersectIncludeMin = other.includeMin;
- } else if (intersectMin == other.min && !other.includeMin) {
- // The edges are the same, but one is exclusive, make it exclusive.
- intersectIncludeMin = false;
+ } else {
+ if (strictlyLower(other, this)) return VersionConstraint.empty;
+ intersectMin = this.min;
+ intersectIncludeMin = this.includeMin;
}
- if (other.max == null) {
- // Do nothing.
- } else if (intersectMax == null || intersectMax > other.max) {
+ Version intersectMax;
+ bool intersectIncludeMax;
+ if (allowsHigher(this, other)) {
intersectMax = other.max;
intersectIncludeMax = other.includeMax;
- } else if (intersectMax == other.max && !other.includeMax) {
- // The edges are the same, but one is exclusive, make it exclusive.
- intersectIncludeMax = false;
+ } else {
+ intersectMax = this.max;
+ intersectIncludeMax = this.includeMax;
}
if (intersectMin == null && intersectMax == null) {
@@ -169,18 +166,10 @@
// If the range is just a single version.
if (intersectMin == intersectMax) {
- // If both ends are inclusive, allow that version.
- if (intersectIncludeMin && intersectIncludeMax) return intersectMin;
-
- // Otherwise, no versions.
- return VersionConstraint.empty;
- }
-
- if (intersectMin != null &&
- intersectMax != null &&
- intersectMin > intersectMax) {
- // Non-overlapping ranges, so empty.
- return VersionConstraint.empty;
+ // Because we already verified that the lower range isn't strictly
+ // lower, there must be some overlap.
+ assert(intersectIncludeMin && intersectIncludeMax);
+ return intersectMin;
}
// If we got here, there is an actual range.
diff --git a/pubspec.yaml b/pubspec.yaml
index 7f6dd0c..4dbabe4 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: pub_semver
-version: 1.3.4
+version: 1.3.5
author: Dart Team <misc@dartlang.org>
description: >
Versions and version constraints implementing pub's versioning policy. This
diff --git a/test/version_range_test.dart b/test/version_range_test.dart
index f011012..2c311f5 100644
--- a/test/version_range_test.dart
+++ b/test/version_range_test.dart
@@ -461,6 +461,14 @@
expect(new VersionRange(min: v123, max: v124).intersect(v114).isEmpty,
isTrue);
});
+
+ test("with a range with a pre-release min, returns an empty constraint",
+ () {
+ expect(
+ new VersionRange(max: v200)
+ .intersect(new VersionConstraint.parse(">=2.0.0-dev")),
+ equals(VersionConstraint.empty));
+ });
});
group('union()', () {
@@ -539,6 +547,17 @@
equals(new VersionRange(
min: v003, max: v114, includeMin: true, includeMax: true)));
});
+
+ test("with a range with a pre-release min, returns a constraint with a gap",
+ () {
+ var result = new VersionRange(max: v200)
+ .union(new VersionConstraint.parse(">=2.0.0-dev"));
+ expect(result, allows(v140));
+ expect(result, doesNotAllow(new Version.parse("2.0.0-alpha")));
+ expect(result, allows(new Version.parse("2.0.0-dev")));
+ expect(result, allows(new Version.parse("2.0.0-dev.1")));
+ expect(result, allows(new Version.parse("2.0.0")));
+ });
});
group('difference()', () {