mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 11:36:54 +00:00
Clean up against add-trailing-comma
This commit is contained in:
parent
55658c4bbc
commit
ab2a849052
2 changed files with 66 additions and 34 deletions
|
|
@ -60,15 +60,17 @@ ANY_LINE_ENDING_PATTERN = re.compile(
|
||||||
b'|' + LineEnding.LF.regex.pattern +
|
b'|' + LineEnding.LF.regex.pattern +
|
||||||
# or \r
|
# or \r
|
||||||
b'|' + LineEnding.CR.regex.pattern +
|
b'|' + LineEnding.CR.regex.pattern +
|
||||||
b')'
|
b')',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def mixed_line_ending(argv=None):
|
def mixed_line_ending(argv=None):
|
||||||
options = _parse_arguments(argv)
|
options = _parse_arguments(argv)
|
||||||
|
|
||||||
logging.basicConfig(format='%(levelname)s: %(message)s',
|
logging.basicConfig(
|
||||||
level=options['logging_severity'])
|
format='%(levelname)s: %(message)s',
|
||||||
|
level=options['logging_severity'],
|
||||||
|
)
|
||||||
logging.debug('mixed_line_ending: options = %s', options)
|
logging.debug('mixed_line_ending: options = %s', options)
|
||||||
|
|
||||||
filenames = options['filenames']
|
filenames = options['filenames']
|
||||||
|
|
@ -92,14 +94,16 @@ def _parse_arguments(argv=None):
|
||||||
'--fix',
|
'--fix',
|
||||||
choices=[m.opt_name for m in MixedLineEndingOption],
|
choices=[m.opt_name for m in MixedLineEndingOption],
|
||||||
default=MixedLineEndingOption.AUTO.opt_name,
|
default=MixedLineEndingOption.AUTO.opt_name,
|
||||||
help='Replace line ending with the specified. Default is "auto"')
|
help='Replace line ending with the specified. Default is "auto"',
|
||||||
|
)
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-v',
|
'-v',
|
||||||
'--verbose',
|
'--verbose',
|
||||||
action="count",
|
action="count",
|
||||||
default=0,
|
default=0,
|
||||||
help='Increase output verbosity')
|
help='Increase output verbosity',
|
||||||
|
)
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
fix = None
|
fix = None
|
||||||
|
|
@ -115,8 +119,10 @@ def _parse_arguments(argv=None):
|
||||||
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)
|
||||||
|
|
||||||
options = {'fix': fix, 'filenames': args.filenames,
|
options = {
|
||||||
'logging_severity': severity}
|
'fix': fix, 'filenames': args.filenames,
|
||||||
|
'logging_severity': severity,
|
||||||
|
}
|
||||||
|
|
||||||
return options
|
return options
|
||||||
|
|
||||||
|
|
@ -173,8 +179,10 @@ def _process_no_fix(filenames):
|
||||||
mle_filenames = []
|
mle_filenames = []
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
detect_result = _detect_line_ending(filename)
|
detect_result = _detect_line_ending(filename)
|
||||||
logging.debug('mixed_line_ending: detect_result = %s',
|
logging.debug(
|
||||||
detect_result)
|
'mixed_line_ending: detect_result = %s',
|
||||||
|
detect_result,
|
||||||
|
)
|
||||||
|
|
||||||
if detect_result.mle_found:
|
if detect_result.mle_found:
|
||||||
mle_filenames.append(filename)
|
mle_filenames.append(filename)
|
||||||
|
|
@ -182,8 +190,10 @@ def _process_no_fix(filenames):
|
||||||
mle_found = len(mle_filenames) > 0
|
mle_found = len(mle_filenames) > 0
|
||||||
|
|
||||||
if mle_found:
|
if mle_found:
|
||||||
logging.info('The following files have mixed line endings:\n\t%s',
|
logging.info(
|
||||||
'\n\t'.join(mle_filenames))
|
'The following files have mixed line endings:\n\t%s',
|
||||||
|
'\n\t'.join(mle_filenames),
|
||||||
|
)
|
||||||
|
|
||||||
return 1 if mle_found else 0
|
return 1 if mle_found else 0
|
||||||
|
|
||||||
|
|
@ -194,30 +204,38 @@ def _process_fix_auto(filenames):
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
detect_result = _detect_line_ending(filename)
|
detect_result = _detect_line_ending(filename)
|
||||||
|
|
||||||
logging.debug('mixed_line_ending: detect_result = %s',
|
logging.debug(
|
||||||
detect_result)
|
'mixed_line_ending: detect_result = %s',
|
||||||
|
detect_result,
|
||||||
|
)
|
||||||
|
|
||||||
if detect_result == MixedLineDetection.NOT_MIXED:
|
if 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)
|
||||||
|
|
||||||
mle_found |= False
|
mle_found |= False
|
||||||
elif detect_result == MixedLineDetection.UNKNOWN:
|
elif detect_result == MixedLineDetection.UNKNOWN:
|
||||||
logging.info('Could not define most frequent line ending in '
|
logging.info(
|
||||||
'file %s. File skiped.', filename)
|
'Could not define most frequent line ending in '
|
||||||
|
'file %s. File skiped.', filename,
|
||||||
|
)
|
||||||
|
|
||||||
mle_found = True
|
mle_found = True
|
||||||
else:
|
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(
|
||||||
'majority of "%s". Converting...', filename,
|
'The file %s has mixed line ending with a '
|
||||||
le_enum.str_print)
|
'majority of "%s". Converting...', filename,
|
||||||
|
le_enum.str_print,
|
||||||
|
)
|
||||||
|
|
||||||
_convert_line_ending(filename, le_enum.string)
|
_convert_line_ending(filename, le_enum.string)
|
||||||
mle_found = True
|
mle_found = True
|
||||||
|
|
||||||
logging.info('The file %s has been converted to "%s" line '
|
logging.info(
|
||||||
'ending.', filename, le_enum.str_print)
|
'The file %s has been converted to "%s" line '
|
||||||
|
'ending.', filename, le_enum.str_print,
|
||||||
|
)
|
||||||
|
|
||||||
return 1 if mle_found else 0
|
return 1 if mle_found else 0
|
||||||
|
|
||||||
|
|
@ -226,8 +244,10 @@ def _process_fix_force(filenames, line_ending_enum):
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
_convert_line_ending(filename, line_ending_enum.string)
|
_convert_line_ending(filename, line_ending_enum.string)
|
||||||
|
|
||||||
logging.info('The file %s has been forced to "%s" line ending.',
|
logging.info(
|
||||||
filename, line_ending_enum.str_print)
|
'The file %s has been forced to "%s" line ending.',
|
||||||
|
filename, line_ending_enum.str_print,
|
||||||
|
)
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,10 @@ TESTS_FIX_AUTO = (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'),
|
@pytest.mark.parametrize(
|
||||||
TESTS_FIX_AUTO)
|
('input_s', 'expected_retval', 'output'),
|
||||||
|
TESTS_FIX_AUTO,
|
||||||
|
)
|
||||||
def test_mixed_line_ending_fix_auto(input_s, expected_retval, output, tmpdir):
|
def test_mixed_line_ending_fix_auto(input_s, expected_retval, output, tmpdir):
|
||||||
path = tmpdir.join('file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
path.write(input_s)
|
path.write(input_s)
|
||||||
|
|
@ -61,8 +63,10 @@ TESTS_NO_FIX = (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'),
|
@pytest.mark.parametrize(
|
||||||
TESTS_NO_FIX)
|
('input_s', 'expected_retval', 'output'),
|
||||||
|
TESTS_NO_FIX,
|
||||||
|
)
|
||||||
def test_detect_mixed_line_ending(input_s, expected_retval, output, tmpdir):
|
def test_detect_mixed_line_ending(input_s, expected_retval, output, tmpdir):
|
||||||
path = tmpdir.join('file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
path.write(input_s)
|
path.write(input_s)
|
||||||
|
|
@ -95,10 +99,14 @@ TESTS_FIX_FORCE_LF = (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'),
|
@pytest.mark.parametrize(
|
||||||
TESTS_FIX_FORCE_LF)
|
('input_s', 'expected_retval', 'output'),
|
||||||
def test_mixed_line_ending_fix_force_lf(input_s, expected_retval, output,
|
TESTS_FIX_FORCE_LF,
|
||||||
tmpdir):
|
)
|
||||||
|
def test_mixed_line_ending_fix_force_lf(
|
||||||
|
input_s, expected_retval, output,
|
||||||
|
tmpdir,
|
||||||
|
):
|
||||||
path = tmpdir.join('file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
path.write(input_s)
|
path.write(input_s)
|
||||||
ret = mixed_line_ending(('--fix=lf', '-vv', path.strpath))
|
ret = mixed_line_ending(('--fix=lf', '-vv', path.strpath))
|
||||||
|
|
@ -130,10 +138,14 @@ TESTS_FIX_FORCE_CRLF = (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'),
|
@pytest.mark.parametrize(
|
||||||
TESTS_FIX_FORCE_CRLF)
|
('input_s', 'expected_retval', 'output'),
|
||||||
def test_mixed_line_ending_fix_force_crlf(input_s, expected_retval, output,
|
TESTS_FIX_FORCE_CRLF,
|
||||||
tmpdir):
|
)
|
||||||
|
def test_mixed_line_ending_fix_force_crlf(
|
||||||
|
input_s, expected_retval, output,
|
||||||
|
tmpdir,
|
||||||
|
):
|
||||||
path = tmpdir.join('file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
path.write(input_s)
|
path.write(input_s)
|
||||||
ret = mixed_line_ending(('--fix=crlf', '-vv', path.strpath))
|
ret = mixed_line_ending(('--fix=crlf', '-vv', path.strpath))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue