Skip to content

Commit 3b8b26a

Browse files
author
zhiwei.meng
committed
Add "--check" option support to end_of_file_fixer hook
1 parent f1dff44 commit 3b8b26a

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

pre_commit_hooks/end_of_file_fixer.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import IO
77

88

9-
def fix_file(file_obj: IO[bytes]) -> int:
9+
def fix_file(file_obj: IO[bytes], check_only=False) -> int:
1010
# Test for newline at end of file
1111
# Empty files will throw IOError here
1212
try:
@@ -18,7 +18,8 @@ def fix_file(file_obj: IO[bytes]) -> int:
1818
if last_character not in {b'\n', b'\r'} and last_character != b'':
1919
# Needs this seek for windows, otherwise IOError
2020
file_obj.seek(0, os.SEEK_END)
21-
file_obj.write(b'\n')
21+
if not check_only:
22+
file_obj.write(b'\n')
2223
return 1
2324

2425
while last_character in {b'\n', b'\r'}:
@@ -27,7 +28,8 @@ def fix_file(file_obj: IO[bytes]) -> int:
2728
# If we've reached the beginning of the file and it is all
2829
# linebreaks then we can make this file empty
2930
file_obj.seek(0)
30-
file_obj.truncate()
31+
if not check_only:
32+
file_obj.truncate()
3133
return 1
3234

3335
# Go back two bytes and read a character
@@ -43,14 +45,16 @@ def fix_file(file_obj: IO[bytes]) -> int:
4345
return 0
4446
elif remaining.startswith(sequence):
4547
file_obj.seek(position + len(sequence))
46-
file_obj.truncate()
48+
if not check_only:
49+
file_obj.truncate()
4750
return 1
4851

4952
return 0
5053

5154

5255
def main(argv: Sequence[str] | None = None) -> int:
5356
parser = argparse.ArgumentParser()
57+
parser.add_argument('--check', action='store_true', help='Check without fixing')
5458
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
5559
args = parser.parse_args(argv)
5660

@@ -59,11 +63,13 @@ def main(argv: Sequence[str] | None = None) -> int:
5963
for filename in args.filenames:
6064
# Read as binary so we can read byte-by-byte
6165
with open(filename, 'rb+') as file_obj:
62-
ret_for_file = fix_file(file_obj)
66+
ret_for_file = fix_file(file_obj, args.check)
6367
if ret_for_file:
64-
print(f'Fixing {filename}')
68+
if args.check:
69+
print(f'Wrong ending of file: {filename}')
70+
else:
71+
print(f'Fixing {filename}')
6572
retv |= ret_for_file
66-
6773
return retv
6874

6975

tests/end_of_file_fixer_test.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,56 @@
1010

1111
# Input, expected return value, expected output
1212
TESTS = (
13-
(b'foo\n', 0, b'foo\n'),
14-
(b'', 0, b''),
15-
(b'\n\n', 1, b''),
16-
(b'\n\n\n\n', 1, b''),
17-
(b'foo', 1, b'foo\n'),
18-
(b'foo\n\n\n', 1, b'foo\n'),
19-
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n'),
20-
(b'foo\r\n', 0, b'foo\r\n'),
21-
(b'foo\r\n\r\n\r\n', 1, b'foo\r\n'),
22-
(b'foo\r', 0, b'foo\r'),
23-
(b'foo\r\r\r\r', 1, b'foo\r'),
13+
(b'foo\n', 0, b'foo\n', None),
14+
(b'', 0, b'', None),
15+
(b'\n\n', 1, b'', None),
16+
(b'\n\n\n\n', 1, b'', None),
17+
(b'foo', 1, b'foo\n', None),
18+
(b'foo\n\n\n', 1, b'foo\n', None),
19+
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n', None),
20+
(b'foo\r\n', 0, b'foo\r\n', None),
21+
(b'foo\r\n\r\n\r\n', 1, b'foo\r\n', None),
22+
(b'foo\r', 0, b'foo\r', None),
23+
(b'foo\r\r\r\r', 1, b'foo\r', None),
24+
25+
(b'foo\n', 0, b'foo\n', '--check'),
26+
(b'', 0, b'', '--check'),
27+
(b'\n\n', 1, b'\n\n', '--check'),
28+
(b'\n\n\n\n', 1, b'\n\n\n\n', '--check'),
29+
(b'foo', 1, b'foo', '--check'),
30+
(b'foo\n\n\n', 1, b'foo\n\n\n', '--check'),
31+
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83', '--check'),
32+
(b'foo\r\n', 0, b'foo\r\n', '--check'),
33+
(b'foo\r\n\r\n\r\n', 1, b'foo\r\n\r\n\r\n', '--check'),
34+
(b'foo\r', 0, b'foo\r', '--check'),
35+
(b'foo\r\r\r\r', 1, b'foo\r\r\r\r', '--check'),
2436
)
2537

2638

27-
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
28-
def test_fix_file(input_s, expected_retval, output):
39+
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output', 'options'), TESTS)
40+
def test_fix_file(input_s, expected_retval, output, options):
41+
if options is None:
42+
options = []
43+
elif isinstance(options, str):
44+
options = [options]
45+
2946
file_obj = io.BytesIO(input_s)
30-
ret = fix_file(file_obj)
47+
ret = fix_file(file_obj, "--check" in [*options])
3148
assert file_obj.getvalue() == output
3249
assert ret == expected_retval
3350

3451

35-
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
36-
def test_integration(input_s, expected_retval, output, tmpdir):
52+
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output', 'options'), TESTS)
53+
def test_integration(input_s, expected_retval, output, options, tmpdir):
3754
path = tmpdir.join('file.txt')
3855
path.write_binary(input_s)
3956

40-
ret = main([str(path)])
57+
if options is None:
58+
options = []
59+
elif isinstance(options, str):
60+
options = [options]
61+
62+
ret = main([*options, str(path)])
4163
file_output = path.read_binary()
4264

4365
assert file_output == output

0 commit comments

Comments
 (0)