未验证 提交 4227f801 编写于 作者: F Frost Ming 提交者: GitHub

Merge pull request #371 from pdm-project/bugfix/no-overwrite-import

Don't overwrite existing dependencies
Don't overwrite existing dependencies when importing from requirements.txt.
......@@ -17,6 +17,7 @@ from pdm.cli.utils import (
find_importable_files,
format_lockfile,
format_resolution_impossible,
merge_dictionary,
save_version_specifiers,
set_env_in_reg,
translate_sections,
......@@ -537,8 +538,8 @@ def do_import(
tomlkit.comment("See https://www.python.org/dev/peps/pep-0621/")
)
pyproject["project"].update(project_data)
pyproject["tool"]["pdm"].update(settings)
merge_dictionary(pyproject["project"], project_data)
merge_dictionary(pyproject["tool"]["pdm"], settings)
pyproject["build-system"] = {
"requires": ["pdm-pep517"],
"build-backend": "pdm.pep517.api",
......
......@@ -102,7 +102,7 @@ sections_group.add_argument(
dest="default",
action="store_false",
default=True,
help="Don't include dependencies from default seciton",
help="Don't include dependencies from default section",
)
......
......@@ -462,3 +462,16 @@ def translate_sections(
if default:
sections.add("default")
return sections
def merge_dictionary(target: dict, input: dict) -> None:
"""Merge the input dict with the target while preserving the existing values
properly. This will update the target dictionary in place.
"""
for key, value in input.items():
if key not in target:
target[key] = value
elif isinstance(value, dict):
target[key].update(value)
elif isinstance(value, list):
target[key].extend(value)
......@@ -7,7 +7,6 @@ from typing import Any, Dict, List, Optional, Tuple, Union
from distlib.wheel import Wheel
from pip._vendor.packaging.requirements import Requirement as PRequirement
from pdm.exceptions import PdmUsageError
from pdm.formats.base import make_array
from pdm.models.candidates import Candidate
from pdm.models.environment import Environment
......@@ -109,12 +108,10 @@ def convert(
reqs = [ireq_as_line(ireq, project.environment) for ireq in ireqs]
deps = make_array(reqs, True)
data = {"dependencies": []}
data = {}
settings = {}
if options.dev and options.section:
raise PdmUsageError("Can't specify --dev and --section at the same time")
elif options.dev:
settings["dev-dependencies"] = {"dev": deps}
if options.dev:
settings["dev-dependencies"] = {options.section or "dev": deps}
elif options.section:
data["optional-dependencies"] = {options.section: deps}
else:
......
......@@ -291,6 +291,17 @@ def test_import_other_format_file(project, invoke, filename):
assert result.exit_code == 0
def test_import_requirement_no_overwrite(project, invoke, tmp_path):
project.add_dependencies({"requests": parse_requirement("requests")})
tmp_path.joinpath("reqs.txt").write_text("flask\nflask-login\n")
result = invoke(
["import", "-dsweb", str(tmp_path.joinpath("reqs.txt"))], obj=project
)
assert result.exit_code == 0, result.stderr
assert list(project.get_dependencies()) == ["requests"]
assert list(project.get_dependencies("web")) == ["flask", "flask-login"]
@pytest.mark.pypi
def test_search_package(project, invoke):
result = invoke(["search", "requests"], obj=project)
......
......@@ -151,5 +151,5 @@ def test_import_requirements_with_section(project):
assert "webassets==2.0" in section
assert 'whoosh==2.7.4; sys_platform == "win32"' in section
assert "-e git+https://github.com/pypa/pip.git@master#egg=pip" in section
assert not result["dependencies"]
assert not result.get("dependencies")
assert not result.get("dev-dependencies", {}).get("dev")
......@@ -5,6 +5,7 @@ import sys
import pytest
from pdm import utils
from pdm.cli import utils as cli_utils
@pytest.mark.parametrize(
......@@ -66,3 +67,21 @@ def test_find_python_in_path(tmp_path):
)
assert not utils.find_python_in_path(tmp_path)
def test_merge_dictionary():
target = {
"existing_dict": {"foo": "bar", "hello": "world"},
"existing_list": ["hello"],
}
input_dict = {
"existing_dict": {"foo": "baz"},
"existing_list": ["world"],
"new_dict": {"name": "Sam"},
}
cli_utils.merge_dictionary(target, input_dict)
assert target == {
"existing_dict": {"foo": "baz", "hello": "world"},
"existing_list": ["hello", "world"],
"new_dict": {"name": "Sam"},
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册