mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 21:04:17 +00:00
Merge pull request #386 from pre-commit/line_endings_string_fixer
Fix crlf line endings for double-quote-string-fixer
This commit is contained in:
commit
24c5f07105
2 changed files with 13 additions and 6 deletions
|
|
@ -31,13 +31,13 @@ def handle_match(token_text): # type: (str) -> str
|
||||||
def get_line_offsets_by_line_no(src): # type: (str) -> List[int]
|
def get_line_offsets_by_line_no(src): # type: (str) -> List[int]
|
||||||
# Padded so we can index with line number
|
# Padded so we can index with line number
|
||||||
offsets = [-1, 0]
|
offsets = [-1, 0]
|
||||||
for line in src.splitlines():
|
for line in src.splitlines(True):
|
||||||
offsets.append(offsets[-1] + len(line) + 1)
|
offsets.append(offsets[-1] + len(line))
|
||||||
return offsets
|
return offsets
|
||||||
|
|
||||||
|
|
||||||
def fix_strings(filename): # type: (str) -> int
|
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()
|
contents = f.read()
|
||||||
line_offsets = get_line_offsets_by_line_no(contents)
|
line_offsets = get_line_offsets_by_line_no(contents)
|
||||||
|
|
||||||
|
|
@ -58,8 +58,8 @@ def fix_strings(filename): # type: (str) -> int
|
||||||
|
|
||||||
new_contents = ''.join(splitcontents)
|
new_contents = ''.join(splitcontents)
|
||||||
if contents != new_contents:
|
if contents != new_contents:
|
||||||
with io.open(filename, 'w', encoding='UTF-8') as write_handle:
|
with io.open(filename, 'w', encoding='UTF-8', newline='') as f:
|
||||||
write_handle.write(new_contents)
|
f.write(new_contents)
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,15 @@ TESTS = (
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS)
|
||||||
def test_rewrite(input_s, output, expected_retval, tmpdir):
|
def test_rewrite(input_s, output, expected_retval, tmpdir):
|
||||||
path = tmpdir.join('file.txt')
|
path = tmpdir.join('file.py')
|
||||||
path.write(input_s)
|
path.write(input_s)
|
||||||
retval = main([path.strpath])
|
retval = main([path.strpath])
|
||||||
assert path.read() == output
|
assert path.read() == output
|
||||||
assert retval == expected_retval
|
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"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue