mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-16 01:45:25 -07:00
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.
This commit is contained in:
parent
ce80485ecd
commit
2b6ac4561b
5 changed files with 105 additions and 53 deletions
|
@ -3,6 +3,7 @@ from __future__ import absolute_import, print_function
|
|||
|
||||
import abc
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
|
||||
|
@ -13,6 +14,7 @@ from lib.util import (
|
|||
run_command,
|
||||
import_plugins,
|
||||
load_plugins,
|
||||
parse_to_dict,
|
||||
ABC,
|
||||
)
|
||||
|
||||
|
@ -94,7 +96,7 @@ def command_sanity(args):
|
|||
options = ''
|
||||
|
||||
if isinstance(test, SanityCodeSmellTest):
|
||||
result = test.test(args)
|
||||
result = test.test(args, targets)
|
||||
elif isinstance(test, SanityMultipleVersion):
|
||||
result = test.test(args, targets, python_version=version)
|
||||
options = ' --python %s' % version
|
||||
|
@ -205,19 +207,52 @@ class SanityCodeSmellTest(SanityTest):
|
|||
"""Sanity test script."""
|
||||
def __init__(self, path):
|
||||
name = os.path.splitext(os.path.basename(path))[0]
|
||||
config = os.path.splitext(path)[0] + '.json'
|
||||
|
||||
self.path = path
|
||||
self.config = config if os.path.exists(config) else None
|
||||
|
||||
super(SanityCodeSmellTest, self).__init__(name)
|
||||
|
||||
def test(self, args):
|
||||
def test(self, args, targets):
|
||||
"""
|
||||
:type args: SanityConfig
|
||||
:type targets: SanityTargets
|
||||
:rtype: SanityResult
|
||||
"""
|
||||
cmd = [self.path]
|
||||
env = ansible_environment(args, color=False)
|
||||
|
||||
pattern = None
|
||||
|
||||
if self.config:
|
||||
with open(self.config, 'r') as config_fd:
|
||||
config = json.load(config_fd)
|
||||
|
||||
output = config.get('output')
|
||||
extensions = config.get('extensions')
|
||||
prefixes = config.get('prefixes')
|
||||
|
||||
if output == 'path-line-column-message':
|
||||
pattern = '^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<message>.*)$'
|
||||
elif output == 'path-message':
|
||||
pattern = '^(?P<path>[^:]*): (?P<message>.*)$'
|
||||
else:
|
||||
pattern = ApplicationError('Unsupported output type: %s' % output)
|
||||
|
||||
paths = sorted(i.path for i in targets.include)
|
||||
|
||||
if extensions:
|
||||
paths = [p for p in paths if os.path.splitext(p)[1] in extensions or (p.startswith('bin/') and '.py' in extensions)]
|
||||
|
||||
if prefixes:
|
||||
paths = [p for p in paths if any(p.startswith(pre) for pre in prefixes)]
|
||||
|
||||
if not paths:
|
||||
return SanitySkipped(self.name)
|
||||
|
||||
cmd += paths
|
||||
|
||||
try:
|
||||
stdout, stderr = run_command(args, cmd, env=env, capture=True)
|
||||
status = 0
|
||||
|
@ -226,6 +261,19 @@ class SanityCodeSmellTest(SanityTest):
|
|||
stderr = ex.stderr
|
||||
status = ex.status
|
||||
|
||||
if stdout and not stderr:
|
||||
if pattern:
|
||||
matches = [parse_to_dict(pattern, line) for line in stdout.splitlines()]
|
||||
|
||||
messages = [SanityMessage(
|
||||
message=m['message'],
|
||||
path=m['path'],
|
||||
line=int(m.get('line', 0)),
|
||||
column=int(m.get('column', 0)),
|
||||
) for m in matches]
|
||||
|
||||
return SanityFailure(self.name, messages=messages)
|
||||
|
||||
if stderr or status:
|
||||
summary = u'%s' % SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
|
||||
return SanityFailure(self.name, summary=summary)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue