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.
96 lines
2.6 KiB
Python
96 lines
2.6 KiB
Python
7 years ago
|
"""Sanity test using rstcheck."""
|
||
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
|
SanitySingleVersion,
|
||
|
SanityMessage,
|
||
|
SanityFailure,
|
||
|
SanitySuccess,
|
||
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,
|
||
6 years ago
|
parse_to_list_of_dict,
|
||
6 years ago
|
read_lines_without_comments,
|
||
5 years ago
|
find_python,
|
||
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 RstcheckTest(SanitySingleVersion):
|
||
|
"""Sanity test using rstcheck."""
|
||
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] in ('.rst',)]
|
||
|
|
||
5 years ago
|
def test(self, args, targets, python_version):
|
||
7 years ago
|
"""
|
||
|
:type args: SanityConfig
|
||
|
:type targets: SanityTargets
|
||
5 years ago
|
:type python_version: str
|
||
7 years ago
|
:rtype: TestResult
|
||
7 years ago
|
"""
|
||
5 years ago
|
ignore_file = os.path.join(SANITY_ROOT, 'rstcheck', 'ignore-substitutions.txt')
|
||
6 years ago
|
ignore_substitutions = sorted(set(read_lines_without_comments(ignore_file, remove_blank_lines=True)))
|
||
7 years ago
|
|
||
5 years ago
|
settings = self.load_processor(args)
|
||
5 years ago
|
|
||
5 years ago
|
paths = [target.path for target in targets.include]
|
||
7 years ago
|
|
||
|
cmd = [
|
||
5 years ago
|
find_python(python_version),
|
||
7 years ago
|
'-m', 'rstcheck',
|
||
7 years ago
|
'--report', 'warning',
|
||
|
'--ignore-substitutions', ','.join(ignore_substitutions),
|
||
|
] + paths
|
||
|
|
||
|
try:
|
||
|
stdout, stderr = run_command(args, cmd, capture=True)
|
||
|
status = 0
|
||
|
except SubprocessError as ex:
|
||
|
stdout = ex.stdout
|
||
|
stderr = ex.stderr
|
||
|
status = ex.status
|
||
|
|
||
|
if stdout:
|
||
|
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
|
||
|
|
||
|
if args.explain:
|
||
|
return SanitySuccess(self.name)
|
||
|
|
||
|
pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+): \((?P<level>INFO|WARNING|ERROR|SEVERE)/[0-4]\) (?P<message>.*)$'
|
||
|
|
||
6 years ago
|
results = parse_to_list_of_dict(pattern, stderr)
|
||
7 years ago
|
|
||
|
results = [SanityMessage(
|
||
|
message=r['message'],
|
||
|
path=r['path'],
|
||
|
line=int(r['line']),
|
||
|
column=0,
|
||
|
level=r['level'],
|
||
|
) for r in results]
|
||
|
|
||
5 years ago
|
settings.process_errors(results, paths)
|
||
|
|
||
7 years ago
|
if results:
|
||
|
return SanityFailure(self.name, messages=results)
|
||
|
|
||
|
return SanitySuccess(self.name)
|