Skip to content

Commit

Permalink
replace existing dependency when adding dependency with version const…
Browse files Browse the repository at this point in the history
…raint
  • Loading branch information
finswimmer authored and neersighted committed Jan 17, 2022
1 parent ecb030e commit 20587f1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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 @@ -203,6 +205,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

0 comments on commit 20587f1

Please sign in to comment.