2
2
import shutil
3
3
import os
4
4
import sys
5
+ import warnings
5
6
from io import StringIO
6
7
from unittest .mock import Mock
7
8
from pathlib import Path
@@ -35,7 +36,7 @@ def doctree():
35
36
def doctree (
36
37
source ,
37
38
config = None ,
38
- return_warnings = False ,
39
+ return_all = False ,
39
40
entrypoint = "jupyter_sphinx" ,
40
41
buildername = 'html'
41
42
):
@@ -59,8 +60,8 @@ def doctree(
59
60
app .build ()
60
61
61
62
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 ()
64
65
else :
65
66
return doctree
66
67
@@ -294,8 +295,6 @@ def test_emphasize_lines(doctree):
294
295
"""
295
296
tree = doctree (source )
296
297
cell0 , cell1 = tree .traverse (JupyterCellNode )
297
- (cellinput0 , celloutput0 ) = cell0 .children
298
- (cellinput1 , celloutput1 ) = cell1 .children
299
298
300
299
assert cell0 .attributes ["emphasize_lines" ] == [1 , 3 , 4 , 5 ]
301
300
assert cell1 .attributes ["emphasize_lines" ] == [2 , 4 ]
@@ -313,9 +312,8 @@ def test_execution_environment_carries_over(doctree):
313
312
a
314
313
"""
315
314
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
319
317
assert celloutput1 .children [0 ].rawsource .strip () == "2"
320
318
321
319
@@ -335,9 +333,8 @@ def test_kernel_restart(doctree):
335
333
a
336
334
"""
337
335
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
341
338
assert "NameError" in celloutput1 .children [0 ].rawsource
342
339
343
340
@@ -358,7 +355,7 @@ def test_raises(doctree):
358
355
"""
359
356
tree = doctree (source )
360
357
(cell ,) = tree .traverse (JupyterCellNode )
361
- (cellinput , celloutput ) = cell .children
358
+ (_ , celloutput ) = cell .children
362
359
assert "ValueError" in celloutput .children [0 ].rawsource
363
360
364
361
source = """
@@ -369,7 +366,7 @@ def test_raises(doctree):
369
366
"""
370
367
tree = doctree (source )
371
368
(cell ,) = tree .traverse (JupyterCellNode )
372
- (cellinput , celloutput ) = cell .children
369
+ (_ , celloutput ) = cell .children
373
370
assert "ValueError" in celloutput .children [0 ].rawsource
374
371
375
372
@@ -406,7 +403,7 @@ def test_stdout(doctree):
406
403
"""
407
404
tree = doctree (source )
408
405
(cell ,) = tree .traverse (JupyterCellNode )
409
- (cellinput , celloutput ) = cell .children
406
+ (_ , celloutput ) = cell .children
410
407
assert len (cell .children ) == 2
411
408
assert celloutput .children [0 ].rawsource .strip () == "hello world"
412
409
@@ -419,7 +416,7 @@ def test_stderr(doctree):
419
416
print('hello world', file=sys.stderr)
420
417
"""
421
418
422
- tree , warnings = doctree (source , return_warnings = True )
419
+ tree , _ , warnings = doctree (source , return_all = True )
423
420
assert "hello world" in warnings
424
421
(cell ,) = tree .traverse (JupyterCellNode )
425
422
(_ , celloutput ) = cell .children
@@ -555,7 +552,7 @@ def test_latex(doctree):
555
552
for start , end in delimiter_pairs :
556
553
tree = doctree (source .format (start , end ))
557
554
(cell ,) = tree .traverse (JupyterCellNode )
558
- (cellinput , celloutput ) = cell .children
555
+ (_ , celloutput ) = cell .children
559
556
assert celloutput .children [0 ].astext () == r"\int"
560
557
561
558
@@ -618,3 +615,39 @@ def test_download_role(text, reftarget, caption, tmp_path):
618
615
assert_node (ret [0 ][0 ], [literal , caption ])
619
616
assert msg == []
620
617
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