Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty version range results in empty condition set #17116

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 11 additions & 1 deletion 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"], []],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we want to support this syntax at all, requires = "dep/[,include_prerelease=True" looks really ugly and not intuitive what would be the expected behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree, but wasn't sure whether adding an exception for this would be the preferred option. My assumption would be to only raise an exception when the version range specifier is empty and include_prerelease is specified. Leaving "dep/[]" as a valid range, but disallowing "dep/[,include_prerelease]". Is this assumption correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point.

In retrospective, probably we shouldn't have allowed the empty [ ] in first place, it is possible that it slipped from previous implementations, or earlier unit tests. But you are right now that it is allowed, and we most likely don't want to risk breaking existing users that would rely on it, that it would be more consistent to allow the prereleases to work with the same syntax even if ugly.

So looks good, lets go for it.
Please check that you need to sign the CLA in order to be able to merge the PR. Many thanks for your contribution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me! I should have signed the CLA now and will push the discussed update to the test now.

['>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 @@ -81,6 +87,10 @@ def test_range(version_range, conditions, versions_in, versions_out):
['*, 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"]],
['>1 <2.0', None, ["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