-
Notifications
You must be signed in to change notification settings - Fork 65
/
test_execute.py
179 lines (139 loc) · 3.99 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
import tempfile
import shutil
import os
import sys
from io import StringIO
from sphinx.testing.util import SphinxTestApp, path
from sphinx.errors import ExtensionError
import pytest
from jupyter_sphinx.execute import (
JupyterCellNode,
JupyterKernelNode,
JupyterWidgetViewNode,
JupyterWidgetStateNode,
)
@pytest.fixture()
def doctree():
source_trees = []
apps = []
syspath = sys.path[:]
def doctree(source):
src_dir = tempfile.mkdtemp()
source_trees.append(src_dir)
with open(os.path.join(src_dir, 'conf.py'), 'w') as f:
f.write("extensions = ['jupyter_sphinx.execute']")
with open(os.path.join(src_dir, 'index.rst'), 'w') as f:
f.write(source)
app = SphinxTestApp(srcdir=path(src_dir), status=StringIO(),
warning=StringIO())
apps.append(app)
app.build()
return app.env.get_doctree('index')
yield doctree
sys.path[:] = syspath
for app in reversed(apps):
app.cleanup()
for tree in source_trees:
shutil.rmtree(tree)
def test_basic(doctree):
source = '''
.. jupyter-execute::
2 + 2
'''
tree = doctree(source)
cell, = tree.traverse(JupyterCellNode)
assert cell.attributes['code_below'] is False
assert cell.attributes['hide_code'] is False
assert cell.attributes['hide_output'] is False
assert cell.children[0].rawsource.strip() == "2 + 2"
assert cell.children[1].rawsource.strip() == "4"
def test_hide_output(doctree):
source = '''
.. jupyter-execute::
:hide-output:
2 + 2
'''
tree = doctree(source)
cell, = tree.traverse(JupyterCellNode)
assert cell.attributes['hide_output'] is True
assert len(cell.children) == 1
assert cell.children[0].rawsource.strip() == "2 + 2"
def test_hide_code(doctree):
source = '''
.. jupyter-execute::
:hide-code:
2 + 2
'''
tree = doctree(source)
cell, = tree.traverse(JupyterCellNode)
assert cell.attributes['hide_code'] is True
assert len(cell.children) == 1
assert cell.children[0].rawsource.strip() == "4"
def test_code_below(doctree):
source = '''
.. jupyter-execute::
:code-below:
2 + 2
'''
tree = doctree(source)
cell, = tree.traverse(JupyterCellNode)
assert cell.attributes['code_below'] is True
assert cell.children[0].rawsource.strip() == "4"
assert cell.children[1].rawsource.strip() == "2 + 2"
def test_execution_environment_carries_over(doctree):
source = '''
.. jupyter-execute::
a = 1
.. jupyter-execute::
a += 1
a
'''
tree = doctree(source)
cell0, cell1 = tree.traverse(JupyterCellNode)
assert cell1.children[1].rawsource.strip() == "2"
def test_kernel_restart(doctree):
source = '''
.. jupyter-execute::
a = 1
.. jupyter-kernel::
:id: new-kernel
.. jupyter-execute::
:raises:
a += 1
a
'''
tree = doctree(source)
cell0, cell1 = tree.traverse(JupyterCellNode)
assert 'NameError' in cell1.children[1].rawsource
def test_raises(doctree):
source = '''
.. jupyter-execute::
raise ValueError()
'''
with pytest.raises(ExtensionError):
doctree(source)
source = '''
.. jupyter-execute::
:raises:
raise ValueError()
'''
tree = doctree(source)
cell, = tree.traverse(JupyterCellNode)
'ValueError' in cell.children[1].rawsource
source = '''
.. jupyter-execute::
:raises: KeyError, ValueError
raise ValueError()
'''
tree = doctree(source)
cell, = tree.traverse(JupyterCellNode)
'ValueError' in cell.children[1].rawsource
def test_widgets(doctree):
source = '''
.. jupyter-execute::
import ipywidgets
ipywidgets.Button()
'''
tree = doctree(source)
assert len(list(tree.traverse(JupyterWidgetViewNode))) == 1
assert len(list(tree.traverse(JupyterWidgetStateNode))) == 1