From 060933b4f9fd87469572df80f8a8ccc7fde11698 Mon Sep 17 00:00:00 2001 From: memsharded Date: Wed, 23 Aug 2023 13:59:03 +0200 Subject: [PATCH 1/3] fix for prereleases with hyphens --- conans/model/version.py | 4 +++- .../test/unittests/model/version/test_version_comparison.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/conans/model/version.py b/conans/model/version.py index ccb24a85e70..124e8e08717 100644 --- a/conans/model/version.py +++ b/conans/model/version.py @@ -63,7 +63,9 @@ def __init__(self, value): value = items[0] self._build = None - items = value.rsplit("-", 1) # split for pre-release + # 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) # This is a nested version by itself diff --git a/conans/test/unittests/model/version/test_version_comparison.py b/conans/test/unittests/model/version/test_version_comparison.py index 39b1398b30d..83492e9f09f 100644 --- a/conans/test/unittests/model/version/test_version_comparison.py +++ b/conans/test/unittests/model/version/test_version_comparison.py @@ -55,6 +55,8 @@ ("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") ] From 699656af97c6c6119351d9521749fe03c1dd20d5 Mon Sep 17 00:00:00 2001 From: memsharded Date: Thu, 24 Aug 2023 01:43:29 +0200 Subject: [PATCH 2/3] more tests --- .../unittests/model/version/test_version_comparison.py | 4 +++- .../test/unittests/model/version/test_version_parse.py | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/conans/test/unittests/model/version/test_version_comparison.py b/conans/test/unittests/model/version/test_version_comparison.py index 83492e9f09f..3472305189b 100644 --- a/conans/test/unittests/model/version/test_version_comparison.py +++ b/conans/test/unittests/model/version/test_version_comparison.py @@ -56,7 +56,9 @@ ("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"), + ("0.0.1-3-1", "0.0.1-4"), + ("0.0.1-3-1", "0.0.1-3") ] 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) From 41b895af248ed020a4b0ab9c0f7def16925a1ce9 Mon Sep 17 00:00:00 2001 From: memsharded Date: Thu, 24 Aug 2023 18:00:37 +0200 Subject: [PATCH 3/3] improved semver compliance --- conans/model/version.py | 37 +++++++++---------- .../model/version/test_version_comparison.py | 2 +- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/conans/model/version.py b/conans/model/version.py index 124e8e08717..42d866b4361 100644 --- a/conans/model/version.py +++ b/conans/model/version.py @@ -51,27 +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 - - # 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) # 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] @@ -165,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 3472305189b..2b053fa2a36 100644 --- a/conans/test/unittests/model/version/test_version_comparison.py +++ b/conans/test/unittests/model/version/test_version_comparison.py @@ -58,7 +58,7 @@ ("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-1", "0.0.1-3") + ("0.0.1-3", "0.0.1-3-1") # 3 is less than 3-1, pure alphanumeric ID "3-1" ]