From b1294b86144f3e95744d681cdc92971dce53c5e9 Mon Sep 17 00:00:00 2001 From: Morgan Courbet Date: Mon, 3 Jul 2017 19:57:43 +0200 Subject: [PATCH] Add unit test for mixed_line_ending --- pre_commit_hooks/mixed_line_ending.py | 11 ++++++++++- tests/mixed_line_ending_test.py | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/mixed_line_ending_test.py diff --git a/pre_commit_hooks/mixed_line_ending.py b/pre_commit_hooks/mixed_line_ending.py index c389898..ddc25eb 100644 --- a/pre_commit_hooks/mixed_line_ending.py +++ b/pre_commit_hooks/mixed_line_ending.py @@ -74,7 +74,8 @@ def mixed_line_ending(argv=None): if fix_option == MixedLineEndingOption.NO: logging.info('No conversion asked') - pass + + return 0 elif fix_option == MixedLineEndingOption.AUTO: for filename in options['filenames']: detect_result = _detect_line_ending(filename) @@ -90,12 +91,18 @@ def mixed_line_ending(argv=None): le_enum.str_print, le_enum.str_print) _convert_line_ending(filename, le_enum.string) + + return 1 elif detect_result == MixedLineDetection.NOT_MIXED: logging.info('The file %s has no mixed line ending', filename) + + return 0 elif detect_result == MixedLineDetection.UNKNOWN: logging.info('Could not define most frequent line ending in ' 'file %s. File skiped.', filename) + return 0 + # when a line ending character is forced with --fix option else: line_ending_enum = fix_option.line_ending_enum @@ -105,6 +112,8 @@ def mixed_line_ending(argv=None): for filename in options['filenames']: _convert_line_ending(filename, line_ending_enum.string) + return 1 + return 0 diff --git a/tests/mixed_line_ending_test.py b/tests/mixed_line_ending_test.py new file mode 100644 index 0000000..9de14a5 --- /dev/null +++ b/tests/mixed_line_ending_test.py @@ -0,0 +1,26 @@ +import pytest + +from pre_commit_hooks.mixed_line_ending import mixed_line_ending + +# Input, expected return value, expected output +TESTS_FIX_AUTO = ( + # 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\nbar\nbaz\n'), + # mixed with majority of 'CRLF' + (b'foo\r\nbar\nbaz\r\n', 1, b'foo\r\nbar\r\nbaz\r\n'), +) + + +@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), + TESTS_FIX_AUTO) +def test_mixed_line_ending_fix_auto(input_s, expected_retval, output, tmpdir): + path = tmpdir.join('file.txt') + path.write(input_s) + ret = mixed_line_ending(('--fix=auto', '-vv', path.strpath)) + + assert ret == expected_retval + assert path.read() == output