Skip to content

Commit de998a4

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 de998a4

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-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

+20
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,23 @@ def prepend_path(p):
372372

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

0 commit comments

Comments
 (0)