Skip to content

Commit

Permalink
Fix/version prerelease hyphens (#14561)
Browse files Browse the repository at this point in the history
* fix for prereleases with hyphens

* more tests

* improved semver compliance
  • Loading branch information
memsharded authored Aug 25, 2023
1 parent f595556 commit abcce2f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
35 changes: 17 additions & 18 deletions conans/model/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]


Expand Down
10 changes: 6 additions & 4 deletions conans/test/unittests/model/version/test_version_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit abcce2f

Please sign in to comment.