Kaydet (Commit) a5deaba9 authored tarafından Evan Hubinger's avatar Evan Hubinger Kaydeden (comit) Anthony Sottile

Support Windows line endings (#33)

* Support Windows line endings

Previously, since the file was opened as bytes but written as text, \r\n
got written as \r\r\n, since it read in \r\n as bytes, passed through
the \r, then wrote out \r\n as text, which automatically transforms \n
into \r\n on Windows, resulting in \r\r\n. This commit fixes that.

* Improve line ending handling

* Add appveyor config

* Remove all newline translation

* Remove newline arg from binary open

* Add regression test and make requested change
üst 11440cff
......@@ -376,7 +376,7 @@ def fix_file(filename, args):
if contents_text != contents_text_orig:
print('Rewriting {}'.format(filename))
with io.open(filename, 'w', encoding='UTF-8') as f:
with io.open(filename, 'w', newline='', encoding='UTF-8') as f:
f.write(contents_text)
return 1
......
environment:
matrix:
- TOXENV: py27
- TOXENV: py36
install:
- "SET PATH=C:\\Python36;C:\\Python36\\Scripts;%PATH%"
- pip install tox
# Not a C# project
build: false
test_script: tox
cache:
- '%LOCALAPPDATA%\pip\cache'
- '%USERPROFILE%\.pre-commit'
......@@ -704,6 +704,15 @@ def test_main_changes_a_file(tmpdir, capsys):
assert f.read() == 'x(\n 1,\n)\n'
def test_main_preserves_line_endings(tmpdir, capsys):
f = tmpdir.join('f.py')
f.write_binary(b'x(\r\n 1\r\n)\r\n')
assert main((f.strpath,)) == 1
out, _ = capsys.readouterr()
assert out == 'Rewriting {}\n'.format(f.strpath)
assert f.read_binary() == b'x(\r\n 1,\r\n)\r\n'
def test_main_syntax_error(tmpdir):
f = tmpdir.join('f.py')
f.write('from __future__ import print_function\nprint 1\n')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment