From a1ffbfaa5922281bd38e307392cb5b863bc1fe07 Mon Sep 17 00:00:00 2001 From: Morgan Courbet Date: Tue, 4 Jul 2017 20:50:49 +0200 Subject: [PATCH] Add mixed line detection --- pre_commit_hooks/mixed_line_ending.py | 33 +++++++++++++++++++++++---- tests/mixed_line_ending_test.py | 24 +++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/pre_commit_hooks/mixed_line_ending.py b/pre_commit_hooks/mixed_line_ending.py index b136a8f..c4aa9be 100644 --- a/pre_commit_hooks/mixed_line_ending.py +++ b/pre_commit_hooks/mixed_line_ending.py @@ -68,14 +68,13 @@ def mixed_line_ending(argv=None): level=options['logging_severity']) logging.debug('mixed_line_ending: options = %s', options) - _check_filenames(options['filenames']) - + filenames = options['filenames'] fix_option = options['fix'] - if fix_option == MixedLineEndingOption.NO: - logging.info('No conversion asked') + _check_filenames(filenames) - return 0 + if fix_option == MixedLineEndingOption.NO: + return _process_no_fix(filenames) elif fix_option == MixedLineEndingOption.AUTO: for filename in options['filenames']: detect_result = _detect_line_ending(filename) @@ -188,6 +187,30 @@ def _detect_line_ending(filename): return MixedLineDetection.MIXED_MOSTLY_LF +def _process_no_fix(filenames): + logging.info('Checking if the files have mixed line ending.') + + mle_found = False + mle_filenames = [] + for filename in filenames: + detect_result = _detect_line_ending(filename) + logging.debug('mixed_line_ending: detect_result = %s', + detect_result) + + if detect_result.mle_found: + mle_found = True + mle_filenames.append(filename) + logging.debug(filename) + + logging.debug(mle_found) + logging.debug(str(mle_filenames)) + if mle_filenames: + logging.info('The following files have mixed line endings:\n\t%s', + '\n\t'.join(mle_filenames)) + + return 1 if mle_found else 0 + + def _convert_line_ending(filename, line_ending): # read the file fin = open(filename, 'r') diff --git a/tests/mixed_line_ending_test.py b/tests/mixed_line_ending_test.py index 9de14a5..0b33c92 100644 --- a/tests/mixed_line_ending_test.py +++ b/tests/mixed_line_ending_test.py @@ -24,3 +24,27 @@ def test_mixed_line_ending_fix_auto(input_s, expected_retval, output, tmpdir): assert ret == expected_retval assert path.read() == output + + +# Input, expected return value, expected output +TESTS_NO_FIX = ( + # only 'LF' + (b'foo\nbar\nbaz\n', 0, b'foo\nbar\nbaz\n'), + # only 'CRLF' + (b'foo\r\nbar\r\nbaz\r\n', 0, b'foo\r\nbar\r\nbaz\r\n'), + # mixed with majority of 'LF' + (b'foo\r\nbar\nbaz\n', 1, b'foo\r\nbar\nbaz\n'), + # mixed with majority of 'CRLF' + (b'foo\r\nbar\nbaz\r\n', 1, b'foo\r\nbar\nbaz\r\n'), +) + + +@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), + TESTS_NO_FIX) +def test_detect_mixed_line_ending(input_s, expected_retval, output, tmpdir): + path = tmpdir.join('file.txt') + path.write(input_s) + ret = mixed_line_ending(('--fix=no', '-vv', path.strpath)) + + assert ret == expected_retval + assert path.read() == output