Skip to content

Commit

Permalink
Add better error UX when spaces are found in required_conan_version (#…
Browse files Browse the repository at this point in the history
…12695)

* Add better error UX when spaces in required_conan_version

* Fix typo

* Fix import
  • Loading branch information
AbrilRBS authored Dec 12, 2022
1 parent 64a64b8 commit fd8d17b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions conans/model/version_range.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import namedtuple

from conans.errors import ConanException
from conans.model.recipe_ref import Version


Expand Down Expand Up @@ -35,6 +36,8 @@ def _parse_expression(expression):
operator += "="
index = 2
version = expression[index:]
if version == "":
raise ConanException(f"Error parsing version range {expression}")
if operator == "~": # tilde minor
v = Version(version)
index = 1 if len(v.main) > 1 else 0
Expand Down
16 changes: 16 additions & 0 deletions conans/test/integration/conanfile/required_conan_version_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,19 @@ class Lib(ConanFile):
""")
client.save({"conanfile.py": conanfile})
client.run("export . --name=pkg --version=1.0")

def test_required_conan_version_invalid_syntax(self):
""" required_conan_version used to warn of mismatching versions if spaces were present,
but now we have a nicer error"""
# https://github.com/conan-io/conan/issues/12692
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
required_conan_version = ">= 1.0"
class Lib(ConanFile):
pass""")
client.save({"conanfile.py": conanfile})
client.run("export . --name=pkg --version=1.0", assert_error=True)
self.assertNotIn(f"Current Conan version ({__version__}) does not satisfy the defined one "
"(>= 1.0)", client.out)
self.assertIn("Error parsing version range >=", client.out)
6 changes: 6 additions & 0 deletions conans/test/unittests/model/version/test_version_range.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from conans.errors import ConanException
from conans.model.recipe_ref import Version
from conans.model.version_range import VersionRange

Expand Down Expand Up @@ -50,3 +51,8 @@ def test_range(version_range, conditions, versions_in, versions_out):

for v in versions_out:
assert Version(v) not in r

def test_wrong_range_syntax():
# https://github.com/conan-io/conan/issues/12692
with pytest.raises(ConanException) as e:
VersionRange(">= 1.0")

0 comments on commit fd8d17b

Please sign in to comment.