Skip to content

Commit 20587f1

Browse files
finswimmerneersighted
authored andcommitted
replace existing dependency when adding dependency with version constraint
1 parent ecb030e commit 20587f1

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/poetry/console/commands/add.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import contextlib
2+
13
from typing import Dict
24
from typing import List
35

@@ -203,6 +205,12 @@ def handle(self) -> int:
203205
constraint = constraint["version"]
204206

205207
section[_constraint["name"]] = constraint
208+
209+
with contextlib.suppress(ValueError):
210+
self.poetry.package.dependency_group(group).remove_dependency(
211+
_constraint["name"]
212+
)
213+
206214
self.poetry.package.add_dependency(
207215
Factory.create_dependency(
208216
_constraint["name"],

tests/console/commands/test_add.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,53 @@ def test_add_no_constraint(
6767
assert content["dependencies"]["cachy"] == "^0.2.0"
6868

6969

70+
def test_add_replace_by_constraint(
71+
app: "PoetryTestApplication", repo: "TestRepository", tester: "CommandTester"
72+
):
73+
repo.add_package(get_package("cachy", "0.1.0"))
74+
repo.add_package(get_package("cachy", "0.2.0"))
75+
76+
tester.execute("cachy")
77+
78+
expected = """\
79+
Using version ^0.2.0 for cachy
80+
81+
Updating dependencies
82+
Resolving dependencies...
83+
84+
Writing lock file
85+
86+
Package operations: 1 install, 0 updates, 0 removals
87+
88+
• Installing cachy (0.2.0)
89+
"""
90+
assert tester.io.fetch_output() == expected
91+
assert tester.command.installer.executor.installations_count == 1
92+
93+
content = app.poetry.file.read()["tool"]["poetry"]
94+
95+
assert "cachy" in content["dependencies"]
96+
assert content["dependencies"]["cachy"] == "^0.2.0"
97+
98+
tester.execute("[email protected]")
99+
expected = """
100+
Updating dependencies
101+
Resolving dependencies...
102+
103+
Writing lock file
104+
105+
Package operations: 1 install, 0 updates, 0 removals
106+
107+
• Installing cachy (0.1.0)
108+
"""
109+
assert tester.io.fetch_output() == expected
110+
111+
content = app.poetry.file.read()["tool"]["poetry"]
112+
113+
assert "cachy" in content["dependencies"]
114+
assert content["dependencies"]["cachy"] == "0.1.0"
115+
116+
70117
def test_add_no_constraint_editable_error(
71118
app: "PoetryTestApplication", repo: "TestRepository", tester: "CommandTester"
72119
):

0 commit comments

Comments
 (0)