Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate extras against dependencies and in schema #542

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ def validate(
'Use "allow-prereleases" instead.'
)

if "extras" in config:
for extra_name, requirements in config["extras"].items():
extra_name = canonicalize_name(extra_name)

for req in requirements:
for dependency in config["dependencies"].keys():
if req == dependency:
Panaetius marked this conversation as resolved.
Show resolved Hide resolved
break
else:
result["warnings"].append(
f'Cannot find dependency "{req}" for extra '
f'"{extra_name}" in main dependencies.'
)

# Checking for scripts with extras
if "scripts" in config:
scripts = config["scripts"]
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/core/json/schemas/poetry-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@
"^[a-zA-Z-_.0-9]+$": {
"type": "array",
"items": {
"type": "string"
"type": "string",
"pattern": "^[a-zA-Z-_.0-9]+$"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ readme = ["README.rst", "README_WITH_ANOTHER_EXTENSION.md"]
python = "*"
pathlib2 = { version = "^2.2", python = "3.7", allows-prereleases = true }

[tool.poetry.extras]
some_extras = ["missing_extra", "another_missing_extra"]

[tool.poetry.scripts]
a_script_with_unknown_extra = { reference = "a_script_with_unknown_extra.py", type = "file", extras = ["foo"] }
a_script_without_extras = { reference = "a_script_without_extras.py", type = "file" }
Expand Down
12 changes: 12 additions & 0 deletions tests/json/test_poetry_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ def test_multiline_description(base_object: dict[str, Any]) -> None:
errors = validate_object(base_object, "poetry-schema")
assert len(errors) == 1
assert errors[0] == f"[description] {bad_description!r} does not match '^[^\\n]*$'"


def test_bad_extra(base_object: dict[str, Any]) -> None:
bad_extra = "a{[*+"
base_object["extras"] = {}
base_object["extras"]["test"] = [bad_extra]

errors = validate_object(base_object, "poetry-schema")
assert len(errors) == 1
assert (
errors[0] == f"[extras.test.0] {bad_extra!r} does not match '^[a-zA-Z-_.0-9]+$'"
)
8 changes: 8 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ def test_validate_strict_fails_strict_and_non_strict() -> None:
'The "pathlib2" dependency specifies the "allows-prereleases" property,'
' which is deprecated. Use "allow-prereleases" instead.'
),
(
'Cannot find dependency "missing_extra" for extra "some-extras" in '
"main dependencies."
),
(
'Cannot find dependency "another_missing_extra" for extra '
'"some-extras" in main dependencies.'
),
],
}

Expand Down