Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure no duplicate package names are stored in pyproject.toml #6832

Merged
merged 1 commit into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/poetry/console/commands/add.py
Expand Up @@ -215,7 +215,15 @@ def handle(self) -> int:

constraint_name = _constraint["name"]
assert isinstance(constraint_name, str)
section[constraint_name] = constraint

canonical_constraint_name = canonicalize_name(constraint_name)

for key in section:
if canonicalize_name(key) == canonical_constraint_name:
section[key] = constraint
break
else:
section[constraint_name] = constraint

with contextlib.suppress(ValueError):
self.poetry.package.dependency_group(group).remove_dependency(
Expand Down
38 changes: 38 additions & 0 deletions tests/console/commands/test_add.py
Expand Up @@ -1055,6 +1055,44 @@ def test_add_should_skip_when_adding_canonicalized_existing_package_with_no_cons
assert expected in tester.io.fetch_output()


def test_add_latest_should_not_create_duplicate_keys(
project_factory: ProjectFactory,
repo: TestRepository,
command_tester_factory: CommandTesterFactory,
):
pyproject_content = """\
[tool.poetry]
name = "simple-project"
version = "1.2.3"
description = "Some description."
authors = [
"Python Poetry <tests@python-poetry.org>"
]
license = "MIT"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.6"
Foo = "^0.6"
"""

poetry = project_factory(name="simple-project", pyproject_content=pyproject_content)
content = poetry.file.read()

assert "Foo" in content["tool"]["poetry"]["dependencies"]
assert content["tool"]["poetry"]["dependencies"]["Foo"] == "^0.6"
assert "foo" not in content["tool"]["poetry"]["dependencies"]

tester = command_tester_factory("add", poetry=poetry)
repo.add_package(get_package("foo", "1.1.2"))
tester.execute("foo@latest")

updated_content = poetry.file.read()
assert "Foo" in updated_content["tool"]["poetry"]["dependencies"]
assert updated_content["tool"]["poetry"]["dependencies"]["Foo"] == "^1.1.2"
assert "foo" not in updated_content["tool"]["poetry"]["dependencies"]


def test_add_should_work_when_adding_existing_package_with_latest_constraint(
app: PoetryTestApplication, repo: TestRepository, tester: CommandTester
):
Expand Down