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

replace existing dependency when adding dependency with version constraint #4945

Merged
merged 1 commit into from
Jan 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/poetry/console/commands/add.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import contextlib

from typing import Dict
from typing import List

Expand Down Expand Up @@ -196,6 +198,12 @@ def handle(self) -> int:
constraint = constraint["version"]

section[_constraint["name"]] = constraint

with contextlib.suppress(ValueError):
self.poetry.package.dependency_group(group).remove_dependency(
_constraint["name"]
)

self.poetry.package.add_dependency(
Factory.create_dependency(
_constraint["name"],
Expand Down
47 changes: 47 additions & 0 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,53 @@ def test_add_no_constraint(
assert content["dependencies"]["cachy"] == "^0.2.0"


def test_add_replace_by_constraint(
app: "PoetryTestApplication", repo: "TestRepository", tester: "CommandTester"
):
repo.add_package(get_package("cachy", "0.1.0"))
repo.add_package(get_package("cachy", "0.2.0"))

tester.execute("cachy")

expected = """\
Using version ^0.2.0 for cachy

Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 1 install, 0 updates, 0 removals

• Installing cachy (0.2.0)
"""
assert tester.io.fetch_output() == expected
assert tester.command.installer.executor.installations_count == 1

content = app.poetry.file.read()["tool"]["poetry"]

assert "cachy" in content["dependencies"]
assert content["dependencies"]["cachy"] == "^0.2.0"

tester.execute("cachy@0.1.0")
expected = """
Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 1 install, 0 updates, 0 removals

• Installing cachy (0.1.0)
"""
assert tester.io.fetch_output() == expected

content = app.poetry.file.read()["tool"]["poetry"]

assert "cachy" in content["dependencies"]
assert content["dependencies"]["cachy"] == "0.1.0"


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