Skip to content

Commit

Permalink
* remove moving package to different section on poetry add
Browse files Browse the repository at this point in the history
  • Loading branch information
finswimmer committed Sep 10, 2020
1 parent 17b165b commit 74adf7e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
39 changes: 28 additions & 11 deletions poetry/console/commands/add.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Dict
from typing import List

from cleo import argument
from cleo import option

Expand Down Expand Up @@ -86,18 +89,14 @@ def handle(self):
if section not in poetry_content:
poetry_content[section] = {}

for name in packages:
for key in poetry_content[section]:
if key.lower() == name.lower():
pair = self._parse_requirements([name])[0]
if (
"git" in pair
or "url" in pair
or pair.get("version") == "latest"
):
continue
packages = self.remove_existing_packages_from_input(
packages, poetry_content, section
)

raise ValueError("Package {} is already present".format(name))
if not packages:
self.poetry.file.write(content)
self.line("Nothing to add.", style="info")
return 0

requirements = self._determine_requirements(
packages,
Expand Down Expand Up @@ -184,3 +183,21 @@ def handle(self):
self.poetry.file.write(original_content)

return status

def remove_existing_packages_from_input(
self, packages, poetry_content, target_section
): # type: (List[str], Dict, str) -> List[str]
existing_packages = []

for name in packages:
for key in poetry_content[target_section]:
if key.lower() == name.lower():
existing_packages.append(name)
self.line(
"{name} is already in pyproject.toml. Skipping.".format(
name=name
)
)
packages = [name for name in packages if name not in existing_packages]

return packages
23 changes: 15 additions & 8 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,19 +702,22 @@ def test_add_should_not_select_prereleases(app, repo, tester):
assert content["dependencies"]["pyyaml"] == "^3.13"


def test_add_should_display_an_error_when_adding_existing_package_with_no_constraint(
def test_add_should_skip_when_adding_existing_package_with_no_constraint(
app, repo, tester
):
content = app.poetry.file.read()
content["tool"]["poetry"]["dependencies"]["foo"] = "^1.0"
app.poetry.file.write(content)

repo.add_package(get_package("foo", "1.1.2"))
tester.execute("foo")

with pytest.raises(ValueError) as e:
tester.execute("foo")
expected = """\
foo is already in pyproject.toml. Skipping.
Nothing to add.
"""

assert "Package foo is already present" == str(e.value)
assert expected in tester.io.fetch_output()


def test_add_should_work_when_adding_existing_package_with_latest_constraint(
Expand Down Expand Up @@ -1503,7 +1506,7 @@ def test_add_should_not_select_prereleases_old_installer(
assert content["dependencies"]["pyyaml"] == "^3.13"


def test_add_should_display_an_error_when_adding_existing_package_with_no_constraint_old_installer(
def test_add_should_skip_when_adding_existing_package_with_no_constraint_old_installer(
app, repo, installer, old_tester
):
content = app.poetry.file.read()
Expand All @@ -1512,10 +1515,14 @@ def test_add_should_display_an_error_when_adding_existing_package_with_no_constr

repo.add_package(get_package("foo", "1.1.2"))

with pytest.raises(ValueError) as e:
old_tester.execute("foo")
old_tester.execute("foo")

assert "Package foo is already present" == str(e.value)
expected = """\
foo is already in pyproject.toml. Skipping.
Nothing to add.
"""

assert expected in old_tester.io.fetch_output()


def test_add_should_work_when_adding_existing_package_with_latest_constraint_old_installer(
Expand Down

0 comments on commit 74adf7e

Please sign in to comment.