Skip to content

Commit

Permalink
tolerate funky group names at 'poetry add' (#9277)
Browse files Browse the repository at this point in the history
Co-authored-by: Randy Döring <[email protected]>
  • Loading branch information
dimbleby and radoering committed Apr 7, 2024
1 parent ea26f3d commit 9d9bcf1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
19 changes: 9 additions & 10 deletions src/poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class AddCommand(InstallerCommand, InitCommand):
def handle(self) -> int:
from poetry.core.constraints.version import parse_constraint
from tomlkit import inline_table
from tomlkit import parse as parse_toml
from tomlkit import nl
from tomlkit import table

from poetry.factory import Factory
Expand Down Expand Up @@ -150,17 +150,17 @@ def handle(self) -> int:
poetry_content["group"] = table(is_super_table=True)

groups = poetry_content["group"]

if group not in groups:
dependencies_toml: dict[str, Any] = parse_toml(
f"[tool.poetry.group.{group}.dependencies]\n\n"
)
group_table = dependencies_toml["tool"]["poetry"]["group"][group]
poetry_content["group"][group] = group_table
groups[group] = table()
groups.add(nl())

this_group = groups[group]

if "dependencies" not in poetry_content["group"][group]:
poetry_content["group"][group]["dependencies"] = table()
if "dependencies" not in this_group:
this_group["dependencies"] = table()

section = poetry_content["group"][group]["dependencies"]
section = this_group["dependencies"]

existing_packages = self.get_existing_packages_from_input(packages, section)

Expand Down Expand Up @@ -266,7 +266,6 @@ def handle(self) -> int:
)

# Refresh the locker
content["tool"]["poetry"] = poetry_content
self.poetry.locker.set_pyproject_data(content)
self.installer.set_locker(self.poetry.locker)

Expand Down
17 changes: 11 additions & 6 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,13 +992,17 @@ def test_add_constraint_not_found_with_source(
assert str(e.value) == "Could not find a matching version of package cachy"


@pytest.mark.parametrize("group_name", ["dev", "foo.BAR"])
def test_add_to_section_that_does_not_exist_yet(
app: PoetryTestApplication, repo: TestRepository, tester: CommandTester
app: PoetryTestApplication,
repo: TestRepository,
tester: CommandTester,
group_name: str,
) -> None:
repo.add_package(get_package("cachy", "0.1.0"))
repo.add_package(get_package("cachy", "0.2.0"))

tester.execute("cachy --group dev")
tester.execute(f"cachy --group {group_name}")

expected = """\
Using version ^0.2.0 for cachy
Expand All @@ -1020,12 +1024,13 @@ def test_add_to_section_that_does_not_exist_yet(
pyproject: dict[str, Any] = app.poetry.file.read()
content = pyproject["tool"]["poetry"]

assert "cachy" in content["group"]["dev"]["dependencies"]
assert content["group"]["dev"]["dependencies"]["cachy"] == "^0.2.0"
assert "cachy" in content["group"][group_name]["dependencies"]
assert content["group"][group_name]["dependencies"]["cachy"] == "^0.2.0"

expected = """\
escaped_group_name = f'"{group_name}"' if "." in group_name else group_name
expected = f"""\
[tool.poetry.group.dev.dependencies]
[tool.poetry.group.{escaped_group_name}.dependencies]
cachy = "^0.2.0"
"""
Expand Down

0 comments on commit 9d9bcf1

Please sign in to comment.