Skip to content

Commit

Permalink
remove: improve error message when trying to remove a package from a …
Browse files Browse the repository at this point in the history
…project without any dependencies (#9918)
  • Loading branch information
radoering authored Dec 23, 2024
1 parent 2da550a commit a7079ac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/poetry/console/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
38 changes: 38 additions & 0 deletions tests/console/commands/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a7079ac

Please sign in to comment.