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,9 +60,8 @@ def doctree(
59
60
app .build ()
60
61
61
62
doctree = app .env .get_and_resolve_doctree ("index" , app .builder )
62
-
63
- if return_warnings :
64
- return doctree , warnings .getvalue ()
63
+ if return_all :
64
+ return doctree , app , warnings .getvalue ()
65
65
else :
66
66
return doctree
67
67
@@ -295,8 +295,6 @@ def test_emphasize_lines(doctree):
295
295
"""
296
296
tree = doctree (source )
297
297
cell0 , cell1 = tree .traverse (JupyterCellNode )
298
- (cellinput0 , celloutput0 ) = cell0 .children
299
- (cellinput1 , celloutput1 ) = cell1 .children
300
298
301
299
assert cell0 .attributes ["emphasize_lines" ] == [1 , 3 , 4 , 5 ]
302
300
assert cell1 .attributes ["emphasize_lines" ] == [2 , 4 ]
@@ -314,9 +312,8 @@ def test_execution_environment_carries_over(doctree):
314
312
a
315
313
"""
316
314
tree = doctree (source )
317
- cell0 , cell1 = tree .traverse (JupyterCellNode )
318
- (cellinput0 , celloutput0 ) = cell0 .children
319
- (cellinput1 , celloutput1 ) = cell1 .children
315
+ _ , cell1 = tree .traverse (JupyterCellNode )
316
+ (_ , celloutput1 ) = cell1 .children
320
317
assert celloutput1 .children [0 ].rawsource .strip () == "2"
321
318
322
319
@@ -336,9 +333,8 @@ def test_kernel_restart(doctree):
336
333
a
337
334
"""
338
335
tree = doctree (source )
339
- cell0 , cell1 = tree .traverse (JupyterCellNode )
340
- (cellinput0 , celloutput0 ) = cell0 .children
341
- (cellinput1 , celloutput1 ) = cell1 .children
336
+ _ , cell1 = tree .traverse (JupyterCellNode )
337
+ (_ , celloutput1 ) = cell1 .children
342
338
assert "NameError" in celloutput1 .children [0 ].rawsource
343
339
344
340
@@ -359,7 +355,7 @@ def test_raises(doctree):
359
355
"""
360
356
tree = doctree (source )
361
357
(cell ,) = tree .traverse (JupyterCellNode )
362
- (cellinput , celloutput ) = cell .children
358
+ (_ , celloutput ) = cell .children
363
359
assert "ValueError" in celloutput .children [0 ].rawsource
364
360
365
361
source = """
@@ -370,7 +366,7 @@ def test_raises(doctree):
370
366
"""
371
367
tree = doctree (source )
372
368
(cell ,) = tree .traverse (JupyterCellNode )
373
- (cellinput , celloutput ) = cell .children
369
+ (_ , celloutput ) = cell .children
374
370
assert "ValueError" in celloutput .children [0 ].rawsource
375
371
376
372
@@ -407,7 +403,7 @@ def test_stdout(doctree):
407
403
"""
408
404
tree = doctree (source )
409
405
(cell ,) = tree .traverse (JupyterCellNode )
410
- (cellinput , celloutput ) = cell .children
406
+ (_ , celloutput ) = cell .children
411
407
assert len (cell .children ) == 2
412
408
assert celloutput .children [0 ].rawsource .strip () == "hello world"
413
409
@@ -420,7 +416,7 @@ def test_stderr(doctree):
420
416
print('hello world', file=sys.stderr)
421
417
"""
422
418
423
- tree , warnings = doctree (source , return_warnings = True )
419
+ tree , _ , warnings = doctree (source , return_all = True )
424
420
assert "hello world" in warnings
425
421
(cell ,) = tree .traverse (JupyterCellNode )
426
422
(_ , celloutput ) = cell .children
@@ -556,7 +552,7 @@ def test_latex(doctree):
556
552
for start , end in delimiter_pairs :
557
553
tree = doctree (source .format (start , end ))
558
554
(cell ,) = tree .traverse (JupyterCellNode )
559
- (cellinput , celloutput ) = cell .children
555
+ (_ , celloutput ) = cell .children
560
556
assert celloutput .children [0 ].astext () == r"\int"
561
557
562
558
@@ -619,3 +615,42 @@ def test_download_role(text, reftarget, caption, tmp_path):
619
615
assert_node (ret [0 ][0 ], [literal , caption ])
620
616
assert msg == []
621
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
+ if sys .platform == 'win32' :
639
+ pytest .skip ("Not trying bash on windows." )
640
+
641
+ source = """
642
+ .. jupyter-kernel:: bash
643
+ :id: test
644
+
645
+ .. jupyter-execute::
646
+
647
+ echo "foo"
648
+ """
649
+ with warnings .catch_warnings ():
650
+ # See https://github.com/takluyver/bash_kernel/issues/105
651
+ warnings .simplefilter ('ignore' , DeprecationWarning )
652
+ _ , app , _ = doctree (source , return_all = True )
653
+
654
+ outdir = Path (app .outdir )
655
+ saved_text = (outdir / '../jupyter_execute/test.sh' ).read_text ()
656
+ assert 'echo "foo"' in saved_text
0 commit comments