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

[1.1] Fix locked information for path, url and VCS dependencies #4203

Merged
merged 1 commit into from
Jun 23, 2021
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
27 changes: 24 additions & 3 deletions poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,31 @@ def _lock_packages(
return locked

def _dump_package(self, package): # type: (Package) -> dict
dependencies = {}
dependencies = OrderedDict()
for dependency in sorted(package.requires, key=lambda d: d.name):
if dependency.pretty_name not in dependencies:
dependencies[dependency.pretty_name] = []

constraint = inline_table()
constraint["version"] = str(dependency.pretty_constraint)

if dependency.is_directory() or dependency.is_file():
constraint["path"] = dependency.path.as_posix()

if dependency.is_directory() and dependency.develop:
constraint["develop"] = True
elif dependency.is_url():
constraint["url"] = dependency.url
elif dependency.is_vcs():
constraint[dependency.vcs] = dependency.source

if dependency.branch:
constraint["branch"] = dependency.branch
elif dependency.tag:
constraint["tag"] = dependency.tag
elif dependency.rev:
constraint["rev"] = dependency.rev
else:
constraint["version"] = str(dependency.pretty_constraint)

if dependency.extras:
constraint["extras"] = sorted(dependency.extras)
Expand All @@ -520,7 +538,10 @@ def _dump_package(self, package): # type: (Package) -> dict
# All the constraints should have the same type,
# but we want to simplify them if it's possible
for dependency, constraints in tuple(dependencies.items()):
if all(len(constraint) == 1 for constraint in constraints):
if all(
len(constraint) == 1 and "version" in constraint
for constraint in constraints
):
dependencies[dependency] = [
constraint["version"] for constraint in constraints
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ python-versions = "*"
version = "1.2.3"

[package.dependencies]
project-with-extras = "1.2.3"
project-with-transitive-file-dependencies = "1.2.3"
project-with-extras = { "path" = "../../project_with_extras" }
project-with-transitive-file-dependencies = { "path" = "../project_with_transitive_file_dependencies" }

[package.source]
type = "directory"
Expand All @@ -82,8 +82,8 @@ python-versions = "*"
version = "1.2.3"

[package.dependencies]
demo = "0.1.0"
inner-directory-project = "1.2.4"
demo = { "path" = "../../distributions/demo-0.1.0-py2.py3-none-any.whl" }
inner-directory-project = { "path" = "inner-directory-project" }

[package.source]
type = "directory"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ python-versions = "*"
version = "1.2.3"

[package.dependencies]
demo = "0.1.0"
inner-directory-project = "1.2.4"
demo = { "path" = "../../distributions/demo-0.1.0-py2.py3-none-any.whl" }
inner-directory-project = { "path" = "inner-directory-project" }

[package.source]
type = "directory"
Expand Down
66 changes: 66 additions & 0 deletions tests/packages/test_locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from poetry.core.semver.version import Version
from poetry.factory import Factory
from poetry.packages.locker import Locker
from poetry.utils._compat import Path

from ..helpers import get_dependency
from ..helpers import get_package
Expand Down Expand Up @@ -529,3 +530,68 @@ def test_locker_should_neither_emit_warnings_nor_raise_error_for_lower_compatibl
_ = locker.lock_data

assert 0 == len(caplog.records)


def test_locker_dumps_dependency_information_correctly(locker, root):
root_dir = Path(__file__).parent.parent.joinpath("fixtures")
package_a = get_package("A", "1.0.0")
package_a.add_dependency(
Factory.create_dependency(
"B", {"path": "project_with_extras", "develop": True}, root_dir=root_dir
)
)
package_a.add_dependency(
Factory.create_dependency(
"C",
{"path": "directory/project_with_transitive_directory_dependencies"},
root_dir=root_dir,
)
)
package_a.add_dependency(
Factory.create_dependency(
"D", {"path": "distributions/demo-0.1.0.tar.gz"}, root_dir=root_dir
)
)
package_a.add_dependency(
Factory.create_dependency(
"E", {"url": "https://python-poetry.org/poetry-1.2.0.tar.gz"}
)
)
package_a.add_dependency(
Factory.create_dependency(
"F", {"git": "https://github.com/python-poetry/poetry.git", "branch": "foo"}
)
)

packages = [package_a]

locker.set_lock_data(root, packages)

with locker.lock.open(encoding="utf-8") as f:
content = f.read()

expected = """[[package]]
name = "A"
version = "1.0.0"
description = ""
category = "main"
optional = false
python-versions = "*"

[package.dependencies]
B = {path = "project_with_extras", develop = true}
C = {path = "directory/project_with_transitive_directory_dependencies"}
D = {path = "distributions/demo-0.1.0.tar.gz"}
E = {url = "https://python-poetry.org/poetry-1.2.0.tar.gz"}
F = {git = "https://github.com/python-poetry/poetry.git", branch = "foo"}

[metadata]
lock-version = "1.1"
python-versions = "*"
content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"

[metadata.files]
A = []
"""

assert expected == content