From f1ef6e461accc293b14a7bf883865858ac29c7ad Mon Sep 17 00:00:00 2001 From: James Date: Fri, 12 Apr 2024 10:31:53 +0200 Subject: [PATCH] Improve error messages for invalid version ranges operators (#16069) * wip * add checks * wip * wip * wip * Update conans/model/version_range.py Co-authored-by: Antonin RAFFIN --------- Co-authored-by: Antonin RAFFIN --- conans/model/version_range.py | 6 ++++-- conans/test/unittests/model/version/test_version_range.py | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/conans/model/version_range.py b/conans/model/version_range.py index 97778f68cf6..751683e3e73 100644 --- a/conans/model/version_range.py +++ b/conans/model/version_range.py @@ -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}"') @@ -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 @@ -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 diff --git a/conans/test/unittests/model/version/test_version_range.py b/conans/test/unittests/model/version/test_version_range.py index 263caf65f82..256d4420eab 100644 --- a/conans/test/unittests/model/version/test_version_range.py +++ b/conans/test/unittests/model/version/test_version_range.py @@ -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"]], @@ -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):