Skip to content

Commit

Permalink
Add utils for poetry/semver
Browse files Browse the repository at this point in the history
- boolean check for a sem-ver string
- filter and sort a sequence of sem-ver strings
- these utils are used to filter and sort a list of git tags
  • Loading branch information
Darren Weber committed Aug 10, 2019
1 parent 1caf20a commit 06200a9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions poetry/semver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
from typing import List

from .empty_constraint import EmptyConstraint
from .patterns import COMPLETE_VERSION
from .patterns import BASIC_CONSTRAINT
from .patterns import CARET_CONSTRAINT
from .patterns import TILDE_CONSTRAINT
Expand All @@ -12,6 +14,20 @@
from .version_union import VersionUnion


def is_sem_ver_constraint(sem_ver): # type: (str) -> bool
try:
parse_single_constraint(sem_ver)
return True
except ValueError:
return False


def sem_ver_sorted(values): # type: (List[str]) -> List[str]
versions = [t for t in values if COMPLETE_VERSION.match(t)]
versions = sorted(versions, key=lambda t: parse_single_constraint(t))
return versions


def parse_constraint(constraints): # type: (str) -> VersionConstraint
if constraints == "*":
return VersionRange()
Expand Down
46 changes: 46 additions & 0 deletions tests/semver/test_sem_ver_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

from poetry.semver import is_sem_ver_constraint
from poetry.semver import sem_ver_sorted


@pytest.mark.parametrize(
"constraint,result",
[
("*", True),
("*.*", True),
("v*.*", True),
("*.x.*", True),
("x.X.x.*", True),
# ('!=1.0.0', Constraint('!=', '1.0.0.0')),
(">1.0.0", True),
("<1.2.3", True),
("<=1.2.3", True),
(">=1.2.3", True),
("=1.2.3", True),
("1.2.3", True),
("=1.0", True),
("1.2.3b5", True),
(">= 1.2.3", True),
(">dev", True),
("hot-fix-666", False),
],
)
def test_is_sem_ver_constraint(constraint, result):
assert is_sem_ver_constraint(constraint) == result


@pytest.mark.parametrize(
"unsorted, sorted_",
[
(["1.0.3", "1.0.2", "1.0.1"], ["1.0.1", "1.0.2", "1.0.3"]),
(["1.0.0.2", "1.0.0.0rc2"], ["1.0.0.0rc2", "1.0.0.2"]),
(["1.0.0.0", "1.0.0.0rc2"], ["1.0.0.0rc2", "1.0.0.0"]),
(["1.0.0.0.0", "1.0.0.0rc2"], ["1.0.0.0rc2", "1.0.0.0.0"]),
(["1.0.0rc2", "1.0.0rc1"], ["1.0.0rc1", "1.0.0rc2"]),
(["1.0.0rc2", "1.0.0b1"], ["1.0.0b1", "1.0.0rc2"]),
(["1.0.3", "1.0.2", "1.0.1", "hot-fix-666"], ["1.0.1", "1.0.2", "1.0.3"]),
],
)
def test_sem_ver_sorted(unsorted, sorted_):
assert sem_ver_sorted(unsorted) == sorted_

0 comments on commit 06200a9

Please sign in to comment.