ci: Add regex testing ability to checkstyle.py

This commit is contained in:
Edwin Kofler 2022-12-27 05:19:30 -08:00
parent 3492043241
commit d63996c4b0
No known key found for this signature in database
GPG Key ID: A1E60C1F1A423B08

View File

@ -72,6 +72,14 @@ def main():
'regex': '".*?(?P<match>\\\\\\\\[abeEfnrtv\'"?xuUc]).*?(?<!\\\\)"', 'regex': '".*?(?P<match>\\\\\\\\[abeEfnrtv\'"?xuUc]).*?(?<!\\\\)"',
'reason': 'Backslashes are only required if followed by a $, `, ", \\, or <newline>', 'reason': 'Backslashes are only required if followed by a $, `, ", \\, or <newline>',
'fixer_fn': noDoubleBackslashFixer, 'fixer_fn': noDoubleBackslashFixer,
'testPositiveMatches': [
'printf "%s\\\\n" "Hai"',
'echo -n "Hello\\\\n"'
],
'testNegativeMatches': [
'printf "%s\\n" "Hai"',
'echo -n "Hello\\n"'
],
'found': 0 'found': 0
}, },
] ]
@ -79,8 +87,26 @@ def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('files', metavar='FILES', nargs='*') parser.add_argument('files', metavar='FILES', nargs='*')
parser.add_argument('--fix', action='store_true') parser.add_argument('--fix', action='store_true')
parser.add_argument('--internal-test-regex', action='store_true')
args = parser.parse_args() args = parser.parse_args()
if args.internal_test_regex:
for rule in rules:
for positiveMatch in rule['testPositiveMatches']:
m = re.search(rule['regex'], positiveMatch)
if m is None or m.group('match') is None:
print(f'{c.MAGENTA}{rule["name"]}{c.RESET}: Failed {c.CYAN}positive{c.RESET} test:')
print(f'=> {positiveMatch}')
print()
for negativeMatch in rule['testNegativeMatches']:
m = re.search(rule['regex'], negativeMatch)
if m is not None and m.group('match') is not None:
print(f'{c.MAGENTA}{rule["name"]}{c.RESET}: Failed {c.YELLOW}negative{c.RESET} test:')
print(f'=> {negativeMatch}')
print()
return
options = { options = {
'fix': args.fix 'fix': args.fix
} }