Skip to content

Commit

Permalink
Convert most parse_poetry_version tests to doctests
Browse files Browse the repository at this point in the history
This makes it more self-contained
  • Loading branch information
maresb committed Sep 6, 2024
1 parent 693e95f commit 05dca1f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 79 deletions.
79 changes: 79 additions & 0 deletions grayskull/strategy/parse_poetry_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ def parse_version(version: str) -> Dict[str, Optional[int]]:
"""
Parses a version string (not necessarily semver) to a dictionary with keys
"major", "minor", and "patch". "minor" and "patch" are possibly None.
>>> parse_version("0")
{'major': 0, 'minor': None, 'patch': None}
>>> parse_version("1")
{'major': 1, 'minor': None, 'patch': None}
>>> parse_version("1.2")
{'major': 1, 'minor': 2, 'patch': None}
>>> parse_version("1.2.3")
{'major': 1, 'minor': 2, 'patch': 3}
"""
match = VERSION_REGEX.search(version)
if not match:
Expand Down Expand Up @@ -72,6 +81,22 @@ def get_caret_ceiling(target: str) -> str:
- If the major version is not 0, the ceiling is determined by
coercing it to semver and then bumping the major version.
Example: 1 => 2.0.0, 1.2 => 2.0.0, 1.2.3 => 2.0.0
# Examples from Poetry docs
>>> get_caret_ceiling("0")
'1.0.0'
>>> get_caret_ceiling("0.0")
'0.1.0'
>>> get_caret_ceiling("0.0.3")
'0.0.4'
>>> get_caret_ceiling("0.2.3")
'0.3.0'
>>> get_caret_ceiling("1")
'2.0.0'
>>> get_caret_ceiling("1.2")
'2.0.0'
>>> get_caret_ceiling("1.2.3")
'2.0.0'
"""
if not semver.VersionInfo.is_valid(target):
target_dict = parse_version(target)
Expand Down Expand Up @@ -102,6 +127,14 @@ def get_caret_ceiling(target: str) -> str:
def get_tilde_ceiling(target: str) -> str:
"""
Accepts a Poetry tilde target and returns the exclusive version ceiling.
# Examples from Poetry docs
>>> get_tilde_ceiling("1")
'2.0.0'
>>> get_tilde_ceiling("1.2")
'1.3.0'
>>> get_tilde_ceiling("1.2.3")
'1.3.0'
"""
target_dict = parse_version(target)
if target_dict["minor"]:
Expand All @@ -115,6 +148,52 @@ def encode_poetry_version(poetry_specifier: str) -> str:
Encodes Poetry version specifier as a Conda version specifier.
Example: ^1 => >=1.0.0,<2.0.0
# should be unchanged
>>> encode_poetry_version("1.*")
'1.*'
>>> encode_poetry_version(">=1,<2")
'>=1,<2'
>>> encode_poetry_version("==1.2.3")
'==1.2.3'
>>> encode_poetry_version("!=1.2.3")
'!=1.2.3'
# strip spaces
>>> encode_poetry_version(">= 1, < 2")
'>=1,<2'
# handle exact version specifiers correctly
>>> encode_poetry_version("1.2.3")
'1.2.3'
>>> encode_poetry_version("==1.2.3")
'==1.2.3'
# handle caret operator correctly
# examples from Poetry docs
>>> encode_poetry_version("^0")
'>=0.0.0,<1.0.0'
>>> encode_poetry_version("^0.0")
'>=0.0.0,<0.1.0'
>>> encode_poetry_version("^0.0.3")
'>=0.0.3,<0.0.4'
>>> encode_poetry_version("^0.2.3")
'>=0.2.3,<0.3.0'
>>> encode_poetry_version("^1")
'>=1.0.0,<2.0.0'
>>> encode_poetry_version("^1.2")
'>=1.2.0,<2.0.0'
>>> encode_poetry_version("^1.2.3")
'>=1.2.3,<2.0.0'
# handle tilde operator correctly
# examples from Poetry docs
>>> encode_poetry_version("~1")
'>=1.0.0,<2.0.0'
>>> encode_poetry_version("~1.2")
'>=1.2.0,<1.3.0'
>>> encode_poetry_version("~1.2.3")
'>=1.2.3,<1.3.0'
"""
poetry_clauses = poetry_specifier.split(",")

Expand Down
80 changes: 1 addition & 79 deletions tests/test_parse_poetry_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,7 @@

import pytest

from grayskull.strategy.parse_poetry_version import (
InvalidVersion,
encode_poetry_version,
get_caret_ceiling,
get_tilde_ceiling,
parse_version,
)


@pytest.mark.parametrize(
"version, major, minor, patch",
[
("0", 0, None, None),
("1", 1, None, None),
("1.2", 1, 2, None),
("1.2.3", 1, 2, 3),
],
)
def test_parse_version_success(version, major, minor, patch):
assert parse_version(version) == {"major": major, "minor": minor, "patch": patch}
from grayskull.strategy.parse_poetry_version import InvalidVersion, parse_version


@pytest.mark.parametrize(
Expand All @@ -30,62 +11,3 @@ def test_parse_version_success(version, major, minor, patch):
def test_parse_version_failure(invalid_version):
with pytest.raises(InvalidVersion):
parse_version(invalid_version)


@pytest.mark.parametrize(
"version, ceiling_version",
[
("0", "1.0.0"),
("0.0", "0.1.0"),
("0.0.3", "0.0.4"),
("0.2.3", "0.3.0"),
("1", "2.0.0"),
("1.2", "2.0.0"),
("1.2.3", "2.0.0"),
],
)
def test_get_caret_ceiling(version, ceiling_version):
# examples from Poetry docs
assert get_caret_ceiling(version) == ceiling_version


@pytest.mark.parametrize(
"version, ceiling_version",
[("1", "2.0.0"), ("1.2", "1.3.0"), ("1.2.3", "1.3.0")],
)
def test_get_tilde_ceiling(version, ceiling_version):
# examples from Poetry docs
assert get_tilde_ceiling(version) == ceiling_version


@pytest.mark.parametrize(
"version, encoded_version",
[
# should be unchanged
("1.*", "1.*"),
(">=1,<2", ">=1,<2"),
("==1.2.3", "==1.2.3"),
("!=1.2.3", "!=1.2.3"),
# strip spaces
(">= 1, < 2", ">=1,<2"),
# handle exact version specifiers correctly
("1.2.3", "1.2.3"),
("==1.2.3", "==1.2.3"),
# handle caret operator correctly
# examples from Poetry docs
("^0", ">=0.0.0,<1.0.0"),
("^0.0", ">=0.0.0,<0.1.0"),
("^0.0.3", ">=0.0.3,<0.0.4"),
("^0.2.3", ">=0.2.3,<0.3.0"),
("^1", ">=1.0.0,<2.0.0"),
("^1.2", ">=1.2.0,<2.0.0"),
("^1.2.3", ">=1.2.3,<2.0.0"),
# handle tilde operator correctly
# examples from Poetry docs
("~1", ">=1.0.0,<2.0.0"),
("~1.2", ">=1.2.0,<1.3.0"),
("~1.2.3", ">=1.2.3,<1.3.0"),
],
)
def test_encode_poetry_version(version, encoded_version):
assert encode_poetry_version(version) == encoded_version

0 comments on commit 05dca1f

Please sign in to comment.