forked from pre-commit/pre-commit-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathend_of_file_fixer_test.py
More file actions
66 lines (52 loc) · 1.98 KB
/
end_of_file_fixer_test.py
File metadata and controls
66 lines (52 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations
import io
import pytest
from pre_commit_hooks.end_of_file_fixer import fix_file
from pre_commit_hooks.end_of_file_fixer import main
# Input, expected return value, expected output
TESTS = (
(b'foo\n', 0, b'foo\n', None),
(b'', 0, b'', None),
(b'\n\n', 1, b'', None),
(b'\n\n\n\n', 1, b'', None),
(b'foo', 1, b'foo\n', None),
(b'foo\n\n\n', 1, b'foo\n', None),
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83\n', None),
(b'foo\r\n', 0, b'foo\r\n', None),
(b'foo\r\n\r\n\r\n', 1, b'foo\r\n', None),
(b'foo\r', 0, b'foo\r', None),
(b'foo\r\r\r\r', 1, b'foo\r', None),
(b'foo\n', 0, b'foo\n', '--check'),
(b'', 0, b'', '--check'),
(b'\n\n', 1, b'\n\n', '--check'),
(b'\n\n\n\n', 1, b'\n\n\n\n', '--check'),
(b'foo', 1, b'foo', '--check'),
(b'foo\n\n\n', 1, b'foo\n\n\n', '--check'),
(b'\xe2\x98\x83', 1, b'\xe2\x98\x83', '--check'),
(b'foo\r\n', 0, b'foo\r\n', '--check'),
(b'foo\r\n\r\n\r\n', 1, b'foo\r\n\r\n\r\n', '--check'),
(b'foo\r', 0, b'foo\r', '--check'),
(b'foo\r\r\r\r', 1, b'foo\r\r\r\r', '--check'),
)
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output', 'options'), TESTS)
def test_fix_file(input_s, expected_retval, output, options):
if options is None:
options = []
elif isinstance(options, str):
options = [options]
file_obj = io.BytesIO(input_s)
ret = fix_file(file_obj, '--check' in [*options])
assert file_obj.getvalue() == output
assert ret == expected_retval
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output', 'options'), TESTS)
def test_integration(input_s, expected_retval, output, options, tmpdir):
path = tmpdir.join('file.txt')
path.write_binary(input_s)
if options is None:
options = []
elif isinstance(options, str):
options = [options]
ret = main([*options, str(path)])
file_output = path.read_binary()
assert file_output == output
assert ret == expected_retval