Skip to content

Commit 00c6e4a

Browse files
authored
Fix VersionConstraint.intersection throwing on disjoint ranges (#2622)
1 parent d523e1f commit 00c6e4a

4 files changed

Lines changed: 79 additions & 3 deletions

File tree

‎pkgs/pub_semver/CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.2.2-wip
2+
3+
- Fix `VersionConstraint.intersection` throwing a `TypeError` when the
4+
intersection of the constraints is empty (e.g. for disjoint ranges) or a
5+
`VersionUnion`.
6+
17
## 2.2.1
28

39
- Fix parsing of pre-release and build identifiers to only treat pure-digit

‎pkgs/pub_semver/lib/src/version_constraint.dart‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,11 @@ abstract class VersionConstraint {
187187
/// all versions.
188188
factory VersionConstraint.intersection(
189189
Iterable<VersionConstraint> constraints) {
190-
var constraint = VersionRange();
190+
VersionConstraint constraint = VersionRange();
191191
for (var other in constraints) {
192-
constraint = constraint.intersect(other) as VersionRange;
192+
constraint = constraint.intersect(other);
193+
// Once the intersection is empty it cannot become non-empty again.
194+
if (constraint.isEmpty) return VersionConstraint.empty;
193195
}
194196
return constraint;
195197
}

‎pkgs/pub_semver/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: 2.2.1
2+
version: 2.2.2-wip
33
description: >-
44
Versions and version constraints implementing pub's versioning policy. This
55
is very similar to vanilla semver, with a few corner cases.

‎pkgs/pub_semver/test/version_constraint_test.dart‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,72 @@ void main() {
190190
expect(constraint.allows(Version.parse('2.0.0-0')), isFalse);
191191
});
192192
});
193+
194+
group('intersection()', () {
195+
test('of no constraints allows any version', () {
196+
var constraint = VersionConstraint.intersection([]);
197+
198+
expect(constraint, equals(VersionConstraint.any));
199+
});
200+
201+
test('of overlapping ranges is their overlap', () {
202+
var constraint = VersionConstraint.intersection([
203+
VersionRange(min: v123, max: v200),
204+
VersionRange(min: v114, max: v140),
205+
]);
206+
207+
expect(constraint, equals(VersionRange(min: v123, max: v140)));
208+
});
209+
210+
// https://github.com/dart-lang/tools/issues/2608
211+
test('of disjoint ranges is empty', () {
212+
var constraint = VersionConstraint.intersection([
213+
VersionRange(min: v123, max: v140),
214+
VersionRange(min: v200, max: v234),
215+
]);
216+
217+
expect(constraint.isEmpty, isTrue);
218+
});
219+
220+
test('of a range and a version it allows is that version', () {
221+
var constraint =
222+
VersionConstraint.intersection([VersionRange(max: v200), v123]);
223+
224+
expect(constraint, equals(v123));
225+
});
226+
227+
test('of a range and a version it does not allow is empty', () {
228+
var constraint =
229+
VersionConstraint.intersection([VersionRange(max: v140), v200]);
230+
231+
expect(constraint.isEmpty, isTrue);
232+
});
233+
234+
test('stays empty once a disjoint constraint is seen', () {
235+
var constraint = VersionConstraint.intersection([
236+
VersionRange(min: v123, max: v140),
237+
VersionRange(min: v200, max: v234),
238+
VersionConstraint.any,
239+
]);
240+
241+
expect(constraint.isEmpty, isTrue);
242+
});
243+
244+
test('of a range and a union can be a union', () {
245+
var constraint = VersionConstraint.intersection([
246+
VersionRange(min: v010, max: v300),
247+
VersionUnion.fromRanges([
248+
VersionRange(min: v072, max: v123),
249+
VersionRange(min: v200, max: v250),
250+
]),
251+
]);
252+
253+
expect(
254+
constraint,
255+
equals(VersionUnion.fromRanges([
256+
VersionRange(min: v072, max: v123),
257+
VersionRange(min: v200, max: v250),
258+
])));
259+
});
260+
});
193261
}

0 commit comments

Comments
 (0)