Repository: sdispater/tomlkit
Pull request #149 (2021-11-17 14:39:06)

Prevents TOMLFile from changing line endings
I think that line endings should be preserved and not changed
to system ones when reading and saving the file.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3fd1de5..b9cd85b 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/tomlkit/toml_file.py b/tomlkit/toml_file.py
index 38de220..6fa211a 100644
--- a/tomlkit/toml_file.py
+++ b/tomlkit/toml_file.py
@@ -11,9 +11,9 @@ class TOMLFile:
         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())


Test patch:
diff --git a/tests/test_toml_file.py b/tests/test_toml_file.py
index ca50637..e5b9b52 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"