Skip to content

Auto removal of trailing whitespace to skip binary file correctly #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions main/githooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
'''

from collections import defaultdict
from io import StringIO
from pathlib import Path
from tempfile import NamedTemporaryFile
from unittest.mock import patch
import os
import platform
import re
Expand Down Expand Up @@ -276,6 +278,7 @@ def check_eol(files):
with open(filename, 'rb') as fileobj:
data = fileobj.read().decode()
except UnicodeDecodeError:
_skip(filename, 'File is not UTF-8 encoded')
continue

# Skip binary file
Expand All @@ -290,8 +293,12 @@ def check_eol(files):

def check_do_not_merge_in_file(filename, new_file=False):
'''Check for "do not merge" in a filename'''
with open(filename, 'rb') as fileobj:
lines = fileobj.read().decode().splitlines(True)
try:
with open(filename, 'rb') as fileobj:
lines = fileobj.read().decode().splitlines(True)
except UnicodeDecodeError:
_skip(filename, 'File is not UTF-8 encoded')
return 0

if new_file:
line_nums = [f'1-{len(lines)}']
Expand Down Expand Up @@ -377,7 +384,8 @@ def trim_trailing_whitespace_in_file(filename, new_file, dry_run,
with open(filename, 'rb') as fileobj:
lines = fileobj.read().decode().splitlines(True)
except UnicodeDecodeError:
return
_skip(filename, 'File is not UTF-8 encoded')
return 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding a test for this case


if new_file:
line_nums = [f'1-{len(lines)}']
Expand All @@ -398,7 +406,7 @@ def trim_trailing_whitespace_in_file(filename, new_file, dry_run,
if dry_run:
modified_lines.append(str(line_num))
else:
print(f' Fixed line {line_num}')
print(f' Fixed line {filename}:{line_num}')
modified_file = True
lines[line_num-1] = after

Expand Down Expand Up @@ -436,6 +444,14 @@ def test_trim_trailing_whitespace(self):
retval = trim_trailing_whitespace_in_file(tmp.name, True, True)
self.assertEqual(retval, 0)

def test_decodeerror(self):
# A text file that is not utf-8 encoded - report and skip
test_file = Path(__file__).parent / '../test/decode_error.txt'
with patch('sys.stdout', new=StringIO()) as tmp_stdout:
retval = trim_trailing_whitespace_in_file(test_file, True, True)
self.assertEqual(retval, 0)
self.assertEqual(tmp_stdout.getvalue().strip(), f'SKIP {test_file}: File is not UTF-8 encoded')


def remove_trailing_white_space(files, new_files=False, dry_run=False):
'''Remove trailing white spaces in all new and modified lines
Expand Down
Binary file added test/decode_error.txt
Binary file not shown.