/
test_redirectory.py
269 lines (203 loc) · 9.23 KB
/
test_redirectory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from __future__ import with_statement
import os
import sys
from tempfile import TemporaryFile, NamedTemporaryFile
from six import u
from six.moves import input, StringIO
from redirectory import (
stdin_from, stdout_to, stderr_to,
stdout_to_file,
stdin_fd_from_file,
stdout_fd_to_file,
stderr_fd_to_file,
redirect_file_obj,
)
temp_file_1 = TemporaryFile(mode='r+')
temp_file_1.write("one\ntwo\nthree\n")
temp_file_1.flush()
temp_file_1.seek(0)
temp_file_2 = TemporaryFile(mode='r+')
temp_file_2.write("four\nfive\nsix\n")
temp_file_2.flush()
temp_file_2.seek(0)
temp_file_3 = NamedTemporaryFile(mode='r')
#----------------------------------------------------------------------------
# redirect an arbitrary file object
#----------------------------------------------------------------------------
def test_redirect_fileobj_to_str():
target = 'test_redirectory.temp_file_1'
with redirect_file_obj(target, "ONE\nTWO\nTHREE\n"):
assert temp_file_1.readline() == 'ONE\n'
assert temp_file_1.readline() == 'TWO\n'
assert temp_file_1.readline() == 'THREE\n'
#----------------------------------------------------------------------------
# redirect an arbitrary file object
#----------------------------------------------------------------------------
def test_redirect_fileobj_to_fileobj():
target = 'test_redirectory.temp_file_1'
with redirect_file_obj(target, temp_file_2):
assert temp_file_1.readline() == 'four\n'
assert temp_file_1.readline() == 'five\n'
assert temp_file_1.readline() == 'six\n'
#----------------------------------------------------------------------------
# stdin_from with a StringIO as input
#----------------------------------------------------------------------------
def test_stdin_from_StringIO():
a_string = u("bleargh")
with stdin_from(StringIO(a_string)) as stringio:
input_return_value = input()
assert stringio.getvalue() == 'bleargh'
assert input_return_value == 'bleargh'
#----------------------------------------------------------------------------
# stdin_from with a str as input
#----------------------------------------------------------------------------
def test_stdin_from_str():
with stdin_from("some text") as stringio:
input_return_value = input()
assert stringio.getvalue() == 'some text'
assert input_return_value == 'some text'
#----------------------------------------------------------------------------
# stdin_from with a file as input
#----------------------------------------------------------------------------
def test_stdin_from_file():
with stdin_from(temp_file_1):
assert input() == 'one'
assert input() == 'two'
assert input() == 'three'
#----------------------------------------------------------------------------
# stdin_from with an iterable as input
#----------------------------------------------------------------------------
def test_stdin_from_iterable():
with stdin_from(['one', 'two', 'three']):
assert input() == 'one'
assert input() == 'two'
assert input() == 'three'
#----------------------------------------------------------------------------
# stdin_from with a generator function as input
#----------------------------------------------------------------------------
def test_stdin_from_generator_function():
def generator_func():
for word in ['one', 'two', 'three']:
yield word
with stdin_from(generator_func()):
assert input() == 'one'
assert input() == 'two'
assert input() == 'three'
#----------------------------------------------------------------------------
# stdin_from with a generator expression as input
#----------------------------------------------------------------------------
def test_stdin_from_generator_expression():
generator = (x for x in ['one', 'two', 'three'])
with stdin_from(generator):
assert input() == 'one'
assert input() == 'two'
assert input() == 'three'
#----------------------------------------------------------------------------
# stdout_to captures stdout to a StringIO
#----------------------------------------------------------------------------
def test_stdout_to_StringIO():
s = StringIO()
with stdout_to(s):
sys.stdout.write(u("hello"))
assert s.getvalue() == 'hello'
#----------------------------------------------------------------------------
# stdout_to captures stdout to a StringIO
#----------------------------------------------------------------------------
def test_stdout_to_as_StringIO():
with stdout_to() as s:
sys.stdout.write(u("hello"))
assert s.getvalue() == 'hello'
#----------------------------------------------------------------------------
# stdout_to_file captures stdout to a file; takes same args as open
#----------------------------------------------------------------------------
def test_stdout_to_file():
with stdout_to_file(temp_file_3.name):
sys.stdout.write(u("going to a file\n"))
with open(temp_file_3.name) as f:
assert f.read() == 'going to a file\n'
#----------------------------------------------------------------------------
# stdout_to_file appending to a file
#----------------------------------------------------------------------------
def test_stdout_to_file_append():
# Open file for writing
with stdout_to_file(temp_file_3.name, 'w'):
sys.stdout.write(u("1 - going to a file\n"))
# Open file for appending
with stdout_to_file(temp_file_3.name, 'a'):
sys.stdout.write(u("2 - going to a file\n"))
with open(temp_file_3.name) as f:
assert f.read() == '1 - going to a file\n2 - going to a file\n'
#----------------------------------------------------------------------------
# stderr_to captures stderr to a StringIO
#----------------------------------------------------------------------------
def test_stderr_to_StringIO():
s = StringIO()
with stderr_to(s):
sys.stderr.write(u("hello"))
assert s.getvalue() == 'hello'
#----------------------------------------------------------------------------
# stderr_to captures stderr to a StringIO
#----------------------------------------------------------------------------
def test_stderr_to_as_StringIO():
with stderr_to() as s:
sys.stderr.write(u("hello"))
assert s.getvalue() == 'hello'
#----------------------------------------------------------------------------
# Using stdout_to and stdin_from together
# stdout_to suppresses the prompt from `input`
# stdin_from with a str as input
#----------------------------------------------------------------------------
def test_stdout_to_and_stdin():
with stdout_to():
with stdin_from("bleargh") as stringio:
input_return_value = input(u("Type something> "))
assert stringio.getvalue() == 'bleargh'
assert input_return_value == 'bleargh'
#----------------------------------------------------------------------------
# stdin_fd_from_file feeds stdin from a file,
# including for subprocesses
#----------------------------------------------------------------------------
def test_stdin_fd_from_file():
with NamedTemporaryFile() as temp_file_in:
with NamedTemporaryFile() as temp_file_out:
temp_file_in.write("print(23 * 2)\n".encode("ascii"))
temp_file_in.flush()
with stdin_fd_from_file(temp_file_in.name):
with stdout_fd_to_file(temp_file_out.name):
os.system('python')
with open(temp_file_out.name) as f:
assert f.read() == '46\n'
#----------------------------------------------------------------------------
# stdout_fd_to_file captures stdout to a file,
# including for subprocesses
#----------------------------------------------------------------------------
def test_stdout_fd_to_file():
with NamedTemporaryFile() as temp_file:
with stdout_fd_to_file(temp_file.name):
os.system('echo "*** Hello there ***"')
with open(temp_file.name) as f:
assert f.read() == '*** Hello there ***\n'
#----------------------------------------------------------------------------
# stdout_fd_to_file captures stdout to a file (os.devnull),
# including for subprocesses
#----------------------------------------------------------------------------
def test_stdout_fd_to_os_devnull():
with stdout_fd_to_file(os.devnull):
os.system('echo "*** Hello there ***"')
#----------------------------------------------------------------------------
# stderr_fd_to_file captures stderr to a file,
# including for subprocesses
#----------------------------------------------------------------------------
def test_stderr_fd_to_file():
with NamedTemporaryFile() as temp_file:
with stderr_fd_to_file(temp_file.name):
os.system('echo "*** Hello there ***" 1>&2')
with open(temp_file.name) as f:
assert f.read() == '*** Hello there ***\n'
#----------------------------------------------------------------------------
# stderr_fd_to_file captures stderr to a file (os.devnull),
# including for subprocesses
#----------------------------------------------------------------------------
def test_stderr_fd_to_os_devnull():
with stderr_fd_to_file(os.devnull):
os.system('echo "*** Hello there ***" 1>&2')