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