Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 41 additions & 1 deletion tests/test_toml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions tomlkit/toml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())