Skip to content

Commit

Permalink
tests: increase coverage for create_nested_marker()
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Sep 3, 2022
1 parent ae15379 commit 8ff1a72
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/poetry/core/packages/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
if TYPE_CHECKING:
from poetry.core.packages.constraints import BaseConstraint
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.semver.version_union import VersionUnion
from poetry.core.version.markers import BaseMarker

# Even though we've `from __future__ import annotations`, mypy doesn't seem to like
Expand Down Expand Up @@ -202,7 +201,7 @@ def contains_group_without_marker(markers: ConvertedMarkers, marker_name: str) -

def create_nested_marker(
name: str,
constraint: BaseConstraint | VersionUnion | Version | VersionConstraint,
constraint: BaseConstraint | VersionConstraint,
) -> str:
from poetry.core.packages.constraints.constraint import Constraint
from poetry.core.packages.constraints.multi_constraint import MultiConstraint
Expand Down Expand Up @@ -247,11 +246,11 @@ def create_nested_marker(
# `python_version` is a special case: to keep the constructed marker equivalent
# to the constraint we need to be careful with the precision.
#
# PEP 440 tells us that that when we come to make the comparison the release
# PEP 440 tells us that when we come to make the comparison the release
# segment will be zero padded: eg "<= 3.10" is equivalent to "<= 3.10.0".
#
# But "python_version <= 3.10" is _not_ equivalent to "python_version <= 3.10.0" -
# see normalize_python_version_markers.
# But "python_version <= 3.10" is _not_ equivalent to "python_version <= 3.10.0"
# - see normalize_python_version_markers.
#
# A similar issue arises for a constraint like "> 3.6".
if constraint.min is not None:
Expand Down
78 changes: 75 additions & 3 deletions tests/packages/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import pytest

from poetry.core.packages.constraints import parse_constraint
from poetry.core.packages.utils.utils import convert_markers
from poetry.core.packages.utils.utils import create_nested_marker
from poetry.core.packages.utils.utils import get_python_constraint_from_marker
from poetry.core.packages.utils.utils import is_python_project
from poetry.core.semver.helpers import parse_constraint
from poetry.core.semver.helpers import parse_constraint as parse_version_constraint
from poetry.core.version.markers import parse_marker


Expand Down Expand Up @@ -78,6 +80,76 @@ def test_convert_markers(
assert converted == expected


@pytest.mark.parametrize(
["constraint", "expected"],
[
("*", ""),
("==linux", 'sys_platform == "linux"'),
("!=win32", 'sys_platform != "win32"'),
("!=linux, !=win32", 'sys_platform != "linux" and sys_platform != "win32"'),
("==linux || ==win32", 'sys_platform == "linux" or sys_platform == "win32"'),
],
)
def test_create_nested_marker_base_constraint(constraint: str, expected: str) -> None:
assert (
create_nested_marker("sys_platform", parse_constraint(constraint)) == expected
)


@pytest.mark.parametrize(
["constraint", "expected"],
[
("*", ""),
# simple version
("3", 'python_version == "3"'),
("3.9", 'python_version == "3.9"'),
("3.9.0", 'python_full_version == "3.9.0"'),
("3.9.1", 'python_full_version == "3.9.1"'),
# min
(">=3", 'python_version >= "3"'),
(">=3.9", 'python_version >= "3.9"'),
(">=3.9.0", 'python_full_version >= "3.9.0"'),
(">=3.9.1", 'python_full_version >= "3.9.1"'),
(">3", 'python_full_version > "3.0.0"'),
(">3.9", 'python_full_version > "3.9.0"'),
(">3.9.0", 'python_full_version > "3.9.0"'),
(">3.9.1", 'python_full_version > "3.9.1"'),
# max
("<3", 'python_version < "3"'),
("<3.9", 'python_version < "3.9"'),
("<3.9.0", 'python_full_version < "3.9.0"'),
("<3.9.1", 'python_full_version < "3.9.1"'),
("<=3", 'python_full_version <= "3.0.0"'),
("<=3.9", 'python_full_version <= "3.9.0"'),
("<=3.9.0", 'python_full_version <= "3.9.0"'),
("<=3.9.1", 'python_full_version <= "3.9.1"'),
# min and max
(">=3.7, <3.9", 'python_version >= "3.7" and python_version < "3.9"'),
(">=3.7, <=3.9", 'python_version >= "3.7" and python_full_version <= "3.9.0"'),
(">3.7, <3.9", 'python_full_version > "3.7.0" and python_version < "3.9"'),
(
">3.7, <=3.9",
'python_full_version > "3.7.0" and python_full_version <= "3.9.0"',
),
# union
("<3.7 || >=3.8", '(python_version < "3.7") or (python_version >= "3.8")'),
(
">=3.7,<3.8 || >=3.9,<=3.10",
'(python_version >= "3.7" and python_version < "3.8")'
' or (python_version >= "3.9" and python_full_version <= "3.10.0")',
),
],
)
def test_create_nested_marker_version_constraint(
constraint: str,
expected: str,
) -> None:
assert (
create_nested_marker("python_version", parse_version_constraint(constraint))
== expected
)


@pytest.mark.parametrize(
["marker", "constraint"],
[
Expand Down Expand Up @@ -143,7 +215,7 @@ def test_convert_markers(
)
def test_get_python_constraint_from_marker(marker: str, constraint: str) -> None:
marker_parsed = parse_marker(marker)
constraint_parsed = parse_constraint(constraint)
constraint_parsed = parse_version_constraint(constraint)
assert get_python_constraint_from_marker(marker_parsed) == constraint_parsed


Expand All @@ -158,6 +230,6 @@ def test_get_python_constraint_from_marker(marker: str, constraint: str) -> None
("does_not_exist", False),
],
)
def test_package_utils_is_python_project(fixture: str, result: bool) -> None:
def test_is_python_project(fixture: str, result: bool) -> None:
path = Path(__file__).parent.parent.parent / "fixtures" / fixture
assert is_python_project(path) == result

0 comments on commit 8ff1a72

Please sign in to comment.