ci(checkstyle): Add no-pwd-capture test

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

View File

@ -30,20 +30,34 @@ class c:
BOLD = '\033[1m' BOLD = '\033[1m'
UNDERLINE = '\033[4m' UNDERLINE = '\033[4m'
def utilGetStrs(line, m):
return (
line[0:m.start('match')],
line[m.start('match'):m.end('match')],
line[m.end('match'):]
)
# Before: printf '%s\\n' '^w^' # Before: printf '%s\\n' '^w^'
# After: printf '%s\n' '^w^' # After: printf '%s\n' '^w^'
def noDoubleBackslashFixer(line: str, rule: Dict[str, str], m: re.Match[str]) -> str: def noDoubleBackslashFixer(line: str, rule: Dict[str, str], m) -> str:
prestr = line[0:m.start('match')] prestr, midstr, poststr = utilGetStrs(line, m)
midstr = line[m.start('match'):m.end('match')]
poststr = line[m.end('match'):]
fixed_line = f'{prestr}{midstr[1:]}{poststr}' return f'{prestr}{midstr[1:]}{poststr}'
return fixed_line
# Before: $(pwd)
# After: $PWD
def noPwdCapture(line: str, rule: Dict[str, str], m) -> str:
prestr, midstr, poststr = utilGetStrs(line, m)
return f'{prestr}$PWD{poststr}'
def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any]): def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any]):
content_arr = filepath.read_text().split('\n') content_arr = filepath.read_text().split('\n')
for line_i, line in enumerate(content_arr): for line_i, line in enumerate(content_arr):
if 'checkstyle-ignore' in line:
continue
for rule in rules: for rule in rules:
m = re.search(rule['regex'], line) m = re.search(rule['regex'], line)
if m is not None and m.group('match') is not None: if m is not None and m.group('match') is not None:
@ -58,7 +72,7 @@ def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any
print() print()
if options['fix']: if options['fix']:
content_arr[line_i] = rule['fixer_fn'](line, rule, m) content_arr[line_i] = rule['fixerFn'](line, rule, m)
rule['found'] += 1 rule['found'] += 1
@ -71,7 +85,7 @@ def main():
'name': 'no-double-backslash', 'name': 'no-double-backslash',
'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, 'fixerFn': noDoubleBackslashFixer,
'testPositiveMatches': [ 'testPositiveMatches': [
'printf "%s\\\\n" "Hai"', 'printf "%s\\\\n" "Hai"',
'echo -n "Hello\\\\n"' 'echo -n "Hello\\\\n"'
@ -82,6 +96,19 @@ def main():
], ],
'found': 0 'found': 0
}, },
{
'name': 'no-pwd-capture',
'regex': '(?P<match>\\$\\(pwd\\))',
'reason': '$PWD is essentially equivalent to $(pwd) without the overhead of a subshell',
'fixerFn': noPwdCapture,
'testPositiveMatches': [
'$(pwd)'
],
'testNegativeMatches': [
'$PWD'
],
'found': 0
},
] ]
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()