-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtest_execute.py
411 lines (341 loc) · 14.4 KB
/
test_execute.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
"""Test sphinx builds which execute notebooks."""
import os
from pathlib import Path
from IPython import version_info as ipy_version
import pytest
from myst_nb.core.execute import ExecutionError
from myst_nb.sphinx_ import NbMetadataCollector
def regress_nb_doc(file_regression, sphinx_run, check_nbs):
try:
file_regression.check(
sphinx_run.get_nb(),
check_fn=check_nbs,
extension=".ipynb",
encoding="utf-8",
)
finally:
doctree_string = sphinx_run.get_doctree().pformat()
# TODO this is a difference in the hashing on the CI,
# with complex_outputs_unrun.ipynb equation PNG, after execution
# sympy
doctree_string = doctree_string.replace(
"91b3db0f47514d451e7c0f5a501d31c4e101b7112050634ad0788405f417782a",
"e2dfbe330154316cfb6f3186e8f57fc4df8aee03b0303ed1345fc22cd51f66de",
)
doctree_string = doctree_string.replace(
"438c56ea3dcf99d86cd64df1b23e2b436afb25846434efb1cfec7b660ef01127",
"e2dfbe330154316cfb6f3186e8f57fc4df8aee03b0303ed1345fc22cd51f66de",
)
if os.name == "nt": # on Windows image file paths are absolute
doctree_string = doctree_string.replace(
Path(sphinx_run.app.srcdir).as_posix() + "/", ""
)
file_regression.check(doctree_string, extension=".xml", encoding="utf-8")
@pytest.mark.sphinx_params("basic_unrun.ipynb", conf={"nb_execution_mode": "auto"})
def test_basic_unrun_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
assert "test_name" in sphinx_run.app.env.metadata["basic_unrun"]
regress_nb_doc(file_regression, sphinx_run, check_nbs)
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "basic_unrun")
assert data
assert data["method"] == "auto"
assert data["succeeded"] is True
@pytest.mark.sphinx_params("basic_unrun.ipynb", conf={"nb_execution_mode": "cache"})
def test_basic_unrun_cache(sphinx_run, file_regression, check_nbs):
"""The outputs should be populated."""
sphinx_run.build()
assert sphinx_run.warnings() == ""
assert "test_name" in sphinx_run.app.env.metadata["basic_unrun"]
regress_nb_doc(file_regression, sphinx_run, check_nbs)
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "basic_unrun")
assert data
assert data["method"] == "cache"
assert data["succeeded"] is True
@pytest.mark.sphinx_params("basic_unrun.ipynb", conf={"nb_execution_mode": "inline"})
def test_basic_unrun_inline(sphinx_run, file_regression, check_nbs):
"""The outputs should be populated."""
sphinx_run.build()
assert sphinx_run.warnings() == ""
assert "test_name" in sphinx_run.app.env.metadata["basic_unrun"]
regress_nb_doc(file_regression, sphinx_run, check_nbs)
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "basic_unrun")
assert data
assert data["method"] == "inline"
assert data["succeeded"] is True
@pytest.mark.sphinx_params("basic_unrun.ipynb", conf={"nb_execution_mode": "cache"})
def test_rebuild_cache(sphinx_run):
"""The notebook should only be executed once."""
sphinx_run.build()
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
sphinx_run.invalidate_files()
sphinx_run.build()
assert "Using cached" in sphinx_run.status()
@pytest.mark.sphinx_params("basic_unrun.ipynb", conf={"nb_execution_mode": "force"})
def test_rebuild_force(sphinx_run):
"""The notebook should be executed twice."""
sphinx_run.build()
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
sphinx_run.invalidate_files()
sphinx_run.build()
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
@pytest.mark.sphinx_params(
"basic_unrun.ipynb",
conf={
"nb_execution_mode": "cache",
"nb_execution_excludepatterns": ["basic_*"],
},
)
def test_exclude_path(sphinx_run, file_regression):
"""The notebook should not be executed."""
sphinx_run.build()
assert not NbMetadataCollector.new_exec_data(sphinx_run.env)
assert "Executing" not in sphinx_run.status(), sphinx_run.status()
file_regression.check(
sphinx_run.get_doctree().pformat(), extension=".xml", encoding="utf-8"
)
@pytest.mark.skipif(ipy_version[0] < 8, reason="Error message changes for ipython v8")
@pytest.mark.sphinx_params("basic_failing.ipynb", conf={"nb_execution_mode": "cache"})
def test_basic_failing_cache(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.warnings())
assert "Executing notebook failed" in sphinx_run.warnings()
regress_nb_doc(file_regression, sphinx_run, check_nbs)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "basic_failing")
assert data
assert data["method"] == "cache"
assert data["succeeded"] is False
sphinx_run.get_report_file()
@pytest.mark.skipif(ipy_version[0] < 8, reason="Error message changes for ipython v8")
@pytest.mark.sphinx_params("basic_failing.ipynb", conf={"nb_execution_mode": "auto"})
def test_basic_failing_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
assert "Executing notebook failed" in sphinx_run.warnings()
regress_nb_doc(file_regression, sphinx_run, check_nbs)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "basic_failing")
assert data
assert data["method"] == "auto"
assert data["succeeded"] is False
assert data["traceback"]
sphinx_run.get_report_file()
@pytest.mark.skipif(ipy_version[0] < 8, reason="Error message changes for ipython v8")
@pytest.mark.sphinx_params("basic_failing.ipynb", conf={"nb_execution_mode": "inline"})
def test_basic_failing_inline(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
assert "Executing notebook failed" in sphinx_run.warnings()
regress_nb_doc(file_regression, sphinx_run, check_nbs)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "basic_failing")
assert data
assert data["method"] == "inline"
assert data["succeeded"] is False
assert data["traceback"]
sphinx_run.get_report_file()
@pytest.mark.skipif(ipy_version[0] < 8, reason="Error message changes for ipython v8")
@pytest.mark.sphinx_params(
"basic_failing.ipynb",
conf={"nb_execution_mode": "cache", "nb_execution_allow_errors": True},
)
def test_allow_errors_cache(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert not sphinx_run.warnings()
regress_nb_doc(file_regression, sphinx_run, check_nbs)
@pytest.mark.skipif(ipy_version[0] < 8, reason="Error message changes for ipython v8")
@pytest.mark.sphinx_params(
"basic_failing.ipynb",
conf={"nb_execution_mode": "auto", "nb_execution_allow_errors": True},
)
def test_allow_errors_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert not sphinx_run.warnings()
regress_nb_doc(file_regression, sphinx_run, check_nbs)
@pytest.mark.sphinx_params(
"basic_failing.ipynb",
conf={"nb_execution_raise_on_error": True, "nb_execution_mode": "force"},
)
def test_raise_on_error_force(sphinx_run):
with pytest.raises(ExecutionError, match="basic_failing.ipynb"):
sphinx_run.build()
@pytest.mark.sphinx_params(
"basic_failing.ipynb",
conf={"nb_execution_raise_on_error": True, "nb_execution_mode": "cache"},
)
def test_raise_on_error_cache(sphinx_run):
with pytest.raises(ExecutionError, match="basic_failing.ipynb"):
sphinx_run.build()
@pytest.mark.sphinx_params(
"complex_outputs_unrun.ipynb", conf={"nb_execution_mode": "cache"}
)
def test_complex_outputs_unrun_cache(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
regress_nb_doc(file_regression, sphinx_run, check_nbs)
# Widget view and widget state should make it into the HTML
scripts = sphinx_run.get_html().select("script")
assert any(
"application/vnd.jupyter.widget-view+json" in script.get("type", "")
for script in scripts
)
assert any(
"application/vnd.jupyter.widget-state+json" in script.get("type", "")
for script in scripts
)
@pytest.mark.sphinx_params(
"complex_outputs_unrun.ipynb", conf={"nb_execution_mode": "auto"}
)
def test_complex_outputs_unrun_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
regress_nb_doc(file_regression, sphinx_run, check_nbs)
# Widget view and widget state should make it into the HTML
scripts = sphinx_run.get_html().select("script")
assert any(
"application/vnd.jupyter.widget-view+json" in script.get("type", "")
for script in scripts
)
assert any(
"application/vnd.jupyter.widget-state+json" in script.get("type", "")
for script in scripts
)
@pytest.mark.sphinx_params("basic_unrun.ipynb", conf={"nb_execution_mode": "off"})
def test_no_execute(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
regress_nb_doc(file_regression, sphinx_run, check_nbs)
@pytest.mark.sphinx_params("basic_unrun.ipynb", conf={"nb_execution_mode": "cache"})
def test_jupyter_cache_path(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
assert "Cached executed notebook" in sphinx_run.status()
assert sphinx_run.warnings() == ""
regress_nb_doc(file_regression, sphinx_run, check_nbs)
# Testing relative paths within the notebook
@pytest.mark.sphinx_params("basic_relative.ipynb", conf={"nb_execution_mode": "cache"})
def test_relative_path_cache(sphinx_run):
sphinx_run.build()
assert "Execution Failed" not in sphinx_run.status(), sphinx_run.status()
@pytest.mark.sphinx_params("basic_relative.ipynb", conf={"nb_execution_mode": "force"})
def test_relative_path_force(sphinx_run):
sphinx_run.build()
assert "Execution Failed" not in sphinx_run.status(), sphinx_run.status()
@pytest.mark.sphinx_params(
"kernel_alias.md",
conf={"nb_execution_mode": "force", "nb_kernel_rgx_aliases": {"oth.+": "python3"}},
)
def test_kernel_rgx_aliases(sphinx_run):
sphinx_run.build()
assert sphinx_run.warnings() == ""
@pytest.mark.sphinx_params(
"sleep_10.ipynb",
conf={"nb_execution_mode": "cache", "nb_execution_timeout": 1},
)
def test_execution_timeout(sphinx_run):
"""execution should fail given the low timeout value"""
sphinx_run.build()
# print(sphinx_run.warnings())
assert "Executing notebook failed" in sphinx_run.warnings()
@pytest.mark.sphinx_params(
"sleep_10_metadata_timeout.ipynb",
conf={"nb_execution_mode": "cache", "nb_execution_timeout": 60},
)
def test_execution_metadata_timeout(sphinx_run):
"""notebook timeout metadata has higher preference then execution_timeout config"""
sphinx_run.build()
# print(sphinx_run.warnings())
assert "Executing notebook failed" in sphinx_run.warnings()
@pytest.mark.sphinx_params(
"nb_exec_table.md",
conf={"nb_execution_mode": "auto"},
)
def test_nb_exec_table(sphinx_run, file_regression):
"""Test that the table gets output into the HTML,
including a row for the executed notebook.
"""
sphinx_run.build()
# print(sphinx_run.status())
assert not sphinx_run.warnings()
file_regression.check(
sphinx_run.get_doctree().pformat(), extension=".xml", encoding="utf-8"
)
# print(sphinx_run.get_html())
rows = sphinx_run.get_html().select("table.docutils tr")
assert any("nb_exec_table" in row.text for row in rows)
@pytest.mark.sphinx_params(
"custom-formats.Rmd",
conf={
"nb_execution_mode": "auto",
"nb_custom_formats": {".Rmd": ["jupytext.reads", {"fmt": "Rmd"}]},
},
)
def test_custom_convert_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
regress_nb_doc(file_regression, sphinx_run, check_nbs)
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "custom-formats")
assert data
assert data["method"] == "auto"
assert data["succeeded"] is True
@pytest.mark.sphinx_params(
"custom-formats.Rmd",
conf={
"nb_execution_mode": "cache",
"nb_custom_formats": {".Rmd": ["jupytext.reads", {"fmt": "Rmd"}]},
},
)
def test_custom_convert_cache(sphinx_run, file_regression, check_nbs):
"""The outputs should be populated."""
sphinx_run.build()
assert sphinx_run.warnings() == ""
regress_nb_doc(file_regression, sphinx_run, check_nbs)
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "custom-formats")
assert data
assert data["method"] == "cache"
assert data["succeeded"] is True
@pytest.mark.sphinx_params(
"custom-formats2.extra.exnt",
conf={
"nb_execution_mode": "auto",
"nb_custom_formats": {".extra.exnt": ["jupytext.reads", {"fmt": "Rmd"}]},
},
)
def test_custom_convert_multiple_extensions_auto(
sphinx_run, file_regression, check_nbs
):
"""The outputs should be populated."""
sphinx_run.build()
assert sphinx_run.warnings() == ""
regress_nb_doc(file_regression, sphinx_run, check_nbs)
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "custom-formats2")
assert data
assert data["method"] == "auto"
assert data["succeeded"] is True
@pytest.mark.sphinx_params(
"custom-formats2.extra.exnt",
conf={
"nb_execution_mode": "cache",
"nb_custom_formats": {".extra.exnt": ["jupytext.reads", {"fmt": "Rmd"}]},
},
)
def test_custom_convert_multiple_extensions_cache(
sphinx_run, file_regression, check_nbs
):
"""The outputs should be populated."""
sphinx_run.build()
assert sphinx_run.warnings() == ""
regress_nb_doc(file_regression, sphinx_run, check_nbs)
assert NbMetadataCollector.new_exec_data(sphinx_run.env)
data = NbMetadataCollector.get_exec_data(sphinx_run.env, "custom-formats2")
assert data
assert data["method"] == "cache"
assert data["succeeded"] is True