Skip to content

Commit 03ec9db

Browse files
committed
fix #8704 check and validate not exist source
1 parent 0578ed8 commit 03ec9db

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/poetry/console/commands/check.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from __future__ import annotations
22

3+
from collections import defaultdict
4+
from functools import reduce
35
from typing import TYPE_CHECKING
6+
from typing import Any
47

58
from cleo.helpers import option
69

@@ -88,6 +91,37 @@ def _validate_readme(self, readme: str | list[str], poetry_file: Path) -> list[s
8891
errors.append(f"Declared README file does not exist: {name}")
8992
return errors
9093

94+
def _validate_dependencies_source(self, config: dict[str, Any]) -> list[str]:
95+
"""Check dependencies's source are valid"""
96+
dependency_sources: dict[str, set[str]] = defaultdict(set)
97+
sources = {k["name"] for k in config.get("source", [])}
98+
errors = []
99+
100+
dependency_declarations: list[dict[str, str | dict[str, str]]] = []
101+
# scan dependencies and group dependencies settings in pyproject.toml
102+
if "dependencies" in config:
103+
dependency_declarations.append(config["dependencies"])
104+
105+
for group in config.get("group", {}).values():
106+
if "dependencies" in group:
107+
dependency_declarations.append(group["dependencies"])
108+
109+
for dependency_declaration in dependency_declarations:
110+
for dependency, declaration in dependency_declaration.items():
111+
if isinstance(declaration, dict) and "source" in declaration:
112+
dependency_sources[dependency].add(declaration["source"])
113+
114+
all_referenced_sources: set[str] = reduce(
115+
lambda i, j: i | j, dependency_sources.values(), set()
116+
)
117+
if all_referenced_sources not in sources:
118+
errors.extend([
119+
f'Invalid source "{source}" referenced in dependencies.'
120+
for source in all_referenced_sources - sources
121+
])
122+
123+
return errors
124+
91125
def handle(self) -> int:
92126
from poetry.factory import Factory
93127
from poetry.pyproject.toml import PyProjectTOML
@@ -108,6 +142,8 @@ def handle(self) -> int:
108142
errors = self._validate_readme(config["readme"], poetry_file)
109143
check_result["errors"].extend(errors)
110144

145+
check_result["errors"] += self._validate_dependencies_source(config)
146+
111147
# Verify that lock file is consistent
112148
if self.option("lock") and not self.poetry.locker.is_locked():
113149
check_result["errors"] += ["poetry.lock was not found."]

tests/console/commands/test_check.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_check_invalid(
8383
Error: Project name (invalid) is same as one of its dependencies
8484
Error: Unrecognized classifiers: ['Intended Audience :: Clowns'].
8585
Error: Declared README file does not exist: never/exists.md
86+
Error: Invalid source "not-exists" referenced in dependencies.
8687
Error: poetry.lock was not found.
8788
Warning: A wildcard Python dependency is ambiguous.\
8889
Consider specifying a more explicit one.
@@ -98,7 +99,6 @@ def test_check_invalid(
9899
expected_template.format(schema_error=schema_error)
99100
for schema_error in (jsonschema_error, fastjsonschema_error)
100101
}
101-
102102
assert tester.io.fetch_error() in expected
103103

104104

tests/fixtures/invalid_pyproject/pyproject.toml

+15
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,18 @@ classifiers = [
1717
python = "*"
1818
pendulum = {"version" = "^2.0.5", allows-prereleases = true}
1919
invalid = "1.0"
20+
invalid_source = { "version" = "*", source = "not-exists" }
21+
not_consistence_sources = { source = "not-consistence-1" }
22+
23+
[tool.poetry.group.test]
24+
optional = true
25+
26+
[tool.poetry.group.test.dependencies]
27+
not_consistence_sources = { source = "not-consistence-2" }
28+
29+
30+
[[tool.poetry.source]]
31+
name = "not-consistence-1"
32+
33+
[[tool.poetry.source]]
34+
name = "not-consistence-2"

0 commit comments

Comments
 (0)