mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-10 05:14:18 +00:00
Add unit test for mixed_line_ending
This commit is contained in:
parent
f477582ae6
commit
b1294b8614
2 changed files with 36 additions and 1 deletions
|
|
@ -74,7 +74,8 @@ def mixed_line_ending(argv=None):
|
||||||
|
|
||||||
if fix_option == MixedLineEndingOption.NO:
|
if fix_option == MixedLineEndingOption.NO:
|
||||||
logging.info('No conversion asked')
|
logging.info('No conversion asked')
|
||||||
pass
|
|
||||||
|
return 0
|
||||||
elif fix_option == MixedLineEndingOption.AUTO:
|
elif fix_option == MixedLineEndingOption.AUTO:
|
||||||
for filename in options['filenames']:
|
for filename in options['filenames']:
|
||||||
detect_result = _detect_line_ending(filename)
|
detect_result = _detect_line_ending(filename)
|
||||||
|
|
@ -90,12 +91,18 @@ def mixed_line_ending(argv=None):
|
||||||
le_enum.str_print, le_enum.str_print)
|
le_enum.str_print, le_enum.str_print)
|
||||||
|
|
||||||
_convert_line_ending(filename, le_enum.string)
|
_convert_line_ending(filename, le_enum.string)
|
||||||
|
|
||||||
|
return 1
|
||||||
elif detect_result == MixedLineDetection.NOT_MIXED:
|
elif detect_result == MixedLineDetection.NOT_MIXED:
|
||||||
logging.info('The file %s has no mixed line ending', filename)
|
logging.info('The file %s has no mixed line ending', filename)
|
||||||
|
|
||||||
|
return 0
|
||||||
elif detect_result == MixedLineDetection.UNKNOWN:
|
elif detect_result == MixedLineDetection.UNKNOWN:
|
||||||
logging.info('Could not define most frequent line ending in '
|
logging.info('Could not define most frequent line ending in '
|
||||||
'file %s. File skiped.', filename)
|
'file %s. File skiped.', filename)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
# when a line ending character is forced with --fix option
|
# when a line ending character is forced with --fix option
|
||||||
else:
|
else:
|
||||||
line_ending_enum = fix_option.line_ending_enum
|
line_ending_enum = fix_option.line_ending_enum
|
||||||
|
|
@ -105,6 +112,8 @@ def mixed_line_ending(argv=None):
|
||||||
for filename in options['filenames']:
|
for filename in options['filenames']:
|
||||||
_convert_line_ending(filename, line_ending_enum.string)
|
_convert_line_ending(filename, line_ending_enum.string)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
26
tests/mixed_line_ending_test.py
Normal file
26
tests/mixed_line_ending_test.py
Normal 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue