Skip to content

Commit c6d02b4

Browse files
authored
Fix more pre-release constraint bugs (#26)
I hadn't thought to test version ranges with max pre-release constraints, but they turn out to be relevant in some situations. Closes flutter#20
1 parent e9ac6f6 commit c6d02b4

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.3.7
2+
3+
* Fix more bugs with `VersionRange.intersect()`, `VersionRange.difference()`,
4+
and `VersionRange.union()` involving version ranges with pre-release maximums.
5+
16
# 1.3.6
27

38
* Fix a bug where constraints that only allowed pre-release versions would be

lib/src/utils.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ bool allowsHigher(VersionRange range1, VersionRange range2) {
3232
if (range1.max == null) return range2.max != null;
3333
if (range2.max == null) return false;
3434

35-
// `<1.0.0-dev.1` allows `1.0.0-dev.0` which is higher than any versions
36-
// allowed by `<1.0.0`.
35+
// `<1.0.0-dev.1` allows higher versions than `<1.0.0`, such as `1.0.0-dev.0`.
3736
if (disallowedByPreRelease(range2, range1.max)) return true;
3837

38+
// `<1.0.0` doesn't allow any versions higher than `<1.0.0-dev`.
39+
if (disallowedByPreRelease(range1, range2.max)) return false;
40+
3941
var comparison = range1.max.compareTo(range2.max);
4042
if (comparison == 1) return true;
4143
if (comparison == -1) return false;
@@ -46,8 +48,12 @@ bool allowsHigher(VersionRange range1, VersionRange range2) {
4648
/// [range2].
4749
bool strictlyLower(VersionRange range1, VersionRange range2) {
4850
if (range1.max == null || range2.min == null) return false;
51+
52+
// `<1.0.0` doesn't allow any versions allowed by `>=1.0.0-dev.0`.
4953
if (disallowedByPreRelease(range1, range2.min)) return true;
5054

55+
//if (disallowedByPreRelease(range2, range1.min)) return true;
56+
5157
var comparison = range1.max.compareTo(range2.min);
5258
if (comparison == -1) return true;
5359
if (comparison == 1) return false;

lib/src/version_range.dart

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,29 +215,24 @@ class VersionRange implements Comparable<VersionRange>, VersionConstraint {
215215
return new VersionConstraint.unionOf([this, other]);
216216
}
217217

218-
var unionMin = min;
219-
var unionIncludeMin = includeMin;
220-
var unionMax = max;
221-
var unionIncludeMax = includeMax;
222-
223-
if (unionMin == null) {
224-
// Do nothing.
225-
} else if (other.min == null || other.min < min) {
218+
Version unionMin;
219+
bool unionIncludeMin;
220+
if (allowsLower(this, other)) {
221+
unionMin = this.min;
222+
unionIncludeMin = this.includeMin;
223+
} else {
226224
unionMin = other.min;
227225
unionIncludeMin = other.includeMin;
228-
} else if (min == other.min && other.includeMin) {
229-
// If the edges are the same but one is inclusive, make it inclusive.
230-
unionIncludeMin = true;
231226
}
232227

233-
if (unionMax == null) {
234-
// Do nothing.
235-
} else if (other.max == null || other.max > max) {
228+
Version unionMax;
229+
bool unionIncludeMax;
230+
if (allowsHigher(this, other)) {
231+
unionMax = this.max;
232+
unionIncludeMax = this.includeMax;
233+
} else {
236234
unionMax = other.max;
237235
unionIncludeMax = other.includeMax;
238-
} else if (max == other.max && other.includeMax) {
239-
// If the edges are the same but one is inclusive, make it inclusive.
240-
unionIncludeMax = true;
241236
}
242237

243238
return new VersionRange(

pubspec.yaml

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

test/version_range_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ main() {
469469
.intersect(new VersionConstraint.parse(">=2.0.0-dev")),
470470
equals(VersionConstraint.empty));
471471
});
472+
473+
test("with a range with a pre-release max, returns the original", () {
474+
expect(
475+
new VersionRange(max: v200)
476+
.intersect(new VersionConstraint.parse("<2.0.0-dev")),
477+
equals(new VersionRange(max: v200)));
478+
});
472479
});
473480

474481
group('union()', () {
@@ -558,6 +565,14 @@ main() {
558565
expect(result, allows(new Version.parse("2.0.0-dev.1")));
559566
expect(result, allows(new Version.parse("2.0.0")));
560567
});
568+
569+
test("with a range with a pre-release max, returns the larger constraint",
570+
() {
571+
expect(
572+
new VersionRange(max: v200)
573+
.union(new VersionConstraint.parse("<2.0.0-dev")),
574+
equals(new VersionConstraint.parse("<2.0.0-dev")));
575+
});
561576
});
562577

563578
group('difference()', () {
@@ -730,6 +745,13 @@ main() {
730745
.difference(new VersionConstraint.parse(">=2.0.0-dev")),
731746
equals(new VersionRange(max: v200)));
732747
});
748+
749+
test("with a range with a pre-release max, returns null", () {
750+
expect(
751+
new VersionRange(max: v200)
752+
.difference(new VersionConstraint.parse("<2.0.0-dev")),
753+
equals(VersionConstraint.empty));
754+
});
733755
});
734756

735757
test('isEmpty', () {

0 commit comments

Comments
 (0)