Skip to content

Commit fbb1571

Browse files
committed
test saving scripts
1 parent 2de2c1a commit fbb1571

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
run: |
2828
python -m pip install --upgrade pip
2929
pip install -r requirements.txt
30+
python -m bash_kernel.install # For testing a non-standard kernel
3031
pip install .
3132
3233
- name: Run tests

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ nbformat
77
# For documentation building and testing
88
matplotlib
99
pytest
10+
11+
# A non-standard kernel
12+
bash_kernel

tests/test_execute.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shutil
33
import os
44
import sys
5+
import warnings
56
from io import StringIO
67
from unittest.mock import Mock
78
from pathlib import Path
@@ -35,7 +36,7 @@ def doctree():
3536
def doctree(
3637
source,
3738
config=None,
38-
return_warnings=False,
39+
return_all=False,
3940
entrypoint="jupyter_sphinx",
4041
buildername='html'
4142
):
@@ -59,8 +60,8 @@ def doctree(
5960
app.build()
6061

6162
doctree = app.env.get_and_resolve_doctree("index", app.builder)
62-
if return_warnings:
63-
return doctree, warnings.getvalue()
63+
if return_all:
64+
return doctree, app, warnings.getvalue()
6465
else:
6566
return doctree
6667

@@ -294,8 +295,6 @@ def test_emphasize_lines(doctree):
294295
"""
295296
tree = doctree(source)
296297
cell0, cell1 = tree.traverse(JupyterCellNode)
297-
(cellinput0, celloutput0) = cell0.children
298-
(cellinput1, celloutput1) = cell1.children
299298

300299
assert cell0.attributes["emphasize_lines"] == [1, 3, 4, 5]
301300
assert cell1.attributes["emphasize_lines"] == [2, 4]
@@ -313,9 +312,8 @@ def test_execution_environment_carries_over(doctree):
313312
a
314313
"""
315314
tree = doctree(source)
316-
cell0, cell1 = tree.traverse(JupyterCellNode)
317-
(cellinput0, celloutput0) = cell0.children
318-
(cellinput1, celloutput1) = cell1.children
315+
_, cell1 = tree.traverse(JupyterCellNode)
316+
(_, celloutput1) = cell1.children
319317
assert celloutput1.children[0].rawsource.strip() == "2"
320318

321319

@@ -335,9 +333,8 @@ def test_kernel_restart(doctree):
335333
a
336334
"""
337335
tree = doctree(source)
338-
cell0, cell1 = tree.traverse(JupyterCellNode)
339-
(cellinput0, celloutput0) = cell0.children
340-
(cellinput1, celloutput1) = cell1.children
336+
_, cell1 = tree.traverse(JupyterCellNode)
337+
(_, celloutput1) = cell1.children
341338
assert "NameError" in celloutput1.children[0].rawsource
342339

343340

@@ -358,7 +355,7 @@ def test_raises(doctree):
358355
"""
359356
tree = doctree(source)
360357
(cell,) = tree.traverse(JupyterCellNode)
361-
(cellinput, celloutput) = cell.children
358+
(_, celloutput) = cell.children
362359
assert "ValueError" in celloutput.children[0].rawsource
363360

364361
source = """
@@ -369,7 +366,7 @@ def test_raises(doctree):
369366
"""
370367
tree = doctree(source)
371368
(cell,) = tree.traverse(JupyterCellNode)
372-
(cellinput, celloutput) = cell.children
369+
(_, celloutput) = cell.children
373370
assert "ValueError" in celloutput.children[0].rawsource
374371

375372

@@ -406,7 +403,7 @@ def test_stdout(doctree):
406403
"""
407404
tree = doctree(source)
408405
(cell,) = tree.traverse(JupyterCellNode)
409-
(cellinput, celloutput) = cell.children
406+
(_, celloutput) = cell.children
410407
assert len(cell.children) == 2
411408
assert celloutput.children[0].rawsource.strip() == "hello world"
412409

@@ -419,7 +416,7 @@ def test_stderr(doctree):
419416
print('hello world', file=sys.stderr)
420417
"""
421418

422-
tree, warnings = doctree(source, return_warnings=True)
419+
tree, _, warnings = doctree(source, return_all=True)
423420
assert "hello world" in warnings
424421
(cell,) = tree.traverse(JupyterCellNode)
425422
(_, celloutput) = cell.children
@@ -555,7 +552,7 @@ def test_latex(doctree):
555552
for start, end in delimiter_pairs:
556553
tree = doctree(source.format(start, end))
557554
(cell,) = tree.traverse(JupyterCellNode)
558-
(cellinput, celloutput) = cell.children
555+
(_, celloutput) = cell.children
559556
assert celloutput.children[0].astext() == r"\int"
560557

561558

@@ -618,3 +615,39 @@ def test_download_role(text, reftarget, caption, tmp_path):
618615
assert_node(ret[0][0], [literal, caption])
619616
assert msg == []
620617

618+
619+
def test_save_script(doctree):
620+
source = """
621+
.. jupyter-kernel:: python3
622+
:id: test
623+
624+
.. jupyter-execute::
625+
626+
a = 1
627+
print(a)
628+
"""
629+
tree, app, _ = doctree(source, return_all=True)
630+
outdir = Path(app.outdir)
631+
saved_text = (outdir / '../jupyter_execute/test.py').read_text()
632+
assert saved_text.startswith('#!/usr/bin/env python')
633+
assert 'print(a)' in saved_text
634+
635+
636+
def test_bash_kernel(doctree):
637+
pytest.importorskip('bash_kernel')
638+
source = """
639+
.. jupyter-kernel:: bash
640+
:id: test
641+
642+
.. jupyter-execute::
643+
644+
echo "foo"
645+
"""
646+
with warnings.catch_warnings():
647+
# See https://github.com/takluyver/bash_kernel/issues/105
648+
warnings.simplefilter('ignore', DeprecationWarning)
649+
_, app, _ = doctree(source, return_all=True)
650+
651+
outdir = Path(app.outdir)
652+
saved_text = (outdir / '../jupyter_execute/test.sh').read_text()
653+
assert 'echo "foo"' in saved_text

0 commit comments

Comments
 (0)