Skip to content

Commit 9eead00

Browse files
committed
add tests and test configuration
1 parent e21c9eb commit 9eead00

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

tests/test_execute.py

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import tempfile
2+
import shutil
3+
import os
4+
import sys
5+
from io import StringIO
6+
7+
from sphinx.testing.util import SphinxTestApp, path
8+
from sphinx.errors import ExtensionError
9+
10+
import pytest
11+
12+
from jupyter_sphinx.execute import JupyterCellNode, JupyterKernelNode
13+
14+
@pytest.fixture()
15+
def doctree():
16+
source_trees = []
17+
apps = []
18+
syspath = sys.path[:]
19+
20+
def doctree(source):
21+
src_dir = tempfile.mkdtemp()
22+
source_trees.append(src_dir)
23+
with open(os.path.join(src_dir, 'conf.py'), 'w') as f:
24+
f.write("extensions = ['jupyter_sphinx.execute']")
25+
with open(os.path.join(src_dir, 'index.rst'), 'w') as f:
26+
f.write(source)
27+
app = SphinxTestApp(srcdir=path(src_dir), status=StringIO(),
28+
warning=StringIO())
29+
apps.append(app)
30+
app.build()
31+
return app.env.get_doctree('index')
32+
33+
yield doctree
34+
35+
sys.path[:] = syspath
36+
for app in reversed(apps):
37+
app.cleanup()
38+
for tree in source_trees:
39+
shutil.rmtree(tree)
40+
41+
42+
def test_basic(doctree):
43+
source = '''
44+
.. jupyter-execute::
45+
46+
2 + 2
47+
'''
48+
tree = doctree(source)
49+
cell, = tree.traverse(JupyterCellNode)
50+
assert cell.attributes['code_below'] is False
51+
assert cell.attributes['hide_code'] is False
52+
assert cell.attributes['hide_output'] is False
53+
assert cell.children[0].rawsource.strip() == "2 + 2"
54+
assert cell.children[1].rawsource.strip() == "4"
55+
56+
57+
def test_hide_output(doctree):
58+
source = '''
59+
.. jupyter-execute::
60+
:hide-output:
61+
62+
2 + 2
63+
'''
64+
tree = doctree(source)
65+
cell, = tree.traverse(JupyterCellNode)
66+
assert cell.attributes['hide_output'] is True
67+
assert len(cell.children) == 1
68+
assert cell.children[0].rawsource.strip() == "2 + 2"
69+
70+
71+
def test_hide_code(doctree):
72+
source = '''
73+
.. jupyter-execute::
74+
:hide-code:
75+
76+
2 + 2
77+
'''
78+
tree = doctree(source)
79+
cell, = tree.traverse(JupyterCellNode)
80+
assert cell.attributes['hide_code'] is True
81+
assert len(cell.children) == 1
82+
assert cell.children[0].rawsource.strip() == "4"
83+
84+
85+
def test_code_below(doctree):
86+
source = '''
87+
.. jupyter-execute::
88+
:code-below:
89+
90+
2 + 2
91+
'''
92+
tree = doctree(source)
93+
cell, = tree.traverse(JupyterCellNode)
94+
assert cell.attributes['code_below'] is True
95+
assert cell.children[0].rawsource.strip() == "4"
96+
assert cell.children[1].rawsource.strip() == "2 + 2"
97+
98+
99+
def test_execution_environment_carries_over(doctree):
100+
source = '''
101+
.. jupyter-execute::
102+
103+
a = 1
104+
105+
.. jupyter-execute::
106+
107+
a += 1
108+
a
109+
'''
110+
tree = doctree(source)
111+
cell0, cell1 = tree.traverse(JupyterCellNode)
112+
assert cell1.children[1].rawsource.strip() == "2"
113+
114+
115+
def test_kernel_restart(doctree):
116+
source = '''
117+
.. jupyter-execute::
118+
119+
a = 1
120+
121+
.. jupyter-kernel::
122+
:id: new-kernel
123+
124+
.. jupyter-execute::
125+
:raises:
126+
127+
a += 1
128+
a
129+
'''
130+
tree = doctree(source)
131+
cell0, cell1 = tree.traverse(JupyterCellNode)
132+
assert 'NameError' in cell1.children[1].rawsource
133+
134+
135+
def test_raises(doctree):
136+
source = '''
137+
.. jupyter-execute::
138+
139+
raise ValueError()
140+
'''
141+
with pytest.raises(ExtensionError):
142+
doctree(source)
143+
144+
source = '''
145+
.. jupyter-execute::
146+
:raises:
147+
148+
raise ValueError()
149+
'''
150+
tree = doctree(source)
151+
cell, = tree.traverse(JupyterCellNode)
152+
'ValueError' in cell.children[1].rawsource
153+
154+
source = '''
155+
.. jupyter-execute::
156+
:raises: KeyError, ValueError
157+
158+
raise ValueError()
159+
'''
160+
tree = doctree(source)
161+
cell, = tree.traverse(JupyterCellNode)
162+
'ValueError' in cell.children[1].rawsource

tox.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[tox]
2+
envlist = py36,py37
3+
4+
[testenv]
5+
deps =
6+
sphinx
7+
pytest
8+
commands =
9+
pytest

0 commit comments

Comments
 (0)