diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fd1de53..b9cd85b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,8 @@ - Fix incorrect string returned by dumps when moving/renaming table. ([#144](https://github.com/sdispater/tomlkit/issues/144)) - Fix inconsistent dumps when replacing existing item with nested table. ([#145](https://github.com/sdispater/tomlkit/issues/145)) - Fix invalid dumps output when appending to a multiline array. ([#146](https://github.com/sdispater/tomlkit/issues/146)) -- Fix the `KeyAlreadyExistError` when the table is separated into multiple parts. ([#148](https://github.com/sdispater/tomlkit/issues/148)) +- Fix the `KeyAlreadyPresent` when the table is separated into multiple parts. ([#148](https://github.com/sdispater/tomlkit/issues/148)) +- Preserve the line endings in `TOMLFile`. ([#149](https://github.com/sdispater/tomlkit/issues/149)) ## [0.7.2] - 2021-05-20 diff --git a/tests/test_toml_file.py b/tests/test_toml_file.py index ca506370..e5b9b525 100644 --- a/tests/test_toml_file.py +++ b/tests/test_toml_file.py @@ -20,5 +20,45 @@ def test_toml_file(example): with open(toml_file, encoding="utf-8") as f: assert original_content == f.read() finally: - with open(toml_file, "w", encoding="utf-8") as f: + with open(toml_file, "w", encoding="utf-8", newline="") as f: assert f.write(original_content) + + +def test_keep_old_eol(tmpdir): + toml_path = str(tmpdir / "pyproject.toml") + with open(toml_path, "wb+") as f: + f.write(b"a = 1\r\nb = 2\r\n") + + f = TOMLFile(toml_path) + content = f.read() + content["b"] = 3 + f.write(content) + + with open(toml_path, "rb") as f: + assert f.read() == b"a = 1\r\nb = 3\r\n" + + +def test_keep_old_eol_2(tmpdir): + toml_path = str(tmpdir / "pyproject.toml") + with open(toml_path, "wb+") as f: + f.write(b"a = 1\nb = 2\n") + + f = TOMLFile(toml_path) + content = f.read() + content["b"] = 3 + f.write(content) + + with open(toml_path, "rb") as f: + assert f.read() == b"a = 1\nb = 3\n" + + +def test_mixed_eol(tmpdir): + toml_path = str(tmpdir / "pyproject.toml") + with open(toml_path, "wb+") as f: + f.write(b"a = 1\r\nrb = 2\n") + + f = TOMLFile(toml_path) + f.write(f.read()) + + with open(toml_path, "rb") as f: + assert f.read() == b"a = 1\r\nrb = 2\n" diff --git a/tomlkit/toml_file.py b/tomlkit/toml_file.py index 38de2200..6fa211a3 100644 --- a/tomlkit/toml_file.py +++ b/tomlkit/toml_file.py @@ -11,9 +11,9 @@ def __init__(self, path: str) -> None: self._path = path def read(self) -> TOMLDocument: - with open(self._path, encoding="utf-8") as f: + with open(self._path, encoding="utf-8", newline="") as f: return loads(f.read()) def write(self, data: TOMLDocument) -> None: - with open(self._path, "w", encoding="utf-8") as f: + with open(self._path, "w", encoding="utf-8", newline="") as f: f.write(data.as_string())