From f55818059c01ff9e61bee8107ed1389fe272a787 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Mon, 29 May 2023 14:23:52 +0100 Subject: Tests: Log reworked. All log-related code moved to the log.py. --- test/unit/log.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 7 deletions(-) (limited to 'test/unit/log.py') diff --git a/test/unit/log.py b/test/unit/log.py index f984d7a1..c98054d5 100644 --- a/test/unit/log.py +++ b/test/unit/log.py @@ -1,23 +1,95 @@ +import os +import re +import sys + +from unit.option import option + UNIT_LOG = 'unit.log' +def print_log_on_assert(func): + def inner_function(*args, **kwargs): + try: + func(*args, **kwargs) + except AssertionError as exception: + Log.print_log(*args, **kwargs) + raise exception + + return inner_function + + class Log: - temp_dir = None pos = {} + @staticmethod + @print_log_on_assert + def check_alerts(log=None): + if log is None: + log = Log.read(encoding='utf-8') + + found = False + alerts = re.findall(r'.+\[alert\].+', log) + + if alerts: + found = True + + if option.detailed: + print('\nAll alerts/sanitizer errors found in log:') + _ = [print(alert) for alert in alerts] + + if option.skip_alerts: + for skip in option.skip_alerts: + alerts = [al for al in alerts if re.search(skip, al) is None] + + assert not alerts, 'alert(s)' + + if not option.skip_sanitizer: + sanitizer_errors = re.findall('.+Sanitizer.+', log) + + assert not sanitizer_errors, 'sanitizer error(s)' + + if found and option.detailed: + print('skipped.') + + @staticmethod + def get_path(name=UNIT_LOG): + return f'{option.temp_dir}/{name}' + + @staticmethod def open(name=UNIT_LOG, encoding=None): - f = open(Log.get_path(name), 'r', encoding=encoding, errors='ignore') - f.seek(Log.pos.get(name, 0)) + file = open(Log.get_path(name), 'r', encoding=encoding, errors='ignore') + file.seek(Log.pos.get(name, 0)) + + return file + + @staticmethod + def print_log(log=None): + Log.print_path() - return f + if option.print_log: + os.set_blocking(sys.stdout.fileno(), True) + sys.stdout.flush() + if log is None: + log = Log.read(encoding='utf-8') + + sys.stdout.write(log) + + @staticmethod + def print_path(): + print(f'Path to {UNIT_LOG}:\n{Log.get_path()}\n') + + @staticmethod + def read(*args, **kwargs): + with Log.open(*args, **kwargs) as file: + return file.read() + + @staticmethod def set_pos(pos, name=UNIT_LOG): Log.pos[name] = pos + @staticmethod def swap(name): pos = Log.pos.get(UNIT_LOG, 0) Log.pos[UNIT_LOG] = Log.pos.get(name, 0) Log.pos[name] = pos - - def get_path(name=UNIT_LOG): - return f'{Log.temp_dir}/{name}' -- cgit From ce2405ec3dd97e8bdf8f63312e3c6ce59ef562d4 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Mon, 12 Jun 2023 14:16:59 +0100 Subject: Tests: prerequisites checking reworked. Prerequisites check moved to the module level to simplify class structure. Discovery and prerequisites checks functions moved to the separate files. Introduced "require" fixture to provide per-test requirements check. --- test/unit/log.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'test/unit/log.py') diff --git a/test/unit/log.py b/test/unit/log.py index c98054d5..7d7e355a 100644 --- a/test/unit/log.py +++ b/test/unit/log.py @@ -1,6 +1,7 @@ import os import re import sys +import time from unit.option import option @@ -25,7 +26,7 @@ class Log: @print_log_on_assert def check_alerts(log=None): if log is None: - log = Log.read(encoding='utf-8') + log = Log.read() found = False alerts = re.findall(r'.+\[alert\].+', log) @@ -51,12 +52,16 @@ class Log: if found and option.detailed: print('skipped.') + @staticmethod + def findall(pattern, name=UNIT_LOG, flags=re.M): + return re.findall(pattern, Log.read(name), flags) + @staticmethod def get_path(name=UNIT_LOG): return f'{option.temp_dir}/{name}' @staticmethod - def open(name=UNIT_LOG, encoding=None): + def open(name=UNIT_LOG, encoding='utf-8'): file = open(Log.get_path(name), 'r', encoding=encoding, errors='ignore') file.seek(Log.pos.get(name, 0)) @@ -71,7 +76,7 @@ class Log: sys.stdout.flush() if log is None: - log = Log.read(encoding='utf-8') + log = Log.read() sys.stdout.write(log) @@ -93,3 +98,16 @@ class Log: pos = Log.pos.get(UNIT_LOG, 0) Log.pos[UNIT_LOG] = Log.pos.get(name, 0) Log.pos[name] = pos + + @staticmethod + def wait_for_record(pattern, name=UNIT_LOG, wait=150, flags=re.M): + with Log.open(name) as file: + for _ in range(wait): + found = re.search(pattern, file.read(), flags) + + if found is not None: + break + + time.sleep(0.1) + + return found -- cgit