Skip to content

Commit ca0b2dd

Browse files
author
Oleg Chaplashkin
committed
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
1 parent d108b71 commit ca0b2dd

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

lib/luatest_server.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from lib.tarantool_server import Test
1515
from lib.tarantool_server import TestExecutionError
1616
from lib.tarantool_server import TarantoolServer
17+
from lib.utils import captured_stderr
1718

1819

1920
def timeout_handler(process, test_timeout):
@@ -62,10 +63,8 @@ def execute(self, server):
6263
project_dir = os.environ['SOURCEDIR']
6364

6465
with open(server.logfile, 'ab') as f:
65-
stderr = f
66-
if Options().args.show_capture:
67-
stderr = sys.stdout
68-
proc = Popen(command, cwd=project_dir, stdout=sys.stdout, stderr=stderr)
66+
with captured_stderr(f):
67+
proc = Popen(command, cwd=project_dir, stdout=sys.stdout, stderr=f)
6968
sampler.register_process(proc.pid, self.id, server.name)
7069
test_timeout = Options().args.test_timeout
7170
timer = Timer(test_timeout, timeout_handler, (proc, test_timeout))

lib/options.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ def format_help(s):
3737
return textwrap.dedent(s.lstrip('\n')) + '\n'
3838

3939

40-
class DeprecationWarning(argparse._StoreTrueAction):
41-
"""Сustom definition of the 'store_true' procedure"""
42-
43-
def __call__(self, parser, namespace, values, option_string=None):
44-
color_stdout(
45-
"Argument %s is deprecated and is ignored.\n" % self.option_strings,
46-
schema='info'
47-
)
48-
setattr(namespace, self.dest, values)
49-
50-
5140
class Options(object):
5241
"""Handle options of test-runner"""
5342

@@ -135,13 +124,12 @@ def __init__(self):
135124
"""))
136125

137126
parser.add_argument(
138-
"--verbose",
127+
"-v", "--verbose",
139128
dest='is_verbose',
140-
action=DeprecationWarning,
129+
action='store_true',
141130
default=False,
142131
help=format_help(
143132
"""
144-
Deprecated.
145133
Print TAP13 test output to log.
146134
147135
Default: false.

lib/utils.py

+21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import time
99
import json
1010
import subprocess
11+
import tempfile
1112
from lib.colorer import color_stdout
1213

1314
try:
@@ -372,3 +373,23 @@ def prepend_path(p):
372373

373374
def shlex_quote(s):
374375
return _shlex_quote(s)
376+
377+
378+
class captured_stderr:
379+
def __init__(self, logfile):
380+
self.prevfd = None
381+
self.prev = None
382+
self.f = logfile
383+
384+
def __enter__(self):
385+
self.prevfde = os.dup(self.f.fileno())
386+
387+
os.dup2(sys.stderr.fileno(), self.f.fileno())
388+
389+
self.preve = sys.stderr
390+
sys.stderr = os.fdopen(self.prevfde, "w")
391+
return self.f
392+
393+
def __exit__(self, exc_type, exc_value, traceback):
394+
os.dup2(self.prevfde, self.preve.fileno())
395+
sys.stderr = self.preve

0 commit comments

Comments
 (0)