From 914448c8a01271d717bb0df5fcb2ebe0de5b3f3a Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sun, 2 Apr 2023 12:29:55 +0100 Subject: [PATCH] Reject circular self-add --- src/poetry/console/commands/add.py | 9 +++++++++ tests/console/commands/test_add.py | 12 ++++++++++++ tests/publishing/test_publisher.py | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/poetry/console/commands/add.py b/src/poetry/console/commands/add.py index 451965cd431..9ee892fd25f 100644 --- a/src/poetry/console/commands/add.py +++ b/src/poetry/console/commands/add.py @@ -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: @@ -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"Cannot add dependency on {_constraint['name']} 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 diff --git a/tests/console/commands/test_add.py b/tests/console/commands/test_add.py index 8daf01ea9d9..4c681b7f0a9 100644 --- a/tests/console/commands/test_add.py +++ b/tests/console/commands/test_add.py @@ -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, diff --git a/tests/publishing/test_publisher.py b/tests/publishing/test_publisher.py index 6227cde078c..bf30141541e 100644 --- a/tests/publishing/test_publisher.py +++ b/tests/publishing/test_publisher.py @@ -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