Skip to content

Commit a2f836a

Browse files
fix-whitespace: Added test for custom charsets
1 parent 08663c9 commit a2f836a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pre_commit_hooks/trailing_whitespace_fixer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ def _process_line(line, is_markdown, chars_to_strip):
2929
# type: (bytes, bool, Optional[bytes]) -> bytes
3030
if line[-2:] == b'\r\n':
3131
eol = b'\r\n'
32+
line = line[:-2]
3233
elif line[-1:] == b'\n':
3334
eol = b'\n'
35+
line = line[:-1]
3436
else:
3537
eol = b''
3638
# preserve trailing two-space for non-blank lines in markdown files
37-
if is_markdown and (not line.isspace()) and line.endswith(b' ' + eol):
38-
return line.rstrip(chars_to_strip) + b' ' + eol
39+
if is_markdown and (not line.isspace()) and line.endswith(b' '):
40+
return line[:-2].rstrip(chars_to_strip) + b' ' + eol
3941
return line.rstrip(chars_to_strip) + eol
4042

4143

tests/trailing_whitespace_fixer_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,19 @@ def test_preserve_non_utf8_file(tmpdir):
7878
ret = main([path.strpath])
7979
assert ret == 1
8080
assert path.size() == (len(non_utf8_bytes_content) - 1)
81+
82+
83+
def test_custom_charset_change(tmpdir):
84+
# strip spaces only, no tabs
85+
path = tmpdir.join('file.txt')
86+
path.write('\ta \t \n')
87+
ret = main([path.strpath, '--chars', ' '])
88+
assert ret == 1
89+
assert path.read() == '\ta \t\n'
90+
91+
92+
def test_custom_charset_no_change(tmpdir):
93+
path = tmpdir.join('file.txt')
94+
path.write('\ta \t\n')
95+
ret = main([path.strpath, '--chars', ' '])
96+
assert ret == 0

0 commit comments

Comments
 (0)