Skip to content

Commit

Permalink
fix: empty version range results in empty condition set
Browse files Browse the repository at this point in the history
  • Loading branch information
jwidauer committed Oct 4, 2024
1 parent 1e42a99 commit ac46a9b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions conans/model/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class _ConditionSet:

def __init__(self, expression, prerelease):
expressions = expression.split()
if not expressions:
# Guarantee at least one expression
expressions = [""]

self.prerelease = prerelease
self.conditions = []
for e in expressions:
Expand Down
18 changes: 14 additions & 4 deletions test/unittests/model/version/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# pre-releases
['', [[[">=", "0.0.0-"]]], ["1.0"], ["1.0-pre.1"]],
['*, include_prerelease=True', [[[">=", "0.0.0-"]]], ["1.0", "1.0-pre.1", "0.0.0", "0.0.0-pre.1"], []],
[', include_prerelease=True', [[[">=", "0.0.0-"]]], ["1.0", "1.0-pre.1", "0.0.0", "0.0.0-pre.1"], []],
['>1- <2.0', [[['>', '1-'], ['<', '2.0-']]],
["1.0", "1.1", "1.9"], ["1-pre.1", "1.5.1-pre1", "2.1-pre1"]],
['>1- <2.0 || ^3.2 ', [[['>', '1-'], ['<', '2.0-']], [['>=', '3.2-'], ['<', '4.0-']]],
Expand All @@ -53,14 +54,19 @@
['>=2.0-pre.0, include_prerelease', [[['>=', '2.0-pre.0']]], ["2.1", "2.0-pre.1"], ["1.5"]],
# Build metadata
['>=1.0.0+2', [[['>=', '1.0.0+2']]], ["1.0.0+2", "1.0.0+3"], ["1.0.0+1"]],
['>=2.2.0+build.en.305 <2.2.0+build.en.306', [], ["2.2.0+build.en.305", "2.2.0+build.en.305.1", "2.2.0+build.en.305.2"], ["2.2.0+build.en.306"]]
['>=2.2.0+build.en.305 <2.2.0+build.en.306', [[['>=', '2.2.0+build.en.305'], ['<', '2.2.0+build.en.306']]],
["2.2.0+build.en.305", "2.2.0+build.en.305.1", "2.2.0+build.en.305.2"], ["2.2.0+build.en.306"]]
]


@pytest.mark.parametrize("version_range, conditions, versions_in, versions_out", values)
def test_range(version_range, conditions, versions_in, versions_out):
r = VersionRange(version_range)
assert len(r.condition_sets) == len(conditions), \
f"Expected {r} to have {len(conditions)} condition sets, but got {len(r.condition_sets)}"
for condition_set, expected_condition_set in zip(r.condition_sets, conditions):
assert len(condition_set.conditions) == len(expected_condition_set), \
f"Expected {r} to have {len(expected_condition_set)} conditions, but got {len(condition_set.conditions)}"
for condition, expected_condition in zip(condition_set.conditions, expected_condition_set):
assert condition.operator == expected_condition[0], f"Expected {r} condition operator to be {expected_condition[0]}, but got {condition.operator}"
assert condition.version == expected_condition[1], f"Expected {r} condition version to be {expected_condition[1]}, but got {condition.version}"
Expand All @@ -77,9 +83,13 @@ def test_range(version_range, conditions, versions_in, versions_out):
['*', False, ["1.5.1"], ["1.5.1-pre1", "2.1-pre1"]],
['*', None, ["1.5.1"], ["1.5.1-pre1", "2.1-pre1"]],
['*, include_prerelease', True, ["1.5.1", "1.5.1-pre1", "2.1-pre1"], []],
['*, include_prerelease', False, ["1.5.1"], ["1.5.1-pre1", "2.1-pre1"]],
['*, include_prerelease', None, ["1.5.1", "1.5.1-pre1", "2.1-pre1"], []],
['*, include_prerelease', True, ["1.5.1", "1.5.1-pre1", "2.1-pre1", "*"], []],
['*, include_prerelease', False, ["1.5.1", "*"], ["1.5.1-pre1", "2.1-pre1"]],
['*, include_prerelease', None, ["1.5.1", "1.5.1-pre1", "2.1-pre1", "*"], []],
[', include_prerelease', True, ["1.5.1", "1.5.1-pre1", "2.1-pre1", "*"], []],
[', include_prerelease', False, ["1.5.1", "*"], ["1.5.1-pre1", "2.1-pre1"]],
[', include_prerelease', None, ["1.5.1", "1.5.1-pre1", "2.1-pre1", "*"], []],
['>1 <2.0', True, ["1.5.1", "1.5.1-pre1"], ["2.1-pre1"]],
['>1 <2.0', False, ["1.5.1"], ["1.5.1-pre1", "2.1-pre1"]],
Expand Down
10 changes: 10 additions & 0 deletions test/unittests/model/version/test_version_range_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
from conans.model.version_range import VersionRange

values = [
# empty / any ranges
['', "", ">=0.0.0"],
['*', "*", ">=0.0.0"],
# single lower limits bounds
['>1.0', ">1.0", ">1.0"],
['>=1.0', ">1.0", ">1.0"],
['>1.0', ">1.1", ">1.1"],
['>1.0', ">=1.1", ">=1.1"],
['>=1.0', ">=1.1", ">=1.1"],
['', ">1.0", ">1.0"],
['>1.0', "", ">1.0"],
# single upper limits bounds
['<2.0', "<2.0", "<2.0"],
['<=1.0', "<1.0", "<1.0"],
['<2.0', "<2.1", "<2.0"],
['<2.0', "<=1.1", "<=1.1"],
['<=1.0', "<=1.1", "<=1.0"],
['', "<2.0", ">=0.0.0 <2.0"],
['<2.0', "", ">=0.0.0 <2.0"],
# One lower limit, one upper
['>=1.0', "<2.0", ">=1.0 <2.0"],
['>=1', '<=1', ">=1 <=1"],
Expand Down Expand Up @@ -83,6 +90,9 @@ def test_range_intersection_incompatible(range1, range2):


prerelease_values = [
[", include_prerelease",
">=1.0-pre.1 <1.0-pre.99, include_prerelease",
">=1.0-pre.1 <1.0-pre.99, include_prerelease"],
[">=1.0-pre.1 <1.0-pre.99, include_prerelease",
">=1.0-pre.1 <1.0-pre.99, include_prerelease",
">=1.0-pre.1 <1.0-pre.99, include_prerelease"],
Expand Down

0 comments on commit ac46a9b

Please sign in to comment.