Skip to content

Commit

Permalink
adds @generated to header of lockfile (#2773)
Browse files Browse the repository at this point in the history
Resolves: #2034 

It's useful to both developer tools and for users to recognize the
lock file is generated. This header should achieve both.
  • Loading branch information
vishalkuo committed Sep 18, 2022
1 parent 2def357 commit 3b4ac37
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 38 deletions.
7 changes: 7 additions & 0 deletions src/poetry/packages/locker.py
Expand Up @@ -19,6 +19,7 @@
from poetry.core.version.markers import parse_marker
from poetry.core.version.requirements import InvalidRequirement
from tomlkit import array
from tomlkit import comment
from tomlkit import document
from tomlkit import inline_table
from tomlkit import item
Expand All @@ -38,6 +39,11 @@
from poetry.repositories import Repository

logger = logging.getLogger(__name__)
_GENERATED_IDENTIFIER = "@" + "generated"
GENERATED_COMMENT = (
f"This file is automatically {_GENERATED_IDENTIFIER} by Poetry and should not be"
" changed by hand."
)


class Locker:
Expand Down Expand Up @@ -229,6 +235,7 @@ def set_lock_data(self, root: Package, packages: list[Package]) -> bool:
del package["files"]

lock = document()
lock.add(comment(GENERATED_COMMENT))
lock["package"] = package_specs

if root.extras:
Expand Down
106 changes: 68 additions & 38 deletions tests/packages/test_locker.py
Expand Up @@ -19,6 +19,7 @@
from poetry.core.semver.version import Version

from poetry.factory import Factory
from poetry.packages.locker import GENERATED_COMMENT
from poetry.packages.locker import Locker
from tests.helpers import get_dependency
from tests.helpers import get_package
Expand Down Expand Up @@ -93,7 +94,9 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
with locker.lock.open(encoding="utf-8") as f:
content = f.read()

expected = """\
expected = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "A"
version = "1.0.0"
Expand Down Expand Up @@ -183,21 +186,23 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
[metadata.files]
A = [
{file = "bar", hash = "123"},
{file = "foo", hash = "456"},
{file = "baz", hash = "345"},
{{file = "bar", hash = "123"}},
{{file = "foo", hash = "456"}},
{{file = "baz", hash = "345"}},
]
B = []
git-package = []
git-package-subdir = []
url-package = []
"""
""" # noqa: E800

assert content == expected


def test_locker_properly_loads_extras(locker: Locker):
content = """\
content = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "cachecontrol"
version = "0.12.5"
Expand Down Expand Up @@ -225,7 +230,7 @@ def test_locker_properly_loads_extras(locker: Locker):
[metadata.files]
cachecontrol = []
"""
""" # noqa: E800

locker.lock.write(tomlkit.parse(content))

Expand All @@ -242,7 +247,9 @@ def test_locker_properly_loads_extras(locker: Locker):


def test_locker_properly_loads_nested_extras(locker: Locker):
content = """\
content = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "a"
version = "1.0"
Expand All @@ -252,7 +259,7 @@ def test_locker_properly_loads_nested_extras(locker: Locker):
python-versions = "*"
[package.dependencies]
b = {version = "^1.0", optional = true, extras = "c"}
b = {{version = "^1.0", optional = true, extras = "c"}}
[package.extras]
b = ["b[c] (>=1.0,<2.0)"]
Expand All @@ -266,7 +273,7 @@ def test_locker_properly_loads_nested_extras(locker: Locker):
python-versions = "*"
[package.dependencies]
c = {version = "^1.0", optional = true}
c = {{version = "^1.0", optional = true}}
[package.extras]
c = ["c (>=1.0,<2.0)"]
Expand All @@ -288,7 +295,7 @@ def test_locker_properly_loads_nested_extras(locker: Locker):
"a" = []
"b" = []
"c" = []
"""
""" # noqa: E800

locker.lock.write(tomlkit.parse(content))

Expand Down Expand Up @@ -322,7 +329,9 @@ def test_locker_properly_loads_nested_extras(locker: Locker):


def test_locker_properly_loads_extras_legacy(locker: Locker):
content = """\
content = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "a"
version = "1.0"
Expand All @@ -332,7 +341,7 @@ def test_locker_properly_loads_extras_legacy(locker: Locker):
python-versions = "*"
[package.dependencies]
b = {version = "^1.0", optional = true}
b = {{version = "^1.0", optional = true}}
[package.extras]
b = ["b (^1.0)"]
Expand All @@ -353,7 +362,7 @@ def test_locker_properly_loads_extras_legacy(locker: Locker):
[metadata.files]
"a" = []
"b" = []
"""
""" # noqa: E800

locker.lock.write(tomlkit.parse(content))

Expand Down Expand Up @@ -532,7 +541,10 @@ def test_lock_packages_with_null_description(locker: Locker, root: ProjectPackag
with locker.lock.open(encoding="utf-8") as f:
content = f.read()

expected = """[[package]]
expected = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "A"
version = "1.0.0"
description = ""
Expand All @@ -547,7 +559,7 @@ def test_lock_packages_with_null_description(locker: Locker, root: ProjectPackag
[metadata.files]
A = []
"""
""" # noqa: E800

assert content == expected

Expand All @@ -563,7 +575,10 @@ def test_lock_file_should_not_have_mixed_types(locker: Locker, root: ProjectPack

locker.set_lock_data(root, [package_a])

expected = """[[package]]
expected = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "A"
version = "1.0.0"
description = ""
Expand All @@ -573,8 +588,8 @@ def test_lock_file_should_not_have_mixed_types(locker: Locker, root: ProjectPack
[package.dependencies]
B = [
{version = "^1.0.0"},
{version = ">=1.0.0", optional = true},
{{version = "^1.0.0"}},
{{version = ">=1.0.0", optional = true}},
]
[package.extras]
Expand All @@ -587,7 +602,7 @@ def test_lock_file_should_not_have_mixed_types(locker: Locker, root: ProjectPack
[metadata.files]
A = []
"""
""" # noqa: E800

with locker.lock.open(encoding="utf-8") as f:
content = f.read()
Expand All @@ -596,7 +611,10 @@ def test_lock_file_should_not_have_mixed_types(locker: Locker, root: ProjectPack


def test_reading_lock_file_should_raise_an_error_on_invalid_data(locker: Locker):
content = """[[package]]
content = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "A"
version = "1.0.0"
description = ""
Expand All @@ -617,7 +635,7 @@ def test_reading_lock_file_should_raise_an_error_on_invalid_data(locker: Locker)
[metadata.files]
A = []
"""
""" # noqa: E800
with locker.lock.open("w", encoding="utf-8") as f:
f.write(content)

Expand All @@ -644,7 +662,10 @@ def test_locking_legacy_repository_package_should_include_source_section(
with locker.lock.open(encoding="utf-8") as f:
content = f.read()

expected = """[[package]]
expected = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "A"
version = "1.0.0"
description = ""
Expand All @@ -664,7 +685,7 @@ def test_locking_legacy_repository_package_should_include_source_section(
[metadata.files]
A = []
"""
""" # noqa: E800

assert content == expected

Expand Down Expand Up @@ -703,14 +724,16 @@ def test_locker_should_emit_warnings_if_lock_version_is_newer_but_allowed(
def test_locker_should_raise_an_error_if_lock_version_is_newer_and_not_allowed(
locker: Locker, caplog: LogCaptureFixture
):
content = """\
content = f"""\
# {GENERATED_COMMENT}
[metadata]
lock-version = "2.0"
python-versions = "~2.7 || ^3.4"
content-hash = "c3d07fca33fba542ef2b2a4d75bf5b48d892d21a830e2ad9c952ba5123a52f77"
[metadata.files]
"""
""" # noqa: E800
caplog.set_level(logging.WARNING, logger="poetry.packages.locker")

locker.lock.write(tomlkit.parse(content))
Expand All @@ -730,7 +753,10 @@ def test_extras_dependencies_are_ordered(locker: Locker, root: ProjectPackage):

locker.set_lock_data(root, [package_a])

expected = """[[package]]
expected = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "A"
version = "1.0.0"
description = ""
Expand All @@ -739,7 +765,7 @@ def test_extras_dependencies_are_ordered(locker: Locker, root: ProjectPackage):
python-versions = "*"
[package.dependencies]
B = {version = "^1.0.0", extras = ["a", "b", "c"], optional = true}
B = {{version = "^1.0.0", extras = ["a", "b", "c"], optional = true}}
[metadata]
lock-version = "1.1"
Expand All @@ -748,7 +774,7 @@ def test_extras_dependencies_are_ordered(locker: Locker, root: ProjectPackage):
[metadata.files]
A = []
"""
""" # noqa: E800

with locker.lock.open(encoding="utf-8") as f:
content = f.read()
Expand Down Expand Up @@ -820,7 +846,9 @@ def test_locker_dumps_dependency_information_correctly(
with locker.lock.open(encoding="utf-8") as f:
content = f.read()

expected = """[[package]]
expected = f"""# {GENERATED_COMMENT}
[[package]]
name = "A"
version = "1.0.0"
description = ""
Expand All @@ -829,11 +857,11 @@ def test_locker_dumps_dependency_information_correctly(
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"}
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"
Expand Down Expand Up @@ -940,7 +968,9 @@ def test_locker_dumps_dependency_extras_in_correct_order(
def test_locked_repository_uses_root_dir_of_package(
locker: Locker, mocker: MockerFixture
):
content = """\
content = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "lib-a"
version = "0.1.0"
Expand All @@ -951,7 +981,7 @@ def test_locked_repository_uses_root_dir_of_package(
develop = true
[package.dependencies]
lib-b = {path = "../libB", develop = true}
lib-b = {{path = "../libB", develop = true}}
[package.source]
type = "directory"
Expand All @@ -965,7 +995,7 @@ def test_locked_repository_uses_root_dir_of_package(
[metadata.files]
lib-a = []
lib-b = []
"""
""" # noqa: E800

locker.lock.write(tomlkit.parse(content))
create_dependency_patch = mocker.patch(
Expand Down

0 comments on commit 3b4ac37

Please sign in to comment.