Improve test coverage

This commit is contained in:
Morgan Courbet 2017-07-04 22:24:24 +02:00
parent 63bb1fd1f5
commit 3dbeeeefe5
No known key found for this signature in database
GPG key ID: 467299D324A21B24
2 changed files with 30 additions and 24 deletions

View file

@ -31,12 +31,14 @@ class MixedLineEndingOption(Enum):
class MixedLineDetection(Enum): class MixedLineDetection(Enum):
MIXED_MOSTLY_CRLF = True, LineEnding.CRLF MIXED_MOSTLY_CRLF = 1, True, LineEnding.CRLF
MIXED_MOSTLY_LF = True, LineEnding.LF MIXED_MOSTLY_LF = 2, True, LineEnding.LF
NOT_MIXED = False, None NOT_MIXED = 3, False, None
UNKNOWN = False, None UNKNOWN = 4, False, None
def __init__(self, mle_found, line_ending_enum): def __init__(self, index, mle_found, line_ending_enum):
# TODO hack to prevent enum overriding
self.index = index
self.mle_found = mle_found self.mle_found = mle_found
self.line_ending_enum = line_ending_enum self.line_ending_enum = line_ending_enum
@ -81,8 +83,6 @@ def mixed_line_ending(argv=None):
else: else:
return _process_fix_force(filenames, fix_option.line_ending_enum) return _process_fix_force(filenames, fix_option.line_ending_enum)
return 0
def _parse_arguments(argv=None): def _parse_arguments(argv=None):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -102,14 +102,14 @@ def _parse_arguments(argv=None):
args = parser.parse_args(argv) args = parser.parse_args(argv)
fix = None fix = None
if args.fix == MixedLineEndingOption.AUTO.opt_name: if args.fix == MixedLineEndingOption.NO.opt_name:
fix = MixedLineEndingOption.AUTO
elif args.fix == MixedLineEndingOption.NO.opt_name:
fix = MixedLineEndingOption.NO fix = MixedLineEndingOption.NO
elif args.fix == MixedLineEndingOption.CRLF.opt_name: elif args.fix == MixedLineEndingOption.CRLF.opt_name:
fix = MixedLineEndingOption.CRLF fix = MixedLineEndingOption.CRLF
elif args.fix == MixedLineEndingOption.LF.opt_name: elif args.fix == MixedLineEndingOption.LF.opt_name:
fix = MixedLineEndingOption.LF fix = MixedLineEndingOption.LF
else:
fix = MixedLineEndingOption.AUTO
args.verbose = min(args.verbose, 2) args.verbose = min(args.verbose, 2)
severity = VERBOSE_OPTION_TO_LOGGING_SEVERITY.get(args.verbose) severity = VERBOSE_OPTION_TO_LOGGING_SEVERITY.get(args.verbose)
@ -180,7 +180,7 @@ def _process_no_fix(filenames):
def _process_fix_auto(filenames): def _process_fix_auto(filenames):
converted_found = False mle_found = False
for filename in filenames: for filename in filenames:
detect_result = _detect_line_ending(filename) detect_result = _detect_line_ending(filename)
@ -188,7 +188,16 @@ def _process_fix_auto(filenames):
logging.debug('mixed_line_ending: detect_result = %s', logging.debug('mixed_line_ending: detect_result = %s',
detect_result) detect_result)
if detect_result.mle_found: if detect_result == MixedLineDetection.NOT_MIXED:
logging.info('The file %s has no mixed line ending', filename)
mle_found |= False
elif detect_result == MixedLineDetection.UNKNOWN:
logging.info('Could not define most frequent line ending in '
'file %s. File skiped.', filename)
mle_found = True
else:
le_enum = detect_result.line_ending_enum le_enum = detect_result.line_ending_enum
logging.info('The file %s has mixed line ending with a ' logging.info('The file %s has mixed line ending with a '
@ -196,22 +205,12 @@ def _process_fix_auto(filenames):
le_enum.str_print) le_enum.str_print)
_convert_line_ending(filename, le_enum.string) _convert_line_ending(filename, le_enum.string)
converted_found = True mle_found = True
logging.info('The file %s has been converted to "%s" line ' logging.info('The file %s has been converted to "%s" line '
'ending.', filename, le_enum.str_print) 'ending.', filename, le_enum.str_print)
elif detect_result == MixedLineDetection.NOT_MIXED: return 1 if mle_found else 0
logging.info('The file %s has no mixed line ending', filename)
converted_found |= False
elif detect_result == MixedLineDetection.UNKNOWN:
logging.info('Could not define most frequent line ending in '
'file %s. File skiped.', filename)
converted_found |= False
return 1 if converted_found else 0
def _process_fix_force(filenames, line_ending_enum): def _process_fix_force(filenames, line_ending_enum):

View file

@ -12,6 +12,8 @@ TESTS_FIX_AUTO = (
(b'foo\r\nbar\nbaz\n', 1, b'foo\nbar\nbaz\n'), (b'foo\r\nbar\nbaz\n', 1, b'foo\nbar\nbaz\n'),
# mixed with majority of 'CRLF' # mixed with majority of 'CRLF'
(b'foo\r\nbar\nbaz\r\n', 1, b'foo\r\nbar\r\nbaz\r\n'), (b'foo\r\nbar\nbaz\r\n', 1, b'foo\r\nbar\r\nbaz\r\n'),
# mixed with as much 'LF' as 'CRLF'
(b'foo\r\nbar\nbaz', 1, b'foo\r\nbar\nbaz'),
) )
@ -98,3 +100,8 @@ def test_mixed_line_ending_fix_force_crlf(input_s, expected_retval, output,
assert ret == expected_retval assert ret == expected_retval
assert path.read() == output assert path.read() == output
def test_check_filenames():
with pytest.raises(IOError):
mixed_line_ending(['/dev/null'])