Skip to content

Commit

Permalink
raise InvalidMarker for invalid marker
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Mar 31, 2023
1 parent ee2f92b commit 37fcb8e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from poetry.core.constraints.generic import MultiConstraint
from poetry.core.constraints.generic import UnionConstraint
from poetry.core.constraints.version import VersionConstraint
from poetry.core.constraints.version.exceptions import ParseConstraintError
from poetry.core.version.grammars import GRAMMAR_PEP_508_MARKERS
from poetry.core.version.parser import Parser

Expand Down Expand Up @@ -313,7 +314,7 @@ def __init__(

parsed_constraint: BaseConstraint | VersionConstraint
parser: Callable[[str], BaseConstraint | VersionConstraint]
constraint_string = str(constraint)
original_constraint_string = constraint_string = str(constraint)

# Extract operator and value
m = self._CONSTRAINT_RE.match(constraint_string)
Expand Down Expand Up @@ -346,9 +347,7 @@ def __init__(
if self._operator == "in":
glue = " || "

parsed_constraint = parser(glue.join(versions))
else:
parsed_constraint = parser(constraint_string)
constraint_string = glue.join(versions)
else:
# if we have a in/not in operator we split the constraint
# into a union/multi-constraint of single constraint
Expand All @@ -357,7 +356,10 @@ def __init__(
values = re.split("[ ,]+", self._value)
constraint_string = glue.join(f"{op} {value}" for value in values)

try:
parsed_constraint = parser(constraint_string)
except ParseConstraintError as e:
raise InvalidMarker(f"Invalid marker: {original_constraint_string}") from e

super().__init__(name, parsed_constraint)

Expand Down
25 changes: 25 additions & 0 deletions tests/packages/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

from poetry.core.constraints.version.exceptions import ParseConstraintError
from poetry.core.packages.dependency import Dependency
from poetry.core.version.markers import InvalidMarker
from poetry.core.version.markers import parse_marker
from poetry.core.version.requirements import InvalidRequirement


@pytest.mark.parametrize(
Expand Down Expand Up @@ -184,6 +186,29 @@ def test_to_pep_508_combination() -> None:
assert dependency.to_pep_508() == "foo (>=1.2,<1.3,!=1.2.5)"


@pytest.mark.parametrize(
"requirement",
[
"enum34; extra == ':python_version < \"3.4\"'",
"enum34; extra == \":python_version < '3.4'\"",
],
)
def test_to_pep_508_with_invalid_marker(requirement: str) -> None:
with pytest.raises(InvalidMarker):
_ = Dependency.create_from_pep_508(requirement)


@pytest.mark.parametrize(
"requirement",
[
'enum34; extra == ":python_version < "3.4""',
],
)
def test_to_pep_508_with_invalid_requirement(requirement: str) -> None:
with pytest.raises(InvalidRequirement):
_ = Dependency.create_from_pep_508(requirement)


def test_complete_name() -> None:
assert Dependency("foo", ">=1.2.3").complete_name == "foo"
assert (
Expand Down

0 comments on commit 37fcb8e

Please sign in to comment.