-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_specialist.py
414 lines (354 loc) · 18.3 KB
/
test_specialist.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""Tests for the Specialist command-line tool."""
import contextlib
import pathlib
import runpy
import sys
import typing
import unittest.mock
import urllib.request
import pytest
import specialist
OUTPUT_PATTERN = f"output-*-{sys.version_info[0]}-{sys.version_info[1]}.html"
TEST_DATA = pathlib.Path(__file__).parent / "test-data"
TESTS = list(
zip(
sorted((TEST_DATA / "input").glob("input-*.py")),
sorted((TEST_DATA / "output").glob(OUTPUT_PATTERN)),
strict=True,
)
)
TESTS_BLUE = list(
zip(
sorted((TEST_DATA / "input").glob("input-*.py")),
sorted((TEST_DATA / "output-blue").glob(OUTPUT_PATTERN)),
strict=True,
)
)
TESTS_DARK = list(
zip(
sorted((TEST_DATA / "input").glob("input-*.py")),
sorted((TEST_DATA / "output-dark").glob(OUTPUT_PATTERN)),
strict=True,
)
)
TESTS_DARK_BLUE = list(
zip(
sorted((TEST_DATA / "input").glob("input-*.py")),
sorted((TEST_DATA / "output-dark-blue").glob(OUTPUT_PATTERN)),
strict=True,
)
)
@contextlib.contextmanager
def assert_browses(
expected: typing.Sequence[str],
) -> typing.Generator[None, None, None]:
"""Patch webbrowser.open_new_tab, and assert that it browses the expected output."""
expected_iter = iter(expected)
def open_new_tab(url: str) -> None:
with urllib.request.urlopen(url, timeout=1) as actual:
assert next(expected_iter) == actual.read().decode("utf-8")
with unittest.mock.patch("webbrowser.open_new_tab", open_new_tab):
yield
assert next(expected_iter, None) is None
@contextlib.contextmanager
def without_tracing() -> typing.Generator[None, None, None]:
"""Context manager that disables tracing and profiling."""
trace = sys.gettrace()
profile = sys.getprofile()
try:
sys.settrace(None)
sys.setprofile(None)
yield
finally:
sys.settrace(trace)
sys.setprofile(profile)
def no_trace_main(args: list[str]) -> None:
"""Run specialist.main without tracing or profiling during runpy calls."""
run_module = without_tracing()(runpy.run_module) # pylint: disable = not-callable
run_path = without_tracing()(runpy.run_path) # pylint: disable = not-callable
with (
unittest.mock.patch("runpy.run_module", run_module),
unittest.mock.patch("runpy.run_path", run_path),
):
specialist.main(args)
@pytest.mark.parametrize("source, expected", TESTS)
def test_main(source: pathlib.Path, expected: pathlib.Path) -> None:
"""$ specialist <source>"""
with assert_browses([expected.read_text()]):
no_trace_main([str(source)])
@pytest.mark.parametrize("source, expected", TESTS_BLUE)
def test_main_blue(source: pathlib.Path, expected: pathlib.Path) -> None:
"""$ specialist --blue <file>"""
with assert_browses([expected.read_text()]):
no_trace_main(["--blue", str(source)])
@pytest.mark.parametrize("source, expected", TESTS)
def test_main_c(source: pathlib.Path, expected: pathlib.Path) -> None:
"""$ specialist -c ..."""
with assert_browses([expected.read_text()]):
no_trace_main(["-c", source.read_text()])
def test_main_c_raises() -> None:
"""$ specialist -c '[i * i for i in range(100)]; 42 / 0'"""
if sys.version_info < (3, 12):
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#d4fed4'>[</span><span style='backgro"
"und-color:#91fe91'>i</span><span style='background-color:#b0ffb0'> * </spa"
"n><span style='background-color:#91fe91'>i</span><span style='background-c"
"olor:#d4fed4'> for </span><span style='background-color:#b0ffb0'>i</span><"
"span style='background-color:#d4fed4'> in </span><span style='background-c"
"olor:#dfffdf'>range</span><span style='background-color:#daffda'>(</span><"
"span style='background-color:#ddffdd'>100</span><span style='background-co"
"lor:#daffda'>)</span><span style='background-color:#d4fed4'>]</span>; 42 /"
" 0</pre></body></html>"
)
elif sys.version_info < (3, 13):
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#d7ffd7'>[</span><span style='backgro"
"und-color:#a4ffa4'>i</span><span style='background-color:#bfffbf'> * </spa"
"n><span style='background-color:#a4ffa4'>i</span><span style='background-c"
"olor:#d7ffd7'> for </span><span style='background-color:#b6ffb6'>i</span><"
"span style='background-color:#d7ffd7'> in </span><span style='background-c"
"olor:#dfffdf'>range</span><span style='background-color:#daffda'>(</span><"
"span style='background-color:#ddffdd'>100</span><span style='background-co"
"lor:#daffda'>)</span><span style='background-color:#d7ffd7'>]</span>; 42 /"
" 0</pre></body></html>"
)
else:
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#d7ffd7'>[</span><span style='backgro"
"und-color:#bfffbf'>i * </span><span style='background-color:#c3ffc3'>i</sp"
"an><span style='background-color:#d7ffd7'> for </span><span style='backgro"
"und-color:#daffda'>i</span><span style='background-color:#d7ffd7'> in </sp"
"an><span style='background-color:#dfffdf'>range</span><span style='backgro"
"und-color:#daffda'>(</span><span style='background-color:#ddffdd'>100</spa"
"n><span style='background-color:#daffda'>)</span><span style='background-c"
"olor:#d7ffd7'>]</span>; 42 / 0</pre></body></html>"
)
with assert_browses([expected]), pytest.raises(ZeroDivisionError):
no_trace_main(["-c", "[i * i for i in range(100)]; 42 / 0"])
@pytest.mark.parametrize("source, expected", TESTS_DARK)
def test_main_dark(source: pathlib.Path, expected: pathlib.Path) -> None:
"""$ specialist --dark <file>"""
with assert_browses([expected.read_text()]):
no_trace_main(["--dark", str(source)])
@pytest.mark.parametrize("source, expected", TESTS_DARK_BLUE)
def test_main_dark_blue(source: pathlib.Path, expected: pathlib.Path) -> None:
"""$ specialist --dark --blue <file>"""
with assert_browses([expected.read_text()]):
no_trace_main(["--dark", "--blue", str(source)])
@pytest.mark.parametrize("source, expected", TESTS)
def test_main_m(source: pathlib.Path, expected: pathlib.Path) -> None:
"""$ specialist -m <source>"""
module = ".".join(source.with_suffix("").relative_to(pathlib.Path.cwd()).parts)
with assert_browses([expected.read_text()]):
no_trace_main(["-m", module])
def test_main_m_raises_a() -> None:
"""$ specialist -m __hello__"""
no_trace_main(["-m", "__hello__"])
def test_main_m_raises_b() -> None:
"""$ specialist -m __phello__"""
with pytest.raises(ImportError):
no_trace_main(["-m", "__phello__"])
def test_main_m_raises_c() -> None:
"""$ specialist -m __hello__"""
with pytest.raises(ImportError):
no_trace_main(["-m", "__phello__.nonexistent"])
def test_main_no_location() -> None:
"""$ specialist -c 'def g(): yield from range(100)\nlist(g())'"""
if sys.version_info < (3, 12):
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre>def g(): <span style='background-color:#d4fed4'>yield from </span><"
"span style='background-color:#ddffdd'>range</span><span style='background-"
"color:#daffda'>(</span><span style='background-color:#ddffdd'>100</span><s"
"pan style='background-color:#daffda'>)</span>\nlist(g())</pre></body></htm"
"l>"
)
elif sys.version_info < (3, 13):
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre>def g(): <span style='background-color:#ffd7d7'>yield from </span><"
"span style='background-color:#ffdddd'>range</span><span style='background-"
"color:#ffdada'>(</span><span style='background-color:#ffdddd'>100</span><s"
"pan style='background-color:#ffdada'>)</span>\nlist(g())</pre></body></htm"
"l>"
)
else:
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre>def g(): <span style='background-color:#ffdada'>yield from </span><"
"span style='background-color:#ffdfdf'>range</span><span style='background-"
"color:#ffdddd'>(</span><span style='background-color:#ffdfdf'>100</span><s"
"pan style='background-color:#ffdddd'>)</span>\nlist(g())</pre></body></htm"
"l>"
)
with assert_browses([expected]):
no_trace_main(["-c", "def g(): yield from range(100)\nlist(g())"])
def test_main_no_quickened_code_found() -> None:
"""$ specialist -c 'pass'"""
no_trace_main(["-c", "pass"])
@pytest.mark.parametrize("source", [source for source, _ in TESTS])
def test_main_no_quickened_code_found_suggestion(source: pathlib.Path) -> None:
"""$ specialist -c ..."""
no_trace_main(
["-c", f'import runpy; runpy.run_path("{source}", run_name="__main__")']
)
def test_main_no_quickened_code_found_suggestions() -> None:
"""$ specialist -c ..."""
lines = ["import runpy"]
lines.extend(
f'runpy.run_path("{source}", run_name="__main__")' for source, _ in TESTS
)
no_trace_main(["-c", "\n".join(lines)])
def test_main_no_quickened_code_found_suggestions_deep() -> None:
"""$ specialist -c ..."""
input_package = TEST_DATA / "input-package"
lines = ["import runpy"]
lines.append(f'runpy.run_path("{input_package}", run_name="__main__")')
lines.extend(
f'runpy.run_path("{source}", run_name="__main__")' for source, _ in TESTS
)
no_trace_main(["-c", "\n".join(lines)])
@pytest.mark.parametrize("source, expected", TESTS)
def test_main_output(
source: pathlib.Path, expected: pathlib.Path, tmp_path: pathlib.Path
) -> None:
"""$ specialist --output <tmp_path> <source>"""
no_trace_main(["--output", str(tmp_path), str(source)])
children = list(tmp_path.iterdir())
assert len(children) == 1
assert children[0].read_text() == expected.read_text()
@pytest.mark.parametrize("source", [source for source, _ in TESTS])
def test_main_targets_missing(source: pathlib.Path) -> None:
"""$ specialist --targets XXX <source>"""
no_trace_main(["--targets", "XXX", str(source)])
@pytest.mark.parametrize("source, expected", TESTS)
def test_main_targets(source: pathlib.Path, expected: pathlib.Path) -> None:
"""$ specialist --targets <source> <source>"""
targets = source.relative_to(pathlib.Path.cwd())
with assert_browses([expected.read_text()]):
no_trace_main(["--targets", str(targets), str(source)])
def test_main_targets_output_c(tmp_path: pathlib.Path) -> None:
"""$ specialist --targets 'test-data/input/*' -c 'pass'"""
lines = ["import runpy"]
lines.extend(
f'runpy.run_path("{source}", run_name="__main__")' for source, _ in TESTS
)
no_trace_main(
[
"--targets",
"test-data/input/*",
"--output",
str(tmp_path),
"-c",
"\n".join(lines),
]
)
for actual, expected in zip(
sorted(tmp_path.iterdir()),
sorted((TEST_DATA / "output").glob(OUTPUT_PATTERN)),
strict=True,
):
assert actual.read_text() == expected.read_text()
def test_main_leading_and_trailing_whitespace() -> None:
"""$ specialist -c 'pass'"""
if sys.version_info < (3, 12):
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#daffda'>[</span><span style='backgro"
"und-color:#9fff9f'>i</span> \n<span style='background-color:#bbffbb'>*</sp"
"an>\n <span style='background-color:#9fff9f'>i</span><span style='backgrou"
"nd-color:#daffda'> for </span><span style='background-color:#bbffbb'>i</sp"
"an><span style='background-color:#daffda'> in </span><span style='backgrou"
"nd-color:#e2ffe2'>range</span><span style='background-color:#dfffdf'>(</sp"
"an><span style='background-color:#e1ffe1'>100</span><span style='backgroun"
"d-color:#dfffdf'>)</span><span style='background-color:#daffda'>]</span></"
"pre></body></html>"
)
elif sys.version_info < (3, 13):
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#daffda'>[</span><span style='backgro"
"und-color:#aaffaa'>i</span> \n<span style='background-color:#c3ffc3'>*</sp"
"an>\n <span style='background-color:#aaffaa'>i</span><span style='backgrou"
"nd-color:#daffda'> for </span><span style='background-color:#bbffbb'>i</sp"
"an><span style='background-color:#daffda'> in </span><span style='backgrou"
"nd-color:#e1ffe1'>range</span><span style='background-color:#ddffdd'>(</sp"
"an><span style='background-color:#dfffdf'>100</span><span style='backgroun"
"d-color:#ddffdd'>)</span><span style='background-color:#daffda'>]</span></"
"pre></body></html>"
)
else:
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#daffda'>[</span><span style='backgro"
"und-color:#c6ffc6'>i</span> \n<span style='background-color:#c3ffc3'>*</sp"
"an>\n <span style='background-color:#c6ffc6'>i</span><span style='backgrou"
"nd-color:#daffda'> for </span><span style='background-color:#ddffdd'>i</sp"
"an><span style='background-color:#daffda'> in </span><span style='backgrou"
"nd-color:#e1ffe1'>range</span><span style='background-color:#ddffdd'>(</sp"
"an><span style='background-color:#dfffdf'>100</span><span style='backgroun"
"d-color:#ddffdd'>)</span><span style='background-color:#daffda'>]</span></"
"pre></body></html>"
)
with assert_browses([expected]):
no_trace_main(["-c", "[i \n*\n i for i in range(100)]"])
def test_main_package() -> None:
"""$ specialist -m test-data.input-package"""
if sys.version_info < (3, 12):
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#daffda'>[</span><span style='backgro"
"und-color:#9fff9f'>i</span><span style='background-color:#bbffbb'> * </spa"
"n><span style='background-color:#9fff9f'>i</span><span style='background-c"
"olor:#daffda'> for </span><span style='background-color:#bbffbb'>i</span><"
"span style='background-color:#daffda'> in </span><span style='background-c"
"olor:#e2ffe2'>range</span><span style='background-color:#dfffdf'>(</span><"
"span style='background-color:#e1ffe1'>100</span><span style='background-co"
"lor:#dfffdf'>)</span><span style='background-color:#daffda'>]</span>\n</pr"
"e></body></html>"
)
elif sys.version_info < (3, 13):
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#daffda'>[</span><span style='backgro"
"und-color:#aaffaa'>i</span><span style='background-color:#c3ffc3'> * </spa"
"n><span style='background-color:#aaffaa'>i</span><span style='background-c"
"olor:#daffda'> for </span><span style='background-color:#bbffbb'>i</span><"
"span style='background-color:#daffda'> in </span><span style='background-c"
"olor:#e1ffe1'>range</span><span style='background-color:#ddffdd'>(</span><"
"span style='background-color:#dfffdf'>100</span><span style='background-co"
"lor:#ddffdd'>)</span><span style='background-color:#daffda'>]</span>\n</pr"
"e></body></html>"
)
else:
expected = (
"<!doctype html><html><head><meta http-equiv='content-type' content='text/h"
"tml;charset=utf-8'/></head><body style='background-color:white;color:black"
"'><pre><span style='background-color:#daffda'>[</span><span style='backgro"
"und-color:#c3ffc3'>i * </span><span style='background-color:#c6ffc6'>i</sp"
"an><span style='background-color:#daffda'> for </span><span style='backgro"
"und-color:#ddffdd'>i</span><span style='background-color:#daffda'> in </sp"
"an><span style='background-color:#e1ffe1'>range</span><span style='backgro"
"und-color:#ddffdd'>(</span><span style='background-color:#dfffdf'>100</spa"
"n><span style='background-color:#ddffdd'>)</span><span style='background-c"
"olor:#daffda'>]</span>\n</pre></body></html>"
)
module = ".".join(
(TEST_DATA / "input-package").relative_to(pathlib.Path.cwd()).parts
)
with assert_browses([expected]):
no_trace_main(["-m", module])