-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtest_parser.py
148 lines (133 loc) · 4.97 KB
/
test_parser.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
"""Test parsing of already executed notebooks."""
import os
from pathlib import Path
import pytest
@pytest.mark.sphinx_params("basic_run.ipynb", conf={"nb_execution_mode": "off"})
def test_basic_run(sphinx_run, file_regression):
sphinx_run.build()
assert sphinx_run.warnings() == ""
assert set(sphinx_run.env.metadata["basic_run"].keys()) == {
"test_name",
"wordcount",
"kernelspec",
"language_info",
}
assert set(sphinx_run.env.nb_metadata["basic_run"].keys()) == set()
assert sphinx_run.env.metadata["basic_run"]["test_name"] == "notebook1"
assert sphinx_run.env.metadata["basic_run"]["kernelspec"] == {
"display_name": "Python 3",
"language": "python",
"name": "python3",
}
file_regression.check(
sphinx_run.get_doctree().pformat(), extension=".xml", encoding="utf-8"
)
filenames = {
p.name
for p in Path(
os.fspath(sphinx_run.app.srcdir / "_build" / "jupyter_execute")
).iterdir()
}
assert filenames == {"basic_run.ipynb"}
@pytest.mark.sphinx_params(
"basic_run_intl.ipynb", conf={"language": "es", "locale_dirs": ["locale"]}
)
def test_basic_run_intl(sphinx_run, file_regression):
sphinx_run.build()
assert sphinx_run.warnings() == ""
assert set(sphinx_run.env.metadata["basic_run_intl"].keys()) == {
"test_name",
"wordcount",
"kernelspec",
"language_info",
}
assert set(sphinx_run.env.nb_metadata["basic_run_intl"].keys()) == set()
assert sphinx_run.env.metadata["basic_run_intl"]["test_name"] == "notebook1"
assert sphinx_run.env.metadata["basic_run_intl"]["kernelspec"] == {
"display_name": "Python 3",
"language": "python",
"name": "python3",
}
file_regression.check(
sphinx_run.get_doctree().pformat(), extension=".xml", encoding="utf-8"
)
filenames = {
p.name
for p in Path(
os.fspath(sphinx_run.app.srcdir / "_build" / "jupyter_execute")
).iterdir()
}
assert filenames == {"basic_run_intl.ipynb"}
@pytest.mark.sphinx_params("complex_outputs.ipynb", conf={"nb_execution_mode": "off"})
def test_complex_outputs(sphinx_run, file_regression):
sphinx_run.build()
assert sphinx_run.warnings() == ""
assert set(sphinx_run.env.metadata["complex_outputs"].keys()) == {
"ipub",
"hide_input",
"nav_menu",
"celltoolbar",
"latex_envs",
"jupytext",
"toc",
"varInspector",
"wordcount",
"kernelspec",
"language_info",
}
assert set(sphinx_run.env.nb_metadata["complex_outputs"].keys()) == set()
assert sphinx_run.env.metadata["complex_outputs"]["celltoolbar"] == "Edit Metadata"
assert sphinx_run.env.metadata["complex_outputs"]["hide_input"] == "False"
assert sphinx_run.env.metadata["complex_outputs"]["kernelspec"] == {
"display_name": "Python 3",
"language": "python",
"name": "python3",
}
doctree_string = sphinx_run.get_doctree().pformat()
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")
filenames = {
p.name.replace(".jpeg", ".jpg")
for p in Path(
os.fspath(sphinx_run.app.srcdir / "_build" / "jupyter_execute")
).iterdir()
}
# print(filenames)
assert filenames == {
"16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png",
"a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg",
"8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png",
"complex_outputs.ipynb",
}
@pytest.mark.sphinx_params(
"latex_build/index.ipynb",
"latex_build/other.ipynb",
conf={"nb_execution_mode": "off"},
buildername="latex",
)
def test_toctree_in_ipynb(sphinx_run, file_regression):
sphinx_run.build()
print(sphinx_run.status())
print(sphinx_run.warnings())
file_regression.check(
sphinx_run.get_doctree("latex_build/other").pformat(), extension=".xml"
)
assert sphinx_run.warnings() == ""
@pytest.mark.sphinx_params("ipywidgets.ipynb", conf={"nb_execution_mode": "off"})
def test_ipywidgets(sphinx_run):
"""Test that ipywidget state is extracted and JS is included in the HTML head."""
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
assert "js_files" in sphinx_run.env.nb_metadata["ipywidgets"]
assert set(sphinx_run.env.nb_metadata["ipywidgets"]["js_files"]) == {
"ipywidgets_state",
"ipywidgets_0",
"ipywidgets_1",
}
head_scripts = sphinx_run.get_html().select("head > script")
assert any("require.js" in script.get("src", "") for script in head_scripts)
assert any("embed-amd.js" in script.get("src", "") for script in head_scripts)