From 711b730b6738b04e0fc2adc23c5c4c059b633976 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 16 May 2019 09:42:04 -0700 Subject: [PATCH] Fix crlf line endings for double-quote-string-fixer --- pre_commit_hooks/string_fixer.py | 10 +++++----- tests/string_fixer_test.py | 9 ++++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pre_commit_hooks/string_fixer.py b/pre_commit_hooks/string_fixer.py index a5ea1ea..a41b737 100644 --- a/pre_commit_hooks/string_fixer.py +++ b/pre_commit_hooks/string_fixer.py @@ -31,13 +31,13 @@ def handle_match(token_text): # type: (str) -> str def get_line_offsets_by_line_no(src): # type: (str) -> List[int] # Padded so we can index with line number offsets = [-1, 0] - for line in src.splitlines(): - offsets.append(offsets[-1] + len(line) + 1) + for line in src.splitlines(True): + offsets.append(offsets[-1] + len(line)) return offsets def fix_strings(filename): # type: (str) -> int - with io.open(filename, encoding='UTF-8') as f: + with io.open(filename, encoding='UTF-8', newline='') as f: contents = f.read() line_offsets = get_line_offsets_by_line_no(contents) @@ -58,8 +58,8 @@ def fix_strings(filename): # type: (str) -> int new_contents = ''.join(splitcontents) if contents != new_contents: - with io.open(filename, 'w', encoding='UTF-8') as write_handle: - write_handle.write(new_contents) + with io.open(filename, 'w', encoding='UTF-8', newline='') as f: + f.write(new_contents) return 1 else: return 0 diff --git a/tests/string_fixer_test.py b/tests/string_fixer_test.py index a65213b..4adca4a 100644 --- a/tests/string_fixer_test.py +++ b/tests/string_fixer_test.py @@ -44,8 +44,15 @@ TESTS = ( @pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS) def test_rewrite(input_s, output, expected_retval, tmpdir): - path = tmpdir.join('file.txt') + path = tmpdir.join('file.py') path.write(input_s) retval = main([path.strpath]) assert path.read() == output assert retval == expected_retval + + +def test_rewrite_crlf(tmpdir): + f = tmpdir.join('f.py') + f.write_binary(b'"foo"\r\n"bar"\r\n') + assert main((f.strpath,)) + assert f.read_binary() == b"'foo'\r\n'bar'\r\n"