-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
284 lines (222 loc) · 5.1 KB
/
tests.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# To run this test:
# $ pytest -vvv --cov=. ./tests.py
import os
import pty
import subprocess
def py_(
args,
in_=None,
env=None,
want_out=b"",
want_err=b"",
want_returncode=0,
):
stdin = subprocess.PIPE
if in_ is None:
stdin = pty.openpty()[1]
else:
in_ = "".join(line + "\n" for line in in_).encode()
proc = subprocess.Popen(
["./py"] + args,
stdin=stdin,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
)
out, err = proc.communicate(in_)
assert out == want_out
assert err == want_err
assert proc.returncode == want_returncode
def test_noarg():
py_(
[],
want_err=b"""usage: py [-h] [-e] [-b] expr [expr ...]
py: error: the following arguments are required: expr
""",
want_returncode=2,
)
def test_help():
py_(
["-h"],
want_out=b"""usage: py [-h] [-e] [-b] expr [expr ...]
positional arguments:
expr Expression to apply to all inputs.
optional arguments:
-h, --help show this help message and exit
-e, --show-error Print raised exceptions. Default is to skip.
-b, --show-bool Print bool values. Default is to use bool values as a
filter.
""",
)
def test_simple():
py_(
["5 + 5"],
want_out=b"10\n",
)
def test_single_pipe_input():
py_(
["f'[[{x}]]'"],
in_=["foo"],
want_out=b"[[foo]]\n",
)
def test_multiple_pipe_input():
py_(
["f'[[{x}]]'"],
in_=["foo", "bar"],
want_out=b"[[foo]]\n[[bar]]\n",
)
def test_multiple_pipe_expr():
py_(
["int(x)", "x+5"],
in_=["2"],
want_out=b"7\n",
)
def test_implicit_code_value():
py_(
["int", "x+5"],
in_=["2"],
want_out=b"7\n",
)
def test_generators():
py_(
["int", "range", "sum"],
in_=["5"],
want_out=b"10\n",
)
def test_xargs():
py_(
["int", "xargs", "sorted"],
in_=["5", "7", "3", "4"],
want_out=b"[3, 4, 5, 7]\n",
)
def test_xargs_empty():
py_(
["xargs"],
want_out=b"[]\n",
)
def test_xargs_symbols():
py_(
["-e", "a=0", "b=x", "1", "xargs", "a"],
in_=["5", "7", "3", "4"],
want_out=b"0\n",
)
py_(
["-e", "a=0", "b=x", "1", "xargs", "b"],
in_=["5", "7", "3", "4"],
want_out=b"name 'b' is not defined\n",
want_returncode=1,
)
def test_unxargs():
py_(
["5", "range", "unxargs"],
want_out=b"0\n1\n2\n3\n4\n",
)
def test_unxargs_empty():
py_(
["unxargs"],
)
py_(
["unxargs"],
in_=[],
)
py_(
["5 / 0", "unxargs"],
want_returncode=1,
)
def test_none_passthrough():
py_(
["int", "xargs", "sort"],
in_=["5", "7", "3", "4"],
want_out=b"[3, 4, 5, 7]\n",
)
def test_bool_filter():
py_(
["int", "x > 4"],
in_=["5", "7", "3", "4"],
want_out=b"5\n7\n",
)
py_(
["-b", "int", "x > 4"],
in_=["5", "7", "3", "4"],
want_out=b"True\nTrue\nFalse\nFalse\n",
)
def test_auto_import():
py_(
["json.loads", "x['a']"],
in_=['{"a":3}'],
want_out=b"3\n",
)
def test_auto_import_submodule():
py_(
[
"email.message.Message()",
'x.set_param("key", "val")',
'x.set_payload("content")',
"x.as_string()",
],
want_out=b'Content-Type: text/plain; key="val"\n\ncontent\n',
)
def test_assignment():
py_(
["int", "k=1000", "x*k"],
in_=["9"],
want_out=b"9000\n",
)
def test_assignment_overwrite():
py_(
["int", "a=x", "a*x", "a=x", "a"],
in_=["3"],
want_out=b"9\n",
)
def test_undefined_symbol():
py_(
["foo"],
want_returncode=1,
)
py_(
["-e", "foo"],
want_out=b"name 'foo' is not defined\n",
want_returncode=1,
)
py_(
["-e", "foo"],
in_=["3", "4"],
want_out=b"name 'foo' is not defined\nname 'foo' is not defined\n",
want_returncode=1,
)
def test_undefined_attribute():
py_(
["email.message.spacerace"],
want_returncode=1,
)
def test_exception():
py_(
["5 / 0"],
want_returncode=1,
)
py_(
["-e", "5 / 0"],
want_out=b"division by zero\n",
want_returncode=1,
)
py_(
["int", "1 / x", "1 / x"],
in_=["0", "4", "8"],
want_out=b"4.0\n8.0\n",
want_returncode=1,
)
py_(
["-e", "int", "1 / x", "1 / x"],
in_=["0", "4", "8"],
want_out=b"division by zero\n4.0\n8.0\n",
want_returncode=1,
)
def test_user_symbols():
import tempfile
with tempfile.NamedTemporaryFile("w", suffix=".py") as file:
print("__symbols__={'foo': 'bar'}", file=file, flush=True)
py_(
["foo"],
env={**os.environ, "PY_SYMBOL_FILEPATHS": file.name},
want_out=b"bar\n",
)