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 17, 2023
1 parent a5326df commit 914448c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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 @@ -1073,6 +1073,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
2 changes: 1 addition & 1 deletion tests/publishing/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

from cleo.io.buffered_io import BufferedIO
from cleo.io.null_io import NullIO
from packaging.utils import canonicalize_name

from poetry.factory import Factory
from packaging.utils import canonicalize_name
from poetry.publishing.publisher import Publisher


Expand Down

0 comments on commit 914448c

Please sign in to comment.