Add unit test for mixed_line_ending

This commit is contained in:
Morgan Courbet 2017-07-03 19:57:43 +02:00
parent f477582ae6
commit b1294b8614
No known key found for this signature in database
GPG key ID: 467299D324A21B24
2 changed files with 36 additions and 1 deletions

View file

@ -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

View file

@ -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