From ca0b2dd3a731f43731978e7773c170beb338bf0c Mon Sep 17 00:00:00 2001 From: Oleg Chaplashkin Date: Mon, 25 Sep 2023 11:36:50 +0400 Subject: [PATCH] Add duplicate file descriptor to capture stderr We synchronize the options of two tools: test-run and luatest. When luatest runs separate tarantool instance it duplicates the streams thereby we can see logs(warning, error, etc) and prints. We have added a context manager to create the same behavior with luatest. Close tarantool/luatest#308 --- lib/luatest_server.py | 7 +++---- lib/options.py | 16 ++-------------- lib/utils.py | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/luatest_server.py b/lib/luatest_server.py index ac68e2f5..0a19fb11 100644 --- a/lib/luatest_server.py +++ b/lib/luatest_server.py @@ -14,6 +14,7 @@ from lib.tarantool_server import Test from lib.tarantool_server import TestExecutionError from lib.tarantool_server import TarantoolServer +from lib.utils import captured_stderr def timeout_handler(process, test_timeout): @@ -62,10 +63,8 @@ def execute(self, server): project_dir = os.environ['SOURCEDIR'] with open(server.logfile, 'ab') as f: - stderr = f - if Options().args.show_capture: - stderr = sys.stdout - proc = Popen(command, cwd=project_dir, stdout=sys.stdout, stderr=stderr) + with captured_stderr(f): + proc = Popen(command, cwd=project_dir, stdout=sys.stdout, stderr=f) sampler.register_process(proc.pid, self.id, server.name) test_timeout = Options().args.test_timeout timer = Timer(test_timeout, timeout_handler, (proc, test_timeout)) diff --git a/lib/options.py b/lib/options.py index 88c95051..c9cb0fb5 100644 --- a/lib/options.py +++ b/lib/options.py @@ -37,17 +37,6 @@ def format_help(s): return textwrap.dedent(s.lstrip('\n')) + '\n' -class DeprecationWarning(argparse._StoreTrueAction): - """Сustom definition of the 'store_true' procedure""" - - def __call__(self, parser, namespace, values, option_string=None): - color_stdout( - "Argument %s is deprecated and is ignored.\n" % self.option_strings, - schema='info' - ) - setattr(namespace, self.dest, values) - - class Options(object): """Handle options of test-runner""" @@ -135,13 +124,12 @@ def __init__(self): """)) parser.add_argument( - "--verbose", + "-v", "--verbose", dest='is_verbose', - action=DeprecationWarning, + action='store_true', default=False, help=format_help( """ - Deprecated. Print TAP13 test output to log. Default: false. diff --git a/lib/utils.py b/lib/utils.py index 03423768..e439f8bf 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -8,6 +8,7 @@ import time import json import subprocess +import tempfile from lib.colorer import color_stdout try: @@ -372,3 +373,23 @@ def prepend_path(p): def shlex_quote(s): return _shlex_quote(s) + + +class captured_stderr: + def __init__(self, logfile): + self.prevfd = None + self.prev = None + self.f = logfile + + def __enter__(self): + self.prevfde = os.dup(self.f.fileno()) + + os.dup2(sys.stderr.fileno(), self.f.fileno()) + + self.preve = sys.stderr + sys.stderr = os.fdopen(self.prevfde, "w") + return self.f + + def __exit__(self, exc_type, exc_value, traceback): + os.dup2(self.prevfde, self.preve.fileno()) + sys.stderr = self.preve