Skip to content

Commit 38a7b5b

Browse files
authored
Fix another pre-release version bug (#24)
I think at this point we're testing this edge case in all our public APIs. Closes flutter#17 Closes #23
1 parent 101f5c8 commit 38a7b5b

File tree

4 files changed

+43
-30
lines changed

4 files changed

+43
-30
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.3.5
2+
3+
* Fix a bug where `VersionRange.intersect()` would return incorrect results for
4+
pre-release versions with the same base version number as release versions.
5+
16
# 1.3.4
27

38
* Fix a bug where `VersionRange.allowsAll()`, `VersionRange.allowsAny()`, and

lib/src/version_range.dart

+18-29
Original file line numberDiff line numberDiff line change
@@ -137,29 +137,26 @@ class VersionRange implements Comparable<VersionRange>, VersionConstraint {
137137

138138
if (other is VersionRange) {
139139
// Intersect the two ranges.
140-
var intersectMin = min;
141-
var intersectIncludeMin = includeMin;
142-
var intersectMax = max;
143-
var intersectIncludeMax = includeMax;
144-
145-
if (other.min == null) {
146-
// Do nothing.
147-
} else if (intersectMin == null || intersectMin < other.min) {
140+
Version intersectMin;
141+
bool intersectIncludeMin;
142+
if (allowsLower(this, other)) {
143+
if (strictlyLower(this, other)) return VersionConstraint.empty;
148144
intersectMin = other.min;
149145
intersectIncludeMin = other.includeMin;
150-
} else if (intersectMin == other.min && !other.includeMin) {
151-
// The edges are the same, but one is exclusive, make it exclusive.
152-
intersectIncludeMin = false;
146+
} else {
147+
if (strictlyLower(other, this)) return VersionConstraint.empty;
148+
intersectMin = this.min;
149+
intersectIncludeMin = this.includeMin;
153150
}
154151

155-
if (other.max == null) {
156-
// Do nothing.
157-
} else if (intersectMax == null || intersectMax > other.max) {
152+
Version intersectMax;
153+
bool intersectIncludeMax;
154+
if (allowsHigher(this, other)) {
158155
intersectMax = other.max;
159156
intersectIncludeMax = other.includeMax;
160-
} else if (intersectMax == other.max && !other.includeMax) {
161-
// The edges are the same, but one is exclusive, make it exclusive.
162-
intersectIncludeMax = false;
157+
} else {
158+
intersectMax = this.max;
159+
intersectIncludeMax = this.includeMax;
163160
}
164161

165162
if (intersectMin == null && intersectMax == null) {
@@ -169,18 +166,10 @@ class VersionRange implements Comparable<VersionRange>, VersionConstraint {
169166

170167
// If the range is just a single version.
171168
if (intersectMin == intersectMax) {
172-
// If both ends are inclusive, allow that version.
173-
if (intersectIncludeMin && intersectIncludeMax) return intersectMin;
174-
175-
// Otherwise, no versions.
176-
return VersionConstraint.empty;
177-
}
178-
179-
if (intersectMin != null &&
180-
intersectMax != null &&
181-
intersectMin > intersectMax) {
182-
// Non-overlapping ranges, so empty.
183-
return VersionConstraint.empty;
169+
// Because we already verified that the lower range isn't strictly
170+
// lower, there must be some overlap.
171+
assert(intersectIncludeMin && intersectIncludeMax);
172+
return intersectMin;
184173
}
185174

186175
// If we got here, there is an actual range.

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: pub_semver
2-
version: 1.3.4
2+
version: 1.3.5
33
author: Dart Team <[email protected]>
44
description: >
55
Versions and version constraints implementing pub's versioning policy. This

test/version_range_test.dart

+19
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,14 @@ main() {
461461
expect(new VersionRange(min: v123, max: v124).intersect(v114).isEmpty,
462462
isTrue);
463463
});
464+
465+
test("with a range with a pre-release min, returns an empty constraint",
466+
() {
467+
expect(
468+
new VersionRange(max: v200)
469+
.intersect(new VersionConstraint.parse(">=2.0.0-dev")),
470+
equals(VersionConstraint.empty));
471+
});
464472
});
465473

466474
group('union()', () {
@@ -539,6 +547,17 @@ main() {
539547
equals(new VersionRange(
540548
min: v003, max: v114, includeMin: true, includeMax: true)));
541549
});
550+
551+
test("with a range with a pre-release min, returns a constraint with a gap",
552+
() {
553+
var result = new VersionRange(max: v200)
554+
.union(new VersionConstraint.parse(">=2.0.0-dev"));
555+
expect(result, allows(v140));
556+
expect(result, doesNotAllow(new Version.parse("2.0.0-alpha")));
557+
expect(result, allows(new Version.parse("2.0.0-dev")));
558+
expect(result, allows(new Version.parse("2.0.0-dev.1")));
559+
expect(result, allows(new Version.parse("2.0.0")));
560+
});
542561
});
543562

544563
group('difference()', () {

0 commit comments

Comments
 (0)