mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 20:44:18 +00:00
fix-whitespace: Added test for custom charsets
This commit is contained in:
parent
08663c91f2
commit
a2f836a23b
2 changed files with 20 additions and 2 deletions
|
|
@ -29,13 +29,15 @@ def _process_line(line, is_markdown, chars_to_strip):
|
||||||
# type: (bytes, bool, Optional[bytes]) -> bytes
|
# type: (bytes, bool, Optional[bytes]) -> bytes
|
||||||
if line[-2:] == b'\r\n':
|
if line[-2:] == b'\r\n':
|
||||||
eol = b'\r\n'
|
eol = b'\r\n'
|
||||||
|
line = line[:-2]
|
||||||
elif line[-1:] == b'\n':
|
elif line[-1:] == b'\n':
|
||||||
eol = b'\n'
|
eol = b'\n'
|
||||||
|
line = line[:-1]
|
||||||
else:
|
else:
|
||||||
eol = b''
|
eol = b''
|
||||||
# preserve trailing two-space for non-blank lines in markdown files
|
# preserve trailing two-space for non-blank lines in markdown files
|
||||||
if is_markdown and (not line.isspace()) and line.endswith(b' ' + eol):
|
if is_markdown and (not line.isspace()) and line.endswith(b' '):
|
||||||
return line.rstrip(chars_to_strip) + b' ' + eol
|
return line[:-2].rstrip(chars_to_strip) + b' ' + eol
|
||||||
return line.rstrip(chars_to_strip) + eol
|
return line.rstrip(chars_to_strip) + eol
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,3 +78,19 @@ def test_preserve_non_utf8_file(tmpdir):
|
||||||
ret = main([path.strpath])
|
ret = main([path.strpath])
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert path.size() == (len(non_utf8_bytes_content) - 1)
|
assert path.size() == (len(non_utf8_bytes_content) - 1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_charset_change(tmpdir):
|
||||||
|
# strip spaces only, no tabs
|
||||||
|
path = tmpdir.join('file.txt')
|
||||||
|
path.write('\ta \t \n')
|
||||||
|
ret = main([path.strpath, '--chars', ' '])
|
||||||
|
assert ret == 1
|
||||||
|
assert path.read() == '\ta \t\n'
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_charset_no_change(tmpdir):
|
||||||
|
path = tmpdir.join('file.txt')
|
||||||
|
path.write('\ta \t\n')
|
||||||
|
ret = main([path.strpath, '--chars', ' '])
|
||||||
|
assert ret == 0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue