Skip to content

Commit

Permalink
Merge pull request python-poetry#149 from utek/dont_modify_newlines
Browse files Browse the repository at this point in the history
Prevents TOMLFile from changing line endings
  • Loading branch information
frostming authored Dec 17, 2021
2 parents cb2ff88 + cd22f55 commit 7badcf3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
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())

0 comments on commit 7badcf3

Please sign in to comment.