mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
7 years ago
|
"""Sanity test for proper python syntax."""
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
7 years ago
|
|
||
|
import os
|
||
|
|
||
5 years ago
|
from .. import types as t
|
||
5 years ago
|
|
||
5 years ago
|
from ..sanity import (
|
||
7 years ago
|
SanityMultipleVersion,
|
||
|
SanityMessage,
|
||
|
SanityFailure,
|
||
|
SanitySuccess,
|
||
5 years ago
|
SanityTargets,
|
||
5 years ago
|
SANITY_ROOT,
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
from ..target import (
|
||
5 years ago
|
TestTarget,
|
||
7 years ago
|
)
|
||
|
|
||
5 years ago
|
from ..util import (
|
||
7 years ago
|
SubprocessError,
|
||
7 years ago
|
display,
|
||
7 years ago
|
find_python,
|
||
6 years ago
|
parse_to_list_of_dict,
|
||
5 years ago
|
is_subdir,
|
||
7 years ago
|
)
|
||
|
|
||
5 years ago
|
from ..util_common import (
|
||
5 years ago
|
run_command,
|
||
|
)
|
||
|
|
||
5 years ago
|
from ..config import (
|
||
7 years ago
|
SanityConfig,
|
||
|
)
|
||
|
|
||
|
|
||
|
class CompileTest(SanityMultipleVersion):
|
||
|
"""Sanity test for proper python syntax."""
|
||
5 years ago
|
def filter_targets(self, targets): # type: (t.List[TestTarget]) -> t.List[TestTarget]
|
||
|
"""Return the given list of test targets, filtered to include only those relevant for the test."""
|
||
|
return [target for target in targets if os.path.splitext(target.path)[1] == '.py' or is_subdir(target.path, 'bin')]
|
||
|
|
||
7 years ago
|
def test(self, args, targets, python_version):
|
||
|
"""
|
||
|
:type args: SanityConfig
|
||
|
:type targets: SanityTargets
|
||
|
:type python_version: str
|
||
7 years ago
|
:rtype: TestResult
|
||
7 years ago
|
"""
|
||
5 years ago
|
settings = self.load_processor(args, python_version)
|
||
7 years ago
|
|
||
5 years ago
|
paths = [target.path for target in targets.include]
|
||
7 years ago
|
|
||
5 years ago
|
cmd = [find_python(python_version), os.path.join(SANITY_ROOT, 'compile', 'compile.py')]
|
||
7 years ago
|
|
||
|
data = '\n'.join(paths)
|
||
|
|
||
|
display.info(data, verbosity=4)
|
||
7 years ago
|
|
||
|
try:
|
||
7 years ago
|
stdout, stderr = run_command(args, cmd, data=data, capture=True)
|
||
7 years ago
|
status = 0
|
||
|
except SubprocessError as ex:
|
||
|
stdout = ex.stdout
|
||
|
stderr = ex.stderr
|
||
|
status = ex.status
|
||
|
|
||
|
if stderr:
|
||
|
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
|
||
|
|
||
|
if args.explain:
|
||
|
return SanitySuccess(self.name, python_version=python_version)
|
||
|
|
||
|
pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<message>.*)$'
|
||
|
|
||
6 years ago
|
results = parse_to_list_of_dict(pattern, stdout)
|
||
7 years ago
|
|
||
|
results = [SanityMessage(
|
||
|
message=r['message'],
|
||
|
path=r['path'].replace('./', ''),
|
||
|
line=int(r['line']),
|
||
|
column=int(r['column']),
|
||
|
) for r in results]
|
||
|
|
||
5 years ago
|
results = settings.process_errors(results, paths)
|
||
7 years ago
|
|
||
|
if results:
|
||
|
return SanityFailure(self.name, messages=results, python_version=python_version)
|
||
|
|
||
|
return SanitySuccess(self.name, python_version=python_version)
|