blob: 167b887565acbf883c76bfc92eb381a2514c45af [file] [log] [blame]
Henry Schreiner8b405052017-08-24 17:12:43 -07001from 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
Justin Bassett2cbafb02018-08-29 02:48:30 -070056def test_captured_large_string(capsys):
57 # Make this bigger than the buffer used on the C++ side: 1024 chars
58 msg = "I've been redirected to Python, I hope!"
59 msg = msg * (1024 // len(msg) + 1)
60
61 m.captured_output_default(msg)
62 stdout, stderr = capsys.readouterr()
63 assert stdout == msg
64 assert stderr == ''
65
Henry Schreiner8b405052017-08-24 17:12:43 -070066
67def test_guard_capture(capsys):
68 msg = "I've been redirected to Python, I hope!"
69 m.guard_output(msg)
70 stdout, stderr = capsys.readouterr()
71 assert stdout == msg
72 assert stderr == ''
73
74
75def test_series_captured(capture):
76 with capture:
77 m.captured_output("a")
78 m.captured_output("b")
79 assert capture == "ab"
80
81
82def test_flush(capfd):
83 msg = "(not flushed)"
84 msg2 = "(flushed)"
85
86 with m.ostream_redirect():
87 m.noisy_function(msg, flush=False)
88 stdout, stderr = capfd.readouterr()
89 assert stdout == ''
90
91 m.noisy_function(msg2, flush=True)
92 stdout, stderr = capfd.readouterr()
93 assert stdout == msg + msg2
94
95 m.noisy_function(msg, flush=False)
96
97 stdout, stderr = capfd.readouterr()
98 assert stdout == msg
99
100
101def test_not_captured(capfd):
102 msg = "Something that should not show up in log"
103 stream = StringIO()
104 with redirect_stdout(stream):
105 m.raw_output(msg)
106 stdout, stderr = capfd.readouterr()
107 assert stdout == msg
108 assert stderr == ''
109 assert stream.getvalue() == ''
110
111 stream = StringIO()
112 with redirect_stdout(stream):
113 m.captured_output(msg)
114 stdout, stderr = capfd.readouterr()
115 assert stdout == ''
116 assert stderr == ''
117 assert stream.getvalue() == msg
118
119
120def test_err(capfd):
121 msg = "Something that should not show up in log"
122 stream = StringIO()
123 with redirect_stderr(stream):
124 m.raw_err(msg)
125 stdout, stderr = capfd.readouterr()
126 assert stdout == ''
127 assert stderr == msg
128 assert stream.getvalue() == ''
129
130 stream = StringIO()
131 with redirect_stderr(stream):
132 m.captured_err(msg)
133 stdout, stderr = capfd.readouterr()
134 assert stdout == ''
135 assert stderr == ''
136 assert stream.getvalue() == msg
137
138
139def test_multi_captured(capfd):
140 stream = StringIO()
141 with redirect_stdout(stream):
142 m.captured_output("a")
143 m.raw_output("b")
144 m.captured_output("c")
145 m.raw_output("d")
146 stdout, stderr = capfd.readouterr()
147 assert stdout == 'bd'
148 assert stream.getvalue() == 'ac'
149
150
151def test_dual(capsys):
152 m.captured_dual("a", "b")
153 stdout, stderr = capsys.readouterr()
154 assert stdout == "a"
155 assert stderr == "b"
156
157
158def test_redirect(capfd):
159 msg = "Should not be in log!"
160 stream = StringIO()
161 with redirect_stdout(stream):
162 m.raw_output(msg)
163 stdout, stderr = capfd.readouterr()
164 assert stdout == msg
165 assert stream.getvalue() == ''
166
167 stream = StringIO()
168 with redirect_stdout(stream):
169 with m.ostream_redirect():
170 m.raw_output(msg)
171 stdout, stderr = capfd.readouterr()
172 assert stdout == ''
173 assert stream.getvalue() == msg
174
175 stream = StringIO()
176 with redirect_stdout(stream):
177 m.raw_output(msg)
178 stdout, stderr = capfd.readouterr()
179 assert stdout == msg
180 assert stream.getvalue() == ''
181
182
183def test_redirect_err(capfd):
184 msg = "StdOut"
185 msg2 = "StdErr"
186
187 stream = StringIO()
188 with redirect_stderr(stream):
189 with m.ostream_redirect(stdout=False):
190 m.raw_output(msg)
191 m.raw_err(msg2)
192 stdout, stderr = capfd.readouterr()
193 assert stdout == msg
194 assert stderr == ''
195 assert stream.getvalue() == msg2
196
197
198def test_redirect_both(capfd):
199 msg = "StdOut"
200 msg2 = "StdErr"
201
202 stream = StringIO()
203 stream2 = StringIO()
204 with redirect_stdout(stream):
205 with redirect_stderr(stream2):
206 with m.ostream_redirect():
207 m.raw_output(msg)
208 m.raw_err(msg2)
209 stdout, stderr = capfd.readouterr()
210 assert stdout == ''
211 assert stderr == ''
212 assert stream.getvalue() == msg
213 assert stream2.getvalue() == msg2