Skip to content

Commit 706f19f

Browse files
authored
Merge pull request #987 from zapta/main
The apio test runner logger now writes the logged output also to stdo…
2 parents 110a594 + 565ad82 commit 706f19f

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

tests/conftest.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import subprocess
66
from subprocess import CompletedProcess
77
from dataclasses import dataclass
8-
from io import StringIO
8+
from io import StringIO, TextIOBase
99
import shutil
1010
import tempfile
1111
import contextlib
@@ -33,11 +33,42 @@
3333
ANSI_DECODER = AnsiDecoder()
3434

3535

36+
class _LogTee(TextIOBase):
37+
"""Output stream that writes both to a buffer and to the real
38+
std/err stream. Used by the 'with_log' operation."""
39+
40+
def __init__(self, log_buffer: StringIO, live_stream: TextIOBase):
41+
self._buf = log_buffer
42+
self._live = live_stream
43+
44+
# @overrides
45+
def write(self, s: str) -> int:
46+
# -- Write to the log buffer
47+
self._buf.write(s)
48+
# -- Write to live output.
49+
self._live.write(s)
50+
self._live.flush()
51+
# -- All done.
52+
return len(s)
53+
54+
# @overrides
55+
def flush(self) -> None:
56+
self._buf.flush()
57+
self._live.flush()
58+
59+
3660
class CapturedLog:
3761
"""Holds the captured stdout + stderr."""
3862

3963
def __init__(self):
4064
self._buf = StringIO()
65+
self._output_stream = _LogTee(self._buf, sys.__stdout__)
66+
67+
@property
68+
def output_stream(self) -> TextIOBase:
69+
"""Getter to the output stream that sends the output to the logger
70+
buffer and to live output."""
71+
return self._output_stream
4172

4273
@property
4374
def buf(self) -> StringIO:
@@ -480,12 +511,14 @@ def _get_local_config_url() -> str:
480511
@contextlib.contextmanager
481512
def with_logger(self):
482513
"""Capture stdout + stderr and yield a CapturedLog object."""
514+
print("----- Begin log")
483515
log = CapturedLog()
484516
with (
485-
contextlib.redirect_stdout(log.buf),
486-
contextlib.redirect_stderr(log.buf),
517+
contextlib.redirect_stdout(log.output_stream),
518+
contextlib.redirect_stderr(log.output_stream),
487519
):
488520
yield log
521+
print("----- End log")
489522

490523
@property
491524
def sandbox(self) -> Optional[ApioSandbox]:

0 commit comments

Comments
 (0)