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

fix(factory): safe get extras in scripts and config #404

Merged
merged 1 commit into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,15 @@ def validate(
# Checking for scripts with extras
if "scripts" in config:
scripts = config["scripts"]
config_extras = config.get("extras", {})

for name, script in scripts.items():
if not isinstance(script, dict):
continue

extras = script["extras"]
extras = script.get("extras", [])
for extra in extras:
if extra not in config["extras"]:
if extra not in config_extras:
result["errors"].append(
f'Script "{name}" requires extra "{extra}" which is not'
" defined."
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/project_failing_strict_validation/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.poetry]
readme = ["README.rst", "README_WITH_ANOTHER_EXTENSION.md"]

[tool.poetry.dependencies]
python = "*"
pathlib2 = { version = "^2.2", python = "3.7", allows-prereleases = true }

[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" }
a_script_with_empty_extras = { reference = "a_script_with_empty_extras.py", type = "file", extras = [] }
another_script = "another_script:main"
43 changes: 43 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,49 @@ def test_validate_fails() -> None:
assert Factory.validate(content) == {"errors": [expected], "warnings": []}


def test_validate_without_strict_fails_only_non_strict() -> None:
project_failing_strict_validation = TOMLFile(
fixtures_dir / "project_failing_strict_validation" / "pyproject.toml"
)
doc: dict[str, Any] = project_failing_strict_validation.read()
content = doc["tool"]["poetry"]

assert Factory.validate(content) == {
"errors": [
"'name' is a required property",
"'version' is a required property",
"'description' is a required property",
],
"warnings": [],
}


def test_validate_strict_fails_strict_and_non_strict() -> None:
project_failing_strict_validation = TOMLFile(
fixtures_dir / "project_failing_strict_validation" / "pyproject.toml"
)
doc: dict[str, Any] = project_failing_strict_validation.read()
content = doc["tool"]["poetry"]

assert Factory.validate(content, strict=True) == {
"errors": [
"'name' is a required property",
"'version' is a required property",
"'description' is a required property",
'Script "a_script_with_unknown_extra" requires extra "foo" which is not'
" defined.",
"Declared README files must be of same type: found text/markdown,"
" text/x-rst",
],
"warnings": [
"A wildcard Python dependency is ambiguous. Consider specifying a more"
" explicit one.",
'The "pathlib2" dependency specifies the "allows-prereleases" property,'
' which is deprecated. Use "allow-prereleases" instead.',
],
}


def test_strict_validation_success_on_multiple_readme_files() -> None:
with_readme_files = TOMLFile(fixtures_dir / "with_readme_files" / "pyproject.toml")
doc: dict[str, Any] = with_readme_files.read()
Expand Down