Skip to content

Commit

Permalink
Test CLI script to notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
Titan-C committed Nov 20, 2016
1 parent ed5e98a commit 0d28919
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ before_script:
script:
- if [ "$DISTRIB" == "ubuntu" ]; then python setup.py nosetests; fi
- if [ "$DISTRIB" == "conda" ]; then nosetests; fi
- nosetests bin # to test jupyter notebook generator CLI
- cd doc
- make html-noplot
- make html
6 changes: 2 additions & 4 deletions bin/sphx_glr_python_to_jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
' (e.g. *.py)')


def main():
args = parser.parse_args()

def main(args):
for src_file in args.python_src_file:
blocks = sg.split_code_and_text_blocks(src_file)
print('Converting {0}'.format(src_file))
Expand All @@ -37,4 +35,4 @@ def main():


if __name__ == '__main__':
main()
main(parser.parse_args())
41 changes: 41 additions & 0 deletions bin/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
r"""
Test the Sphinx-Gallery python to Jupyter notebook converter CLI
================================================================
This test only the command line utility. The notebook converter is
tested as part of the Sphinx-Gallery tests.
"""

# Author: Óscar Nájera
# License: 3-clause BSD

from __future__ import division, absolute_import, print_function
import os
from unittest import TestCase
import sphx_glr_python_to_jupyter


class CommandLineTest(TestCase):
"""Test the Sphinx-Gallery python to Jupyter notebook converter CLI"""

def test_with_empty_args(self):
""" User passes no args, should fail with SystemExit """
with self.assertRaises(SystemExit):
sphx_glr_python_to_jupyter.parser.parse_args([])

def test_missing_file(self):
""" User passes non existing file, should fail with FileNotFoundError """
args = sphx_glr_python_to_jupyter.parser.parse_args(['nofile.py'])
with self.assertRaises(FileNotFoundError):
sphx_glr_python_to_jupyter.main(args)


def test_file_is_generated():
"""User passes good python file. Check notebook file is created"""

args = sphx_glr_python_to_jupyter.parser.parse_args(
['examples/plot_quantum.py'])
sphx_glr_python_to_jupyter.main(args)
assert os.path.isfile('examples/plot_quantum.ipynb')
os.remove('examples/plot_quantum.ipynb')
2 changes: 1 addition & 1 deletion doc/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Sphinx Gallery exposes its python source to Jupyter notebook convert
as a executable script too. To use this utility just call the script
and give the Python source file as argument::

$ sphx_ex2nb.py python_script.py
$ sphx_glr_python_to_jupyter.py python_script.py


Embedding Sphinx-Gallery inside your documentation script extensions
Expand Down
1 change: 0 additions & 1 deletion sphinx_gallery/tests/test_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from __future__ import division, absolute_import, print_function
import json
import os
import tempfile
from nose.tools import assert_equal, assert_false, assert_true
import sphinx_gallery.gen_rst as sg
from sphinx_gallery.notebook import rst2md, jupyter_notebook, save_notebook
Expand Down

0 comments on commit 0d28919

Please sign in to comment.