Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Darren Weber committed Aug 23, 2019
1 parent 335da0d commit c51df8f
Showing 2 changed files with 79 additions and 0 deletions.
27 changes: 27 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
@@ -12,6 +14,31 @@
from .version_union import VersionUnion


def is_sem_ver_constraint(sem_ver): # type: (str) -> bool
"""Check that a string is a single SEM-VER constraint
:param sem_ver: a single SEM-VER constraint
:return: True if sem_ver is a single SEM-VER constraint
"""
try:
parse_single_constraint(sem_ver)
return True
except (TypeError, ValueError):
return False


def sem_ver_sorted(values): # type: (List[str]) -> List[str]
"""Sort a list of single SEM-VER constraints (string) in
ascending order; discards any string that is not a SEM-VER
:param values: a list of single SEM-VER constraints (strings)
:return: sorted values based on SEM-VER sort criteria
"""
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()
52 changes: 52 additions & 0 deletions tests/semver/test_sem_ver_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

import poetry.semver
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", True),
(">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(mocker, constraint, result):
parser = mocker.spy(poetry.semver, name="parse_single_constraint")
assert is_sem_ver_constraint(constraint) == result
assert parser.call_count == 1


@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"]),
(["10.0.3", "1.0.3", "hot-fix-666"], ["1.0.3", "10.0.3"]),
],
)
def test_sem_ver_sorted(mocker, unsorted, sorted_):
parser = mocker.spy(poetry.semver, name="parse_single_constraint")
assert sem_ver_sorted(unsorted) == sorted_
assert parser.call_count == len(sorted_)

0 comments on commit c51df8f

Please sign in to comment.