diff --git a/conans/model/version.py b/conans/model/version.py index ccb24a85e70..42d866b4361 100644 --- a/conans/model/version.py +++ b/conans/model/version.py @@ -51,25 +51,24 @@ class Version: This is NOT an implementation of semver, as users may use any pattern in their versions. It is just a helper to parse "." or "-" and compare taking into account integers when possible """ - def __init__(self, value): + def __init__(self, value, qualifier=False): value = str(value) self._value = value - - items = value.rsplit("+", 1) # split for build - if len(items) == 2: - value, build = items - self._build = Version(build) # This is a nested version by itself - else: - value = items[0] - self._build = None - - items = value.rsplit("-", 1) # split for pre-release - if len(items) == 2: - value, pre = items - self._pre = Version(pre) # This is a nested version by itself - else: - value = items[0] - self._pre = None + self._build = None + self._pre = None + self._qualifier = qualifier # it is a prerelease or build qualifier, not a main version + + if not qualifier: + items = value.rsplit("+", 1) # split for build + if len(items) == 2: + value, build = items + self._build = Version(build, qualifier=True) # This is a nested version by itself + + # split for pre-release, from the left, semver allows hyphens in identifiers :( + items = value.split("-", 1) + if len(items) == 2: + value, pre = items + self._pre = Version(pre, qualifier=True) # This is a nested version by itself items = value.split(".") items = [_VersionItem(item) for item in items] @@ -163,7 +162,7 @@ def __eq__(self, other): if other is None: return False if not isinstance(other, Version): - other = Version(other) + other = Version(other, self._qualifier) return (self._nonzero_items, self._pre, self._build) ==\ (other._nonzero_items, other._pre, other._build) diff --git a/conans/test/unittests/model/version/test_version_comparison.py b/conans/test/unittests/model/version/test_version_comparison.py index 39b1398b30d..2b053fa2a36 100644 --- a/conans/test/unittests/model/version/test_version_comparison.py +++ b/conans/test/unittests/model/version/test_version_comparison.py @@ -55,6 +55,10 @@ ("1.2.0", "1.2.1-dev"), ("1.2", "1.2.1-dev"), ("1.2-alpha", "1.2.1-dev"), + ("0.0.1-3", "0.0.1"), + ("0.0.1-3-1", "0.0.1"), + ("0.0.1-3-1", "0.0.1-4"), + ("0.0.1-3", "0.0.1-3-1") # 3 is less than 3-1, pure alphanumeric ID "3-1" ] diff --git a/conans/test/unittests/model/version/test_version_parse.py b/conans/test/unittests/model/version/test_version_parse.py index d4bbb7a4d35..78d99901bfa 100644 --- a/conans/test/unittests/model/version/test_version_parse.py +++ b/conans/test/unittests/model/version/test_version_parse.py @@ -26,13 +26,15 @@ '', None, None), ("+build2", '', None, "build2"), + ("1.2.3-pre.4-5-6+build.7-8-9", + "1.2.3", "pre.4-5-6", "build.7-8-9") ] -@pytest.mark.parametrize("v_str, main, build, pre", v) -def test_parse(v_str, main, build, pre): +@pytest.mark.parametrize("v_str, main, pre, build", v) +def test_parse(v_str, main, pre, build): v1 = Version(v_str) assert v1.main == main if type(main) is tuple else tuple([main]) - assert v1.pre == build - assert v1.build == pre + assert v1.pre == pre + assert v1.build == build assert str(v1) == str(v_str)