Improve ansible-test match error handling.

This commit is contained in:
Matt Clay 2018-09-20 22:20:08 -07:00
parent 27c10fa502
commit 2056c981ae
6 changed files with 25 additions and 13 deletions

View file

@ -15,7 +15,7 @@ from lib.util import (
run_command, run_command,
import_plugins, import_plugins,
load_plugins, load_plugins,
parse_to_dict, parse_to_list_of_dict,
ABC, ABC,
is_binary_file, is_binary_file,
read_lines_without_comments, read_lines_without_comments,
@ -305,7 +305,7 @@ class SanityCodeSmellTest(SanityTest):
if stdout and not stderr: if stdout and not stderr:
if pattern: if pattern:
matches = [parse_to_dict(pattern, line) for line in stdout.splitlines()] matches = parse_to_list_of_dict(pattern, stdout)
messages = [SanityMessage( messages = [SanityMessage(
message=m['message'], message=m['message'],

View file

@ -18,6 +18,7 @@ from lib.util import (
display, display,
find_python, find_python,
read_lines_without_comments, read_lines_without_comments,
parse_to_list_of_dict,
) )
from lib.config import ( from lib.config import (
@ -72,7 +73,7 @@ class CompileTest(SanityMultipleVersion):
pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<message>.*)$' pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<message>.*)$'
results = [re.search(pattern, line).groupdict() for line in stdout.splitlines()] results = parse_to_list_of_dict(pattern, stdout)
results = [SanityMessage( results = [SanityMessage(
message=r['message'], message=r['message'],

View file

@ -20,6 +20,7 @@ from lib.util import (
display, display,
find_python, find_python,
read_lines_without_comments, read_lines_without_comments,
parse_to_list_of_dict,
) )
from lib.ansible_util import ( from lib.ansible_util import (
@ -112,7 +113,7 @@ class ImportTest(SanityMultipleVersion):
pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<message>.*)$' pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<message>.*)$'
results = [re.search(pattern, line).groupdict() for line in ex.stdout.splitlines()] results = parse_to_list_of_dict(pattern, ex.stdout)
results = [SanityMessage( results = [SanityMessage(
message=r['message'], message=r['message'],

View file

@ -16,6 +16,7 @@ from lib.util import (
display, display,
run_command, run_command,
read_lines_without_comments, read_lines_without_comments,
parse_to_list_of_dict,
) )
from lib.config import ( from lib.config import (
@ -80,7 +81,7 @@ class Pep8Test(SanitySingleVersion):
if stdout: if stdout:
pattern = '^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<code>[WE][0-9]{3}) (?P<message>.*)$' pattern = '^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<code>[WE][0-9]{3}) (?P<message>.*)$'
results = [re.search(pattern, line).groupdict() for line in stdout.splitlines()] results = parse_to_list_of_dict(pattern, stdout)
else: else:
results = [] results = []

View file

@ -14,7 +14,7 @@ from lib.sanity import (
from lib.util import ( from lib.util import (
SubprocessError, SubprocessError,
run_command, run_command,
parse_to_dict, parse_to_list_of_dict,
display, display,
find_executable, find_executable,
read_lines_without_comments, read_lines_without_comments,
@ -72,7 +72,7 @@ class RstcheckTest(SanitySingleVersion):
pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+): \((?P<level>INFO|WARNING|ERROR|SEVERE)/[0-4]\) (?P<message>.*)$' pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+): \((?P<level>INFO|WARNING|ERROR|SEVERE)/[0-4]\) (?P<message>.*)$'
results = [parse_to_dict(pattern, line) for line in stderr.splitlines()] results = parse_to_list_of_dict(pattern, stderr)
results = [SanityMessage( results = [SanityMessage(
message=r['message'], message=r['message'],

View file

@ -741,18 +741,27 @@ def docker_qualify_image(name):
return config.get('name', name) return config.get('name', name)
def parse_to_dict(pattern, value): def parse_to_list_of_dict(pattern, value):
""" """
:type pattern: str :type pattern: str
:type value: str :type value: str
:return: dict[str, str] :return: list[dict[str, str]]
""" """
match = re.search(pattern, value) matched = []
unmatched = []
if match is None: for line in value.splitlines():
raise Exception('Pattern "%s" did not match value: %s' % (pattern, value)) match = re.search(pattern, line)
return match.groupdict() if match:
matched.append(match.groupdict())
else:
unmatched.append(line)
if unmatched:
raise Exception('Pattern "%s" did not match values:\n%s' % (pattern, '\n'.join(unmatched)))
return matched
def get_available_port(): def get_available_port():