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

adds @generated to header of lockfile #2773

Merged
merged 10 commits into from
Sep 18, 2022
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
7 changes: 7 additions & 0 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -47,6 +48,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."
)
neersighted marked this conversation as resolved.
Show resolved Hide resolved


class Locker:
Expand Down Expand Up @@ -398,6 +404,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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 @@ -81,7 +82,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}
neersighted marked this conversation as resolved.
Show resolved Hide resolved

[[package]]
name = "A"
version = "1.0.0"
Expand Down Expand Up @@ -155,20 +158,22 @@ 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 = []
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 @@ -196,7 +201,7 @@ def test_locker_properly_loads_extras(locker: Locker):

[metadata.files]
cachecontrol = []
"""
""" # noqa: E800

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

Expand All @@ -213,7 +218,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 @@ -223,7 +230,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 @@ -237,7 +244,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}}
neersighted marked this conversation as resolved.
Show resolved Hide resolved

[package.extras]
c = ["c (>=1.0,<2.0)"]
Expand All @@ -259,7 +266,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 @@ -293,7 +300,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 @@ -303,7 +312,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 @@ -324,7 +333,7 @@ def test_locker_properly_loads_extras_legacy(locker: Locker):
[metadata.files]
"a" = []
"b" = []
"""
""" # noqa: E800

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

Expand All @@ -351,7 +360,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 @@ -366,7 +378,7 @@ def test_lock_packages_with_null_description(locker: Locker, root: ProjectPackag

[metadata.files]
A = []
"""
""" # noqa: E800

assert content == expected

Expand All @@ -382,7 +394,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 @@ -392,8 +407,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 @@ -406,7 +421,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 @@ -415,7 +430,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 @@ -436,7 +454,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 @@ -463,7 +481,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 @@ -483,7 +504,7 @@ def test_locking_legacy_repository_package_should_include_source_section(

[metadata.files]
A = []
"""
""" # noqa: E800

assert content == expected

Expand Down Expand Up @@ -522,14 +543,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 @@ -549,7 +572,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 @@ -558,7 +584,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 @@ -567,7 +593,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 @@ -639,7 +665,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 @@ -648,11 +676,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 All @@ -669,7 +697,9 @@ def test_locker_dumps_dependency_information_correctly(
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 @@ -680,7 +710,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 @@ -694,7 +724,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