test_iostream.py revision 12391:ceeca8b41e4b
1from pybind11_tests import iostream as m
2import sys
3
4from contextlib import contextmanager
5
6try:
7    # Python 3
8    from io import StringIO
9except ImportError:
10    # Python 2
11    try:
12        from cStringIO import StringIO
13    except ImportError:
14        from StringIO import StringIO
15
16try:
17    # Python 3.4
18    from contextlib import redirect_stdout
19except ImportError:
20    @contextmanager
21    def redirect_stdout(target):
22        original = sys.stdout
23        sys.stdout = target
24        yield
25        sys.stdout = original
26
27try:
28    # Python 3.5
29    from contextlib import redirect_stderr
30except ImportError:
31    @contextmanager
32    def redirect_stderr(target):
33        original = sys.stderr
34        sys.stderr = target
35        yield
36        sys.stderr = original
37
38
39def test_captured(capsys):
40    msg = "I've been redirected to Python, I hope!"
41    m.captured_output(msg)
42    stdout, stderr = capsys.readouterr()
43    assert stdout == msg
44    assert stderr == ''
45
46    m.captured_output_default(msg)
47    stdout, stderr = capsys.readouterr()
48    assert stdout == msg
49    assert stderr == ''
50
51    m.captured_err(msg)
52    stdout, stderr = capsys.readouterr()
53    assert stdout == ''
54    assert stderr == msg
55
56
57def test_guard_capture(capsys):
58    msg = "I've been redirected to Python, I hope!"
59    m.guard_output(msg)
60    stdout, stderr = capsys.readouterr()
61    assert stdout == msg
62    assert stderr == ''
63
64
65def test_series_captured(capture):
66    with capture:
67        m.captured_output("a")
68        m.captured_output("b")
69    assert capture == "ab"
70
71
72def test_flush(capfd):
73    msg = "(not flushed)"
74    msg2 = "(flushed)"
75
76    with m.ostream_redirect():
77        m.noisy_function(msg, flush=False)
78        stdout, stderr = capfd.readouterr()
79        assert stdout == ''
80
81        m.noisy_function(msg2, flush=True)
82        stdout, stderr = capfd.readouterr()
83        assert stdout == msg + msg2
84
85        m.noisy_function(msg, flush=False)
86
87    stdout, stderr = capfd.readouterr()
88    assert stdout == msg
89
90
91def test_not_captured(capfd):
92    msg = "Something that should not show up in log"
93    stream = StringIO()
94    with redirect_stdout(stream):
95        m.raw_output(msg)
96    stdout, stderr = capfd.readouterr()
97    assert stdout == msg
98    assert stderr == ''
99    assert stream.getvalue() == ''
100
101    stream = StringIO()
102    with redirect_stdout(stream):
103        m.captured_output(msg)
104    stdout, stderr = capfd.readouterr()
105    assert stdout == ''
106    assert stderr == ''
107    assert stream.getvalue() == msg
108
109
110def test_err(capfd):
111    msg = "Something that should not show up in log"
112    stream = StringIO()
113    with redirect_stderr(stream):
114        m.raw_err(msg)
115    stdout, stderr = capfd.readouterr()
116    assert stdout == ''
117    assert stderr == msg
118    assert stream.getvalue() == ''
119
120    stream = StringIO()
121    with redirect_stderr(stream):
122        m.captured_err(msg)
123    stdout, stderr = capfd.readouterr()
124    assert stdout == ''
125    assert stderr == ''
126    assert stream.getvalue() == msg
127
128
129def test_multi_captured(capfd):
130    stream = StringIO()
131    with redirect_stdout(stream):
132        m.captured_output("a")
133        m.raw_output("b")
134        m.captured_output("c")
135        m.raw_output("d")
136    stdout, stderr = capfd.readouterr()
137    assert stdout == 'bd'
138    assert stream.getvalue() == 'ac'
139
140
141def test_dual(capsys):
142    m.captured_dual("a", "b")
143    stdout, stderr = capsys.readouterr()
144    assert stdout == "a"
145    assert stderr == "b"
146
147
148def test_redirect(capfd):
149    msg = "Should not be in log!"
150    stream = StringIO()
151    with redirect_stdout(stream):
152        m.raw_output(msg)
153    stdout, stderr = capfd.readouterr()
154    assert stdout == msg
155    assert stream.getvalue() == ''
156
157    stream = StringIO()
158    with redirect_stdout(stream):
159        with m.ostream_redirect():
160            m.raw_output(msg)
161    stdout, stderr = capfd.readouterr()
162    assert stdout == ''
163    assert stream.getvalue() == msg
164
165    stream = StringIO()
166    with redirect_stdout(stream):
167        m.raw_output(msg)
168    stdout, stderr = capfd.readouterr()
169    assert stdout == msg
170    assert stream.getvalue() == ''
171
172
173def test_redirect_err(capfd):
174    msg = "StdOut"
175    msg2 = "StdErr"
176
177    stream = StringIO()
178    with redirect_stderr(stream):
179        with m.ostream_redirect(stdout=False):
180            m.raw_output(msg)
181            m.raw_err(msg2)
182    stdout, stderr = capfd.readouterr()
183    assert stdout == msg
184    assert stderr == ''
185    assert stream.getvalue() == msg2
186
187
188def test_redirect_both(capfd):
189    msg = "StdOut"
190    msg2 = "StdErr"
191
192    stream = StringIO()
193    stream2 = StringIO()
194    with redirect_stdout(stream):
195        with redirect_stderr(stream2):
196            with m.ostream_redirect():
197                m.raw_output(msg)
198                m.raw_err(msg2)
199    stdout, stderr = capfd.readouterr()
200    assert stdout == ''
201    assert stderr == ''
202    assert stream.getvalue() == msg
203    assert stream2.getvalue() == msg2
204