Skip to content

Commit

Permalink
Fix another pre-release version bug (#24)
Browse files Browse the repository at this point in the history
I think at this point we're testing this edge case in all our public
APIs.

Closes flutter#17
Closes #23
  • Loading branch information
nex3 authored Apr 5, 2018
1 parent 101f5c8 commit 38a7b5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
47 changes: 18 additions & 29 deletions lib/src/version_range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,26 @@ class VersionRange implements Comparable<VersionRange>, VersionConstraint {

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) {
Expand All @@ -169,18 +166,10 @@ class VersionRange implements Comparable<VersionRange>, VersionConstraint {

// 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.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pub_semver
version: 1.3.4
version: 1.3.5
author: Dart Team <[email protected]>
description: >
Versions and version constraints implementing pub's versioning policy. This
Expand Down
19 changes: 19 additions & 0 deletions test/version_range_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ main() {
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()', () {
Expand Down Expand Up @@ -539,6 +547,17 @@ main() {
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()', () {
Expand Down

0 comments on commit 38a7b5b

Please sign in to comment.