Skip to content

Commit

Permalink
Improve error messages for invalid version ranges operators (#16069)
Browse files Browse the repository at this point in the history
* wip

* add checks

* wip

* wip

* wip

* Update conans/model/version_range.py

Co-authored-by: Antonin RAFFIN <antonin.raffin@ensta.org>

---------

Co-authored-by: Antonin RAFFIN <antonin.raffin@ensta.org>
  • Loading branch information
memsharded and araffin committed Apr 12, 2024
1 parent 52f2435 commit f1ef6e4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions conans/model/version_range.py
Expand Up @@ -88,6 +88,8 @@ def _parse_expression(expression):
if expression[1] == "=":
operator += "="
index = 2
elif expression[1] == "=":
raise ConanException(f"Invalid version range operator '{operator}=' in {expression}, you should probably use {operator} instead.")
version = expression[index:]
if version == "":
raise ConanException(f'Error parsing version range "{expression}"')
Expand All @@ -111,7 +113,7 @@ def first_non_zero(main):
else:
return [_Condition(operator, Version(version))]

def _valid(self, version, conf_resolve_prepreleases):
def valid(self, version, conf_resolve_prepreleases):
if version.pre:
# Follow the expression desires only if core.version_ranges:resolve_prereleases is None,
# else force to the conf's value
Expand Down Expand Up @@ -180,7 +182,7 @@ def contains(self, version: Version, resolve_prerelease: Optional[bool]):
"""
assert isinstance(version, Version), type(version)
for condition_set in self.condition_sets:
if condition_set._valid(version, resolve_prerelease):
if condition_set.valid(version, resolve_prerelease):
return True
return False

Expand Down
4 changes: 3 additions & 1 deletion conans/test/unittests/model/version/test_version_range.py
Expand Up @@ -5,6 +5,7 @@
from conans.model.version_range import VersionRange

values = [
['=1.0.0', [[['=', '1.0.0']]], ["1.0.0"], ["0.1"]],
['>1.0.0', [[['>', '1.0.0']]], ["1.0.1"], ["0.1"]],
['<2.0', [[['<', '2.0-']]], ["1.0.1"], ["2.1"]],
['>1 <2.0', [[['>', '1'], ['<', '2.0-']]], ["1.5.1"], ["0.1", "2.1"]],
Expand Down Expand Up @@ -109,7 +110,8 @@ def test_range_prereleases_conf(version_range, resolve_prereleases, versions_in,

@pytest.mark.parametrize("version_range", [
">= 1.0", # https://github.com/conan-io/conan/issues/12692
">=0.0.1 < 1.0" # https://github.com/conan-io/conan/issues/14612
">=0.0.1 < 1.0", # https://github.com/conan-io/conan/issues/14612
"==1.0", "~=1.0", "^=1.0", "v=1.0" # https://github.com/conan-io/conan/issues/16066
])
def test_wrong_range_syntax(version_range):
with pytest.raises(ConanException):
Expand Down

0 comments on commit f1ef6e4

Please sign in to comment.