mirror of https://github.com/ansible/ansible.git
Add support for enhanced code-smell tests. (#36332)
* Add support for enhanced code-smell tests: - Path selection handled by ansible-test. - Optional path filtering based on extension. - Optional path filtering based on prefixes. - Optional lint friendly output. * Enhance no-assert code-smell test. * Enhance no-tests-as-filters code-smell test.pull/36472/head
parent
ce80485ecd
commit
2b6ac4561b
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"extensions": [
|
||||||
|
".py"
|
||||||
|
],
|
||||||
|
"prefixes": [
|
||||||
|
"bin/",
|
||||||
|
"lib/ansible/"
|
||||||
|
],
|
||||||
|
"output": "path-line-column-message"
|
||||||
|
}
|
||||||
@ -1,40 +1,29 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import os
|
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
PATH = 'lib/ansible'
|
|
||||||
ASSERT_RE = re.compile(r'.*(?<![-:a-zA-Z#][ -])\bassert\b(?!:).*')
|
ASSERT_RE = re.compile(r'.*(?<![-:a-zA-Z#][ -])\bassert\b(?!:).*')
|
||||||
|
|
||||||
all_matches = defaultdict(list)
|
|
||||||
|
|
||||||
for dirpath, dirnames, filenames in os.walk(PATH):
|
def main():
|
||||||
for filename in filenames:
|
failed = False
|
||||||
path = os.path.join(dirpath, filename)
|
|
||||||
if not os.path.isfile(path) or not path.endswith('.py'):
|
|
||||||
continue
|
|
||||||
|
|
||||||
|
for path in sys.argv[1:]:
|
||||||
with open(path, 'r') as f:
|
with open(path, 'r') as f:
|
||||||
for i, line in enumerate(f.readlines()):
|
for i, line in enumerate(f.readlines()):
|
||||||
matches = ASSERT_RE.findall(line)
|
matches = ASSERT_RE.findall(line)
|
||||||
|
|
||||||
if matches:
|
if matches:
|
||||||
all_matches[path].append((i + 1, line.index('assert') + 1, matches))
|
failed = True
|
||||||
|
lineno = i + 1
|
||||||
|
colno = line.index('assert') + 1
|
||||||
|
print('%s:%d:%d: raise AssertionError instead of: %s' % (path, lineno, colno, matches[0][colno - 1:]))
|
||||||
|
|
||||||
|
if failed:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
if all_matches:
|
|
||||||
print('Use of assert in production code is not recommended.')
|
|
||||||
print('Python will remove all assert statements if run with optimizations')
|
|
||||||
print('Alternatives:')
|
|
||||||
print(' if not isinstance(value, dict):')
|
|
||||||
print(' raise AssertionError("Expected a dict for value")')
|
|
||||||
|
|
||||||
for path, matches in all_matches.items():
|
if __name__ == '__main__':
|
||||||
for line_matches in matches:
|
main()
|
||||||
for match in line_matches[2]:
|
|
||||||
print('%s:%d:%d: %s' % ((path,) + line_matches[:2] + (match,)))
|
|
||||||
sys.exit(1)
|
|
||||||
|
|||||||
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"extensions": [
|
||||||
|
".yml",
|
||||||
|
".yaml"
|
||||||
|
],
|
||||||
|
"output": "path-line-column-message"
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue