Skip to content

Commit 0c76025

Browse files
committed
Add a way to do case-insensitive sorting via file-contents-sorter.
1 parent e1668fe commit 0c76025

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

pre_commit_hooks/file_contents_sorter.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
conflicts and keep the file nicely ordered.
1111
"""
1212
import argparse
13+
from typing import Any
14+
from typing import Callable
1315
from typing import IO
1416
from typing import Optional
1517
from typing import Sequence
@@ -18,9 +20,15 @@
1820
FAIL = 1
1921

2022

21-
def sort_file_contents(f: IO[bytes]) -> int:
23+
def sort_file_contents(
24+
f: IO[bytes],
25+
key: Optional[Callable[[bytes], Any]],
26+
) -> int:
2227
before = list(f)
23-
after = sorted(line.strip(b'\n\r') for line in before if line.strip())
28+
after = sorted(
29+
(line.strip(b'\n\r') for line in before if line.strip()),
30+
key=key,
31+
)
2432

2533
before_string = b''.join(before)
2634
after_string = b'\n'.join(after) + b'\n'
@@ -37,13 +45,20 @@ def sort_file_contents(f: IO[bytes]) -> int:
3745
def main(argv: Optional[Sequence[str]] = None) -> int:
3846
parser = argparse.ArgumentParser()
3947
parser.add_argument('filenames', nargs='+', help='Files to sort')
48+
parser.add_argument(
49+
'--ignore-case',
50+
action='store_const',
51+
const=bytes.lower,
52+
default=None,
53+
help='fold lower case to upper case characters',
54+
)
4055
args = parser.parse_args(argv)
4156

4257
retv = PASS
4358

4459
for arg in args.filenames:
4560
with open(arg, 'rb+') as file_obj:
46-
ret_for_file = sort_file_contents(file_obj)
61+
ret_for_file = sort_file_contents(file_obj, key=args.ignore_case)
4762

4863
if ret_for_file:
4964
print(f'Sorting {arg}')

tests/file_contents_sorter_test.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,52 @@
66

77

88
@pytest.mark.parametrize(
9-
('input_s', 'expected_retval', 'output'),
9+
('input_s', 'argv', 'expected_retval', 'output'),
1010
(
11-
(b'', FAIL, b'\n'),
12-
(b'lonesome\n', PASS, b'lonesome\n'),
13-
(b'missing_newline', FAIL, b'missing_newline\n'),
14-
(b'newline\nmissing', FAIL, b'missing\nnewline\n'),
15-
(b'missing\nnewline', FAIL, b'missing\nnewline\n'),
16-
(b'alpha\nbeta\n', PASS, b'alpha\nbeta\n'),
17-
(b'beta\nalpha\n', FAIL, b'alpha\nbeta\n'),
18-
(b'C\nc\n', PASS, b'C\nc\n'),
19-
(b'c\nC\n', FAIL, b'C\nc\n'),
20-
(b'mag ical \n tre vor\n', FAIL, b' tre vor\nmag ical \n'),
21-
(b'@\n-\n_\n#\n', FAIL, b'#\n-\n@\n_\n'),
22-
(b'extra\n\n\nwhitespace\n', FAIL, b'extra\nwhitespace\n'),
23-
(b'whitespace\n\n\nextra\n', FAIL, b'extra\nwhitespace\n'),
11+
(b'', [], FAIL, b'\n'),
12+
(b'lonesome\n', [], PASS, b'lonesome\n'),
13+
(b'missing_newline', [], FAIL, b'missing_newline\n'),
14+
(b'newline\nmissing', [], FAIL, b'missing\nnewline\n'),
15+
(b'missing\nnewline', [], FAIL, b'missing\nnewline\n'),
16+
(b'alpha\nbeta\n', [], PASS, b'alpha\nbeta\n'),
17+
(b'beta\nalpha\n', [], FAIL, b'alpha\nbeta\n'),
18+
(b'C\nc\n', [], PASS, b'C\nc\n'),
19+
(b'c\nC\n', [], FAIL, b'C\nc\n'),
20+
(b'mag ical \n tre vor\n', [], FAIL, b' tre vor\nmag ical \n'),
21+
(b'@\n-\n_\n#\n', [], FAIL, b'#\n-\n@\n_\n'),
22+
(b'extra\n\n\nwhitespace\n', [], FAIL, b'extra\nwhitespace\n'),
23+
(b'whitespace\n\n\nextra\n', [], FAIL, b'extra\nwhitespace\n'),
24+
(
25+
b'fee\nFie\nFoe\nfum\n',
26+
[],
27+
FAIL,
28+
b'Fie\nFoe\nfee\nfum\n',
29+
),
30+
(
31+
b'Fie\nFoe\nfee\nfum\n',
32+
[],
33+
PASS,
34+
b'Fie\nFoe\nfee\nfum\n',
35+
),
36+
(
37+
b'fee\nFie\nFoe\nfum\n',
38+
['--ignore-case'],
39+
PASS,
40+
b'fee\nFie\nFoe\nfum\n',
41+
),
42+
(
43+
b'Fie\nFoe\nfee\nfum\n',
44+
['--ignore-case'],
45+
FAIL,
46+
b'fee\nFie\nFoe\nfum\n',
47+
),
2448
),
2549
)
26-
def test_integration(input_s, expected_retval, output, tmpdir):
50+
def test_integration(input_s, argv, expected_retval, output, tmpdir):
2751
path = tmpdir.join('file.txt')
2852
path.write_binary(input_s)
2953

30-
output_retval = main([str(path)])
54+
output_retval = main([str(path)] + argv)
3155

3256
assert path.read_binary() == output
3357
assert output_retval == expected_retval

0 commit comments

Comments
 (0)