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

Add poetry version used to generate lock file #7339

Merged
merged 2 commits into from
Jan 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from tomlkit import inline_table
from tomlkit import table

from poetry.__version__ import __version__
from poetry.utils._compat import tomllib


Expand All @@ -40,8 +41,8 @@
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."
f"This file is automatically {_GENERATED_IDENTIFIER} by Poetry"
f" {__version__} and should not be changed by hand."
)


Expand Down
55 changes: 36 additions & 19 deletions tests/packages/test_locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from typing import TYPE_CHECKING

import pytest
import tomlkit

from poetry.core.constraints.version import Version
from poetry.core.packages.package import Package
from poetry.core.packages.project_package import ProjectPackage

from poetry.__version__ import __version__
from poetry.factory import Factory
from poetry.packages.locker import GENERATED_COMMENT
from poetry.packages.locker import Locker
Expand Down Expand Up @@ -231,9 +231,8 @@ def test_locker_properly_loads_extras(locker: Locker):
content-hash = "c3d07fca33fba542ef2b2a4d75bf5b48d892d21a830e2ad9c952ba5123a52f77"
""" # noqa: E800

data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

packages = locker.locked_repository().packages

Expand Down Expand Up @@ -296,9 +295,8 @@ def test_locker_properly_loads_nested_extras(locker: Locker):
content-hash = "123456789"
""" # noqa: E800

data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

repository = locker.locked_repository()
assert len(repository.packages) == 3
Expand Down Expand Up @@ -363,9 +361,8 @@ def test_locker_properly_loads_extras_legacy(locker: Locker):
content-hash = "123456789"
""" # noqa: E800

data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

repository = locker.locked_repository()
assert len(repository.packages) == 2
Expand Down Expand Up @@ -405,9 +402,8 @@ def test_locker_properly_loads_subdir(locker: Locker) -> None:
python-versions = "*"
content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
"""
data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

repository = locker.locked_repository()
assert len(repository.packages) == 1
Expand Down Expand Up @@ -503,9 +499,8 @@ def test_locker_properly_assigns_metadata_files(locker: Locker) -> None:
{file = "demo-1.0-py3-none-any.whl", hash = "sha256"},
]
"""
data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

repository = locker.locked_repository()
assert len(repository.packages) == 5
Expand Down Expand Up @@ -697,9 +692,8 @@ def test_locker_should_emit_warnings_if_lock_version_is_newer_but_allowed(
"""
caplog.set_level(logging.WARNING, logger="poetry.packages.locker")

data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

_ = locker.lock_data

Expand Down Expand Up @@ -729,9 +723,8 @@ def test_locker_should_raise_an_error_if_lock_version_is_newer_and_not_allowed(
""" # noqa: E800
caplog.set_level(logging.WARNING, logger="poetry.packages.locker")

data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

with pytest.raises(RuntimeError, match="^The lock file is not compatible"):
_ = locker.lock_data
Expand Down Expand Up @@ -824,9 +817,8 @@ def test_locker_should_neither_emit_warnings_nor_raise_error_for_lower_compatibl
"""
caplog.set_level(logging.WARNING, logger="poetry.packages.locker")

data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

_ = locker.lock_data

Expand Down Expand Up @@ -1031,9 +1023,8 @@ def test_locked_repository_uses_root_dir_of_package(
content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
""" # noqa: E800

data = tomlkit.parse(content)
with open(locker.lock, "w", encoding="utf-8") as f:
f.write(data.as_string())
f.write(content)

create_dependency_patch = mocker.patch(
"poetry.factory.Factory.create_dependency", autospec=True
Expand Down Expand Up @@ -1179,3 +1170,29 @@ def test_lock_file_resolves_file_url_symlinks(root: ProjectPackage):
""" # noqa: E800

assert content == expected


def test_lockfile_is_not_rewritten_if_only_poetry_version_changed(
locker: Locker, root: ProjectPackage
) -> None:
generated_comment_old_version = GENERATED_COMMENT.replace(__version__, "1.3.2")
assert generated_comment_old_version != GENERATED_COMMENT
old_content = f"""\
# {generated_comment_old_version}
package = []

[metadata]
lock-version = "2.0"
python-versions = "*"
content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
""" # noqa: E800

with open(locker.lock, "w", encoding="utf-8") as f:
f.write(old_content)

assert not locker.set_lock_data(root, [])

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

assert content == old_content