Skip to content

Commit

Permalink
Do not convert Windows line endings when reading the input file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Reichelt Gjertsen authored and ikamensh committed Mar 23, 2021
1 parent bd4d246 commit 5d422d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/flynt/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _fstringify_file(
def default_result():
return False, 0, len(contents), len(contents)

with open(filename, encoding="utf-8") as f:
with open(filename, encoding="utf-8", newline="") as f:
try:
contents = f.read()
except UnicodeDecodeError as e:
Expand Down
23 changes: 23 additions & 0 deletions test/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from flynt import state
from flynt.api import _fstringify_file

# These "files" are byte-string constants instead of actual files to prevent e.g. Git or text editors from accidentally changing the encoding
invalid_unicode = b"# This is not valid unicode: " + bytes([0xff, 0xff])
mixed_line_endings_before = b"'{}'.format(1)\n'{}'.format(2)# Linux line ending\n'{}'.format(3)# Windows line ending\r\n"
mixed_line_endings_after = b"f'{1}'\nf'{2}'# Linux line ending\nf'{3}'# Windows line ending\r\n"

@pytest.fixture()
def formattable_file(tmpdir):
Expand Down Expand Up @@ -40,6 +43,16 @@ def invalid_unicode_file(tmpdir):
yield tmp_path


@pytest.fixture()
def mixed_line_endings_file(tmpdir):
folder = os.path.dirname(__file__)
tmp_path = os.path.join(tmpdir, "mixed_line_endings.py")
with open(tmp_path, "wb") as file:
file.write(mixed_line_endings_before)

yield tmp_path


def test_py2(py2_file):

with open(py2_file) as f:
Expand Down Expand Up @@ -127,3 +140,13 @@ def test_dry_run(formattable_file, monkeypatch):

assert modified
assert content_after == content_before


def test_mixed_line_endings(mixed_line_endings_file):
modified, _, _, _ = _fstringify_file(mixed_line_endings_file, True, 1000)

with open(mixed_line_endings_file, "rb") as f:
content_after = f.read()

assert modified
assert content_after == mixed_line_endings_after

0 comments on commit 5d422d1

Please sign in to comment.