Skip to content

Commit

Permalink
fix(factory): safe get extras in scripts and config
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner authored and neersighted committed Jun 26, 2022
1 parent 96f2a91 commit f6e8b58
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
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

0 comments on commit f6e8b58

Please sign in to comment.