Skip to content

Commit

Permalink
reject circular self-add
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Apr 2, 2023
1 parent 3649380 commit 66da58d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def handle(self) -> int:
# dictionary.
content: dict[str, Any] = self.poetry.file.read()
poetry_content = content["tool"]["poetry"]
project_name = canonicalize_name(poetry_content["name"])

if group == MAIN_GROUP:
if "dependencies" not in poetry_content:
Expand Down Expand Up @@ -223,6 +224,14 @@ def handle(self) -> int:

canonical_constraint_name = canonicalize_name(constraint_name)

if canonical_constraint_name == project_name:
self.line_error(
f"<error>Cannot add dependency on <c1>{_constraint['name']}</c1> to"
" project with the same name."
)
self.line_error("\nNo changes were applied.")
return 1

for key in section:
if canonicalize_name(key) == canonical_constraint_name:
section[key] = constraint
Expand Down
12 changes: 12 additions & 0 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,18 @@ def test_add_should_skip_when_adding_canonicalized_existing_package_with_no_cons
assert expected in tester.io.fetch_output()


def test_add_should_fail_circular_dependency(
app: PoetryTestApplication, repo: TestRepository, tester: CommandTester
) -> None:
repo.add_package(get_package("simple-project", "1.1.2"))
result = tester.execute("simple-project")

assert result == 1

expected = "Cannot add dependency on simple-project to project with the same name."
assert expected in tester.io.fetch_error()


def test_add_latest_should_not_create_duplicate_keys(
project_factory: ProjectFactory,
repo: TestRepository,
Expand Down

0 comments on commit 66da58d

Please sign in to comment.