Skip to content

Commit

Permalink
Add duplicate file descriptor to capture stderr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Oleg Chaplashkin committed Sep 25, 2023
1 parent d108b71 commit ca0b2dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
7 changes: 3 additions & 4 deletions lib/luatest_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down
16 changes: 2 additions & 14 deletions lib/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
import json
import subprocess
import tempfile
from lib.colorer import color_stdout

try:
Expand Down Expand Up @@ -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

0 comments on commit ca0b2dd

Please sign in to comment.