Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents TOMLFile from changing line endings #149

Merged
merged 6 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
frostming marked this conversation as resolved.
Show resolved Hide resolved
echo "$HOME/.poetry/bin" >> $GITHUB_PATH
echo "%USERPROFILE%/.poetry/bin" >> $GITHUB_PATH
- name: Setup Poetry
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 @@ -21,5 +21,45 @@ 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)


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