From f3c65bcd2033864d2c4207fe8a45393f449e81fa Mon Sep 17 00:00:00 2001 From: Andy Gimblett Date: Mon, 26 Nov 2018 10:17:52 +0000 Subject: [PATCH 1/3] Report failing filenames when --fix=no in mixed-line-endings --- pre_commit_hooks/mixed_line_ending.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pre_commit_hooks/mixed_line_ending.py b/pre_commit_hooks/mixed_line_ending.py index a163726..cd0c125 100644 --- a/pre_commit_hooks/mixed_line_ending.py +++ b/pre_commit_hooks/mixed_line_ending.py @@ -37,9 +37,15 @@ def fix_filename(filename, fix): # Some amount of mixed line endings mixed = sum(bool(x) for x in counts.values()) > 1 - if fix == 'no' or (fix == 'auto' and not mixed): + if fix == 'no': + if mixed: + # Not fixing, just report problematic file + print('Mixed line endings: {filename}'.format(filename=filename)) return mixed + if fix == 'auto' and not mixed: + return False + if fix == 'auto': max_ending = LF max_lines = 0 From 59ed5120200c68aeeb49e5c6ef3370b957b448e5 Mon Sep 17 00:00:00 2001 From: Andy Gimblett Date: Mon, 26 Nov 2018 17:29:49 +0000 Subject: [PATCH 2/3] In mixed-line-endings, move check/print to main() as suggested by asottile (PR 341) --- pre_commit_hooks/mixed_line_ending.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pre_commit_hooks/mixed_line_ending.py b/pre_commit_hooks/mixed_line_ending.py index cd0c125..e35a65c 100644 --- a/pre_commit_hooks/mixed_line_ending.py +++ b/pre_commit_hooks/mixed_line_ending.py @@ -37,15 +37,9 @@ def fix_filename(filename, fix): # Some amount of mixed line endings mixed = sum(bool(x) for x in counts.values()) > 1 - if fix == 'no': - if mixed: - # Not fixing, just report problematic file - print('Mixed line endings: {filename}'.format(filename=filename)) + if fix == 'no' or (fix == 'auto' and not mixed): return mixed - if fix == 'auto' and not mixed: - return False - if fix == 'auto': max_ending = LF max_lines = 0 @@ -82,7 +76,12 @@ def main(argv=None): retv = 0 for filename in args.filenames: - retv |= fix_filename(filename, args.fix) + if fix_filename(filename, args.fix): + if args.fix == 'no': + print('{}: mixed line endings'.format(filename)) + else: + print('{}: fixed mixed line endings'.format(filename)) + retv = 1 return retv From 1566cf9cca729bc1fe24534cd77d7662cb81a1f6 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 3 Dec 2018 08:34:05 -0800 Subject: [PATCH 3/3] Add test for mixed line endings output --- tests/mixed_line_ending_test.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/mixed_line_ending_test.py b/tests/mixed_line_ending_test.py index 23837cd..8ae9354 100644 --- a/tests/mixed_line_ending_test.py +++ b/tests/mixed_line_ending_test.py @@ -66,16 +66,18 @@ def test_mixed_no_newline_end_of_file(tmpdir): ('--fix=lf', b'foo\nbar\nbaz\n'), ), ) -def test_line_endings_ok(fix_option, input_s, tmpdir): +def test_line_endings_ok(fix_option, input_s, tmpdir, capsys): path = tmpdir.join('input.txt') path.write_binary(input_s) ret = main((fix_option, path.strpath)) assert ret == 0 assert path.read_binary() == input_s + out, _ = capsys.readouterr() + assert out == '' -def test_no_fix_does_not_modify(tmpdir): +def test_no_fix_does_not_modify(tmpdir, capsys): path = tmpdir.join('input.txt') contents = b'foo\r\nbar\rbaz\nwomp\n' path.write_binary(contents) @@ -83,15 +85,19 @@ def test_no_fix_does_not_modify(tmpdir): assert ret == 1 assert path.read_binary() == contents + out, _ = capsys.readouterr() + assert out == '{}: mixed line endings\n'.format(path) -def test_fix_lf(tmpdir): +def test_fix_lf(tmpdir, capsys): path = tmpdir.join('input.txt') path.write_binary(b'foo\r\nbar\rbaz\n') ret = main(('--fix=lf', path.strpath)) assert ret == 1 assert path.read_binary() == b'foo\nbar\nbaz\n' + out, _ = capsys.readouterr() + assert out == '{}: fixed mixed line endings\n'.format(path) def test_fix_crlf(tmpdir):