From a2f836a23b095c19744fb5a73a3584cffa90bf8d Mon Sep 17 00:00:00 2001 From: iconmaster5326 Date: Fri, 25 Oct 2019 11:34:26 -0400 Subject: [PATCH] fix-whitespace: Added test for custom charsets --- pre_commit_hooks/trailing_whitespace_fixer.py | 6 ++++-- tests/trailing_whitespace_fixer_test.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pre_commit_hooks/trailing_whitespace_fixer.py b/pre_commit_hooks/trailing_whitespace_fixer.py index 88b3f93..70ab382 100644 --- a/pre_commit_hooks/trailing_whitespace_fixer.py +++ b/pre_commit_hooks/trailing_whitespace_fixer.py @@ -29,13 +29,15 @@ def _process_line(line, is_markdown, chars_to_strip): # type: (bytes, bool, Optional[bytes]) -> bytes if line[-2:] == b'\r\n': eol = b'\r\n' + line = line[:-2] elif line[-1:] == b'\n': eol = b'\n' + line = line[:-1] else: eol = b'' # preserve trailing two-space for non-blank lines in markdown files - if is_markdown and (not line.isspace()) and line.endswith(b' ' + eol): - return line.rstrip(chars_to_strip) + b' ' + eol + if is_markdown and (not line.isspace()) and line.endswith(b' '): + return line[:-2].rstrip(chars_to_strip) + b' ' + eol return line.rstrip(chars_to_strip) + eol diff --git a/tests/trailing_whitespace_fixer_test.py b/tests/trailing_whitespace_fixer_test.py index 82c9b6d..ec4c918 100644 --- a/tests/trailing_whitespace_fixer_test.py +++ b/tests/trailing_whitespace_fixer_test.py @@ -78,3 +78,19 @@ def test_preserve_non_utf8_file(tmpdir): ret = main([path.strpath]) assert ret == 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