diff --git a/src/poetry/console/commands/remove.py b/src/poetry/console/commands/remove.py index a4ce37fc44a..1cb4da54509 100644 --- a/src/poetry/console/commands/remove.py +++ b/src/poetry/console/commands/remove.py @@ -68,13 +68,13 @@ def handle(self) -> int: if group is None: # remove from all groups removed = set() - group_sections = [ - ( - MAIN_GROUP, - project_content.get("dependencies", []), - poetry_content.get("dependencies", {}), + group_sections = [] + project_dependencies = project_content.get("dependencies", []) + poetry_dependencies = poetry_content.get("dependencies", {}) + if project_dependencies or poetry_dependencies: + group_sections.append( + (MAIN_GROUP, project_dependencies, poetry_dependencies) ) - ] group_sections.extend( (group_name, [], group_section.get("dependencies", {})) for group_name, group_section in poetry_content.get("group", {}).items() diff --git a/tests/console/commands/test_remove.py b/tests/console/commands/test_remove.py index fb67774a50e..59730c544b3 100644 --- a/tests/console/commands/test_remove.py +++ b/tests/console/commands/test_remove.py @@ -358,6 +358,44 @@ def test_remove_canonicalized_named_removes_dependency_correctly( assert expected in string_content +def test_remove_package_does_not_exist( + tester: CommandTester, + app: PoetryTestApplication, + repo: TestRepository, + command_tester_factory: CommandTesterFactory, +) -> None: + repo.add_package(Package("foo", "2.0.0")) + + original_content = app.poetry.file.read().as_string() + + with pytest.raises(ValueError) as e: + tester.execute("foo") + + assert str(e.value) == "The following packages were not found: foo" + assert app.poetry.file.read().as_string() == original_content + + +def test_remove_package_no_dependencies( + tester: CommandTester, + app: PoetryTestApplication, + repo: TestRepository, + command_tester_factory: CommandTesterFactory, +) -> None: + repo.add_package(Package("foo", "2.0.0")) + + pyproject: dict[str, Any] = app.poetry.file.read() + assert "dependencies" not in pyproject["project"] + del pyproject["tool"]["poetry"]["dependencies"] + pyproject = cast("TOMLDocument", pyproject) + app.poetry.file.write(pyproject) + app.poetry.package._dependency_groups = {} + + with pytest.raises(ValueError) as e: + tester.execute("foo") + + assert str(e.value) == "The following packages were not found: foo" + + def test_remove_command_should_not_write_changes_upon_installer_errors( tester: CommandTester, app: PoetryTestApplication,