From 844c57789ba21a8d26f19797f6b61ba2e20d8a9b Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Sat, 25 Apr 2020 13:49:28 +0200 Subject: [PATCH] Handle whitespaces for PEP-440 constraints * fix incorrect parsing of spaces when parsing version constraints * add tests for `parse_constraint` --- poetry/semver/__init__.py | 2 +- poetry/semver/patterns.py | 4 +- tests/semver/test_parse_constraint.py | 86 +++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 tests/semver/test_parse_constraint.py diff --git a/poetry/semver/__init__.py b/poetry/semver/__init__.py index 725b7d43f58..39ab433d670 100644 --- a/poetry/semver/__init__.py +++ b/poetry/semver/__init__.py @@ -20,7 +20,7 @@ def parse_constraint(constraints): # type: (str) -> VersionConstraint or_groups = [] for constraints in or_constraints: and_constraints = re.split( - "(?< ,]) *(?< ,]) *(?|!=|>=?|<=?|==?)?\s*({}|dev)".format(_COMPLETE_VERSION) diff --git a/tests/semver/test_parse_constraint.py b/tests/semver/test_parse_constraint.py new file mode 100644 index 00000000000..e950168f17a --- /dev/null +++ b/tests/semver/test_parse_constraint.py @@ -0,0 +1,86 @@ +import pytest + +from poetry.semver import Version +from poetry.semver import VersionRange +from poetry.semver import VersionUnion +from poetry.semver import parse_constraint + + +@pytest.mark.parametrize( + "constraint,version", + [ + ("~=3.8", VersionRange(min=Version(3, 8), max=Version(4, 0), include_min=True)), + ( + "~= 3.8", + VersionRange(min=Version(3, 8), max=Version(4, 0), include_min=True), + ), + ("~3.8", VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True)), + ("~ 3.8", VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True)), + (">3.8", VersionRange(min=Version(3, 8))), + (">=3.8", VersionRange(min=Version(3, 8), include_min=True)), + (">= 3.8", VersionRange(min=Version(3, 8), include_min=True)), + ( + ">3.8,<=6.5", + VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True), + ), + ( + ">3.8,<= 6.5", + VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True), + ), + ( + "> 3.8,<= 6.5", + VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True), + ), + ( + "> 3.8,<=6.5", + VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True), + ), + ( + ">3.8 ,<=6.5", + VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True), + ), + ( + ">3.8, <=6.5", + VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True), + ), + ( + ">3.8 , <=6.5", + VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True), + ), + ( + "==3.8", + VersionRange( + min=Version(3, 8), max=Version(3, 8), include_min=True, include_max=True + ), + ), + ( + "== 3.8", + VersionRange( + min=Version(3, 8), max=Version(3, 8), include_min=True, include_max=True + ), + ), + ( + "~2.7 || ~3.8", + VersionUnion( + VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True), + VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True), + ), + ), + ( + "~2.7||~3.8", + VersionUnion( + VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True), + VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True), + ), + ), + ( + "~ 2.7||~ 3.8", + VersionUnion( + VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True), + VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True), + ), + ), + ], +) +def test_parse_constraint(constraint, version): + assert parse_constraint(constraint) == version