From 69a2813fdcc90f662c3c1f37fc3d335bf734bd2d Mon Sep 17 00:00:00 2001 From: XiaGuochao Date: Tue, 14 Apr 2020 17:10:26 +0800 Subject: [PATCH] fix log and regex match bug --- test/maple_test/compare.py | 70 +++++++++++++++++++++----------------- test/maple_test/main.py | 1 - test/maple_test/run.py | 11 ++++-- test/maple_test/task.py | 9 +++-- 4 files changed, 50 insertions(+), 41 deletions(-) diff --git a/test/maple_test/compare.py b/test/maple_test/compare.py index e6f41e0f57..2d2365e065 100644 --- a/test/maple_test/compare.py +++ b/test/maple_test/compare.py @@ -78,35 +78,10 @@ def main(): pattern_flag, pattern = extract_pattern(assert_line, assert_flag) if not is_valid_pattern(pattern): match_pass = False - break + raise CompareError("Not valid pattern: {!r}".format(pattern)) keywords = pattern_flag.split("-") - valid_keywords = [] - assert_mode = keywords[0] - match_func = None - if assert_mode == "scan": - match_func = regex_match - valid_keywords = SCAN_KEYWORDS - elif assert_mode == "cmp": - match_func = cmp_match - valid_keywords = CMP_KEYWORDS - else: - raise CompareError("scan mode: {} is not valid".format(assert_mode)) - for keyword in keywords[1:]: - if keyword not in valid_keywords: - raise CompareError( - "keyword: {} is not valid for {}".format(keyword, assert_mode) - ) - if keyword == "auto": - match_func = partial(auto_regex_match, match_func=match_func) - elif keyword == "not": - match_func = partial(not_match, match_func=match_func) - elif keyword == "next": - match_func = partial(next_match, match_func=match_func) - elif keyword == "end": - match_func = end_match - elif keyword == "full": - match_func = full_match + match_func = gen_match_func(keywords) if "next" not in keywords and "end" not in keywords: start = 0 result, start = match_func(content, line_map, pattern, start) @@ -133,10 +108,39 @@ def main(): ) ) return 0 - sys.exit(1) +def gen_match_func(keywords): + valid_keywords = [] + assert_mode = keywords[0] + match_func = None + if assert_mode == "scan": + match_func = regex_match + valid_keywords = SCAN_KEYWORDS + elif assert_mode == "cmp": + match_func = cmp_match + valid_keywords = CMP_KEYWORDS + else: + raise CompareError("scan mode: {} is not valid".format(assert_mode)) + for keyword in keywords[1:]: + if keyword not in valid_keywords: + raise CompareError( + "keyword: {} is not valid for {}".format(keyword, assert_mode) + ) + if keyword == "auto": + match_func = partial(auto_regex_match, match_func=match_func) + elif keyword == "not": + match_func = partial(not_match, match_func=match_func) + elif keyword == "next": + match_func = partial(next_match, match_func=match_func) + elif keyword == "end": + match_func = end_match + elif keyword == "full": + match_func = full_match + return match_func + + def extract_pattern(line, flag): line_flag, pattern_line = line.lstrip().split(":", 1) if line_flag.strip() != flag: @@ -145,7 +149,7 @@ def extract_pattern(line, flag): pattern_flag, raw_pattern = pattern_line.lstrip().split(" ", 1) except ValueError: pattern_flag = pattern_line.lstrip() - raw_pattern = None + raw_pattern = "" return pattern_flag, raw_pattern @@ -155,8 +159,9 @@ def is_valid_pattern(pattern): except re.error: logging.error("Error pattern: {!r}".format(pattern)) return False - finally: - return True + except TypeError: + logging.error(type(pattern), repr(pattern)) + return True def parse_cli(): @@ -183,6 +188,7 @@ def parse_cli(): def regex_match(content, line_map, pattern, start=0): + pattern = r"\s*".join([word for word in pattern.split()]) matches = re.finditer(str(pattern), content, re.MULTILINE) end = 0 for _, match in enumerate(matches, start=1): @@ -204,7 +210,7 @@ def cmp_match(content, line_map, pattern, start=0): def auto_regex_match(content, line_map, pattern, start=0, match_func=regex_match): - pattern = r"\s+".join([re.escape(word.strip()) for word in pattern.strip().split()]) + pattern = r"\s+".join([re.escape(word) for word in pattern.split()]) return match_func(content, line_map, pattern, start) diff --git a/test/maple_test/main.py b/test/maple_test/main.py index cd22bd9847..c44f3784e1 100644 --- a/test/maple_test/main.py +++ b/test/maple_test/main.py @@ -28,7 +28,6 @@ from maple_test.utils import timer from maple_test.run import TestError -@timer def main(): test_suite_config, running_config, log_config = configs.init_config() logger = configs.LOGGER diff --git a/test/maple_test/run.py b/test/maple_test/run.py index 1d2258fa38..d40ad00991 100644 --- a/test/maple_test/run.py +++ b/test/maple_test/run.py @@ -20,6 +20,7 @@ import subprocess import sys import time import timeit +import logging from textwrap import indent, shorten from maple_test.configs import construct_logger, get_val @@ -76,7 +77,11 @@ def run_commands( position, old_result, commands, work_dir, timeout, log_config, env=None ): name = "{}_{}".format(log_config[1], int(time.time())) - logger = construct_logger(log_config[0], name) + name = log_config[1] + formatter = logging.Formatter( + "%(asctime)s %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S" + ) + logger = construct_logger(log_config[0], log_config[1], file_fmt=formatter) if not commands: err = "Run task exit unexpected : {}, Log file at: {}.log".format( old_result[-1], log_config[0].get("dir") / name @@ -104,8 +109,8 @@ def run_commands( ) if return_code != 0: result = (FAIL, (return_code, command, shorten(com_err, width=84))) - err = "Run task exit unexpected : {}, Log file at: {}.log".format( - result[-1], log_config[0].get("dir") / name + err = "Failed, Log file at: {}.log".format( + log_config[0].get("dir") / name ) logger.error(err) break diff --git a/test/maple_test/task.py b/test/maple_test/task.py index 857b4d4814..dbacdf734b 100644 --- a/test/maple_test/task.py +++ b/test/maple_test/task.py @@ -165,6 +165,10 @@ class TestSuiteTask: task = SingleTask(case, config, running_config) self.task_set[name].append(task) self.task_set_result[name][task.result[0]] += 1 + if sum([len(case) for case in self.task_set.values()]) < 1: + logger.info( + "Path %s not in testlist, be sure add path to testlist", str(self.path), + ) @staticmethod def _get_testlist(config, base_dir, encoding): @@ -191,11 +195,6 @@ class TestSuiteTask: for file in case_files if is_relative(file, self.path) ] - if not case_files: - logger.info( - "Path %s not in testlist, be sure add path to testlist", - str(self.path), - ) for case_file in case_files: case_name = str(case_file).replace(".", "_") comment = self.suffix_comments[case_file.suffix[1:]] -- Gitee