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

fix: keep lock file eol #9468

Merged
merged 14 commits into from
Aug 18, 2024
6 changes: 6 additions & 0 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import json
import logging
import os
Expand All @@ -26,6 +27,7 @@
from tomlkit import table

from poetry.__version__ import __version__
from poetry.toml import TOMLError
from poetry.toml.file import TOMLFile
from poetry.utils._compat import tomllib

Expand Down Expand Up @@ -291,6 +293,10 @@ def _should_write(self, lock: TOMLDocument) -> bool:

def _write_lock_data(self, data: TOMLDocument) -> None:
lockfile = TOMLFile(self.lock)
if self.lock.exists():
# get original line ending.
with contextlib.suppress(TOMLError):
lockfile.read()
trim21 marked this conversation as resolved.
Show resolved Hide resolved
lockfile.write(data)

self._lock_data = None
Expand Down
17 changes: 17 additions & 0 deletions tests/packages/test_locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,3 +1231,20 @@ def test_lockfile_is_not_rewritten_if_only_poetry_version_changed(
content = f.read()

assert content == old_content


def test_lockfile_keep_eol(locker: Locker, root: ProjectPackage) -> None:
sep = "\n" if os.sep == "\r\n" else "\r\n"
trim21 marked this conversation as resolved.
Show resolved Hide resolved

with open(locker.lock, "wb") as f:
f.write((sep * 10).encode())

assert locker.set_lock_data(root, [Package("test", version="0.0.1")])

with locker.lock.open(encoding="utf-8", newline="") as f:
line, *_ = f.read().splitlines(keepends=True)

if sep == "\r\n":
assert line.endswith("\r\n")
else:
assert not line.endswith("\r\n")
Loading