From b0d873094ac308eace868602c324b23556515ba3 Mon Sep 17 00:00:00 2001 From: Lukasz Boldys Date: Wed, 17 Nov 2021 15:37:37 +0100 Subject: [PATCH 1/5] 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. --- tests/test_toml_file.py | 2 +- tomlkit/toml_file.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_toml_file.py b/tests/test_toml_file.py index c7959654..30904cc7 100644 --- a/tests/test_toml_file.py +++ b/tests/test_toml_file.py @@ -21,5 +21,5 @@ def test_toml_file(example): with io.open(toml_file, encoding="utf-8") as f: assert original_content == f.read() finally: - with io.open(toml_file, "w", encoding="utf-8") as f: + with io.open(toml_file, "w", encoding="utf-8", newline="") as f: assert f.write(original_content) diff --git a/tomlkit/toml_file.py b/tomlkit/toml_file.py index 3b416664..154b64fe 100644 --- a/tomlkit/toml_file.py +++ b/tomlkit/toml_file.py @@ -16,9 +16,9 @@ def __init__(self, path): # type: (str) -> None self._path = path def read(self): # type: () -> TOMLDocument - with io.open(self._path, encoding="utf-8") as f: + with io.open(self._path, encoding="utf-8", newline="") as f: return loads(f.read()) def write(self, data): # type: (TOMLDocument) -> None - with io.open(self._path, "w", encoding="utf-8") as f: + with io.open(self._path, "w", encoding="utf-8", newline="") as f: f.write(data.as_string()) From 2aacc5946eedd6e8044461a0db4d1169f605a6eb Mon Sep 17 00:00:00 2001 From: Lukasz Boldys Date: Thu, 18 Nov 2021 11:07:12 +0100 Subject: [PATCH 2/5] Add more tests --- tests/test_toml_file.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/test_toml_file.py b/tests/test_toml_file.py index 30904cc7..4b94298a 100644 --- a/tests/test_toml_file.py +++ b/tests/test_toml_file.py @@ -23,3 +23,43 @@ def test_toml_file(example): finally: with io.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 io.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 io.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 io.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 io.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 io.open(toml_path, "wb+") as f: + f.write(b"a = 1\r\nrb = 2\n") + + f = TOMLFile(toml_path) + f.write(f.read()) + + with io.open(toml_path, "rb") as f: + assert f.read() == b"a = 1\r\nrb = 2\n" From 359e9c41fb49a334754729cecc49fc8a370ffeac Mon Sep 17 00:00:00 2001 From: Lukasz Boldys Date: Thu, 18 Nov 2021 14:19:12 +0100 Subject: [PATCH 3/5] New version of poetry doesn't support this way of installing --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b792e2a7..d3e85bc9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -52,7 +52,7 @@ jobs: shell: bash run: | curl -fsS -o get-poetry.py https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py - python get-poetry.py --preview -y + python get-poetry.py -y echo "$HOME/.poetry/bin" >> $GITHUB_PATH echo "%USERPROFILE%/.poetry/bin" >> $GITHUB_PATH - name: Setup Poetry From 6b016dc0d5e36454be9a90c358380efb1b1a1034 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Fri, 17 Dec 2021 16:41:07 +0800 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From e05a4f17fcf0ec1bedcc8188d584d31616c4e0af Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Fri, 17 Dec 2021 16:44:29 +0800 Subject: [PATCH 5/5] Update test_toml_file.py --- tests/test_toml_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_toml_file.py b/tests/test_toml_file.py index 05b7c6c6..e5b9b525 100644 --- a/tests/test_toml_file.py +++ b/tests/test_toml_file.py @@ -60,5 +60,5 @@ def test_mixed_eol(tmpdir): f = TOMLFile(toml_path) f.write(f.read()) - with io.open(toml_path, "rb") as f: + with open(toml_path, "rb") as f: assert f.read() == b"a = 1\r\nrb = 2\n"