test_nbconvertapp.py
212 lines
| 7.9 KiB
| text/x-python
|
PythonLexer
marcmolla
|
r13928 | # -*- coding: utf-8 -*- | ||
MinRK
|
r12354 | """Test NbConvertApp""" | ||
MinRK
|
r16418 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Jonathan Frederic
|
r11477 | |||
Jonathan Frederic
|
r11478 | import os | ||
Richard Everson
|
r11990 | import glob | ||
stonebig <stonebig>
|
r12391 | import sys | ||
Richard Everson
|
r11990 | |||
Jonathan Frederic
|
r11477 | from .base import TestsBase | ||
MinRK
|
r16418 | from ..postprocessors import PostProcessorBase | ||
Jonathan Frederic
|
r11477 | |||
MinRK
|
r12354 | import IPython.testing.tools as tt | ||
Jonathan Frederic
|
r11747 | from IPython.testing import decorators as dec | ||
Jonathan Frederic
|
r11547 | |||
Jonathan Frederic
|
r11477 | #----------------------------------------------------------------------------- | ||
# Classes and functions | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r16418 | class DummyPost(PostProcessorBase): | ||
def postprocess(self, filename): | ||||
print("Dummy:%s" % filename) | ||||
Jonathan Frederic
|
r11494 | class TestNbConvertApp(TestsBase): | ||
Jonathan Frederic
|
r11477 | """Collection of NbConvertApp tests""" | ||
Jonathan Frederic
|
r11478 | |||
def test_notebook_help(self): | ||||
MinRK
|
r12354 | """Will help show if no notebooks are specified?""" | ||
Jonathan Frederic
|
r11478 | with self.create_temp_cwd(): | ||
Jonathan Frederic
|
r12133 | out, err = self.call('nbconvert --log-level 0', ignore_return_code=True) | ||
MinRK
|
r12354 | self.assertIn("see '--help-all'", out) | ||
def test_help_output(self): | ||||
"""ipython nbconvert --help-all works""" | ||||
tt.help_all_output_test('nbconvert') | ||||
Jonathan Frederic
|
r11478 | |||
def test_glob(self): | ||||
""" | ||||
Do search patterns work for notebook names? | ||||
""" | ||||
with self.create_temp_cwd(['notebook*.ipynb']): | ||||
Jonathan Frederic
|
r12133 | self.call('nbconvert --to python *.ipynb --log-level 0') | ||
Jonathan Frederic
|
r11639 | assert os.path.isfile('notebook1.py') | ||
assert os.path.isfile('notebook2.py') | ||||
Jonathan Frederic
|
r11478 | |||
def test_glob_subdir(self): | ||||
""" | ||||
Do search patterns work for subdirectory notebook names? | ||||
""" | ||||
Paul Ivanov
|
r11828 | with self.create_temp_cwd(): | ||
Jonathan Frederic
|
r11478 | self.copy_files_to(['notebook*.ipynb'], 'subdir/') | ||
Jonathan Frederic
|
r12133 | self.call('nbconvert --to python --log-level 0 ' + | ||
os.path.join('subdir', '*.ipynb')) | ||||
Jonathan Frederic
|
r11639 | assert os.path.isfile('notebook1.py') | ||
assert os.path.isfile('notebook2.py') | ||||
Jonathan Frederic
|
r11478 | |||
def test_explicit(self): | ||||
""" | ||||
Do explicit notebook names work? | ||||
""" | ||||
with self.create_temp_cwd(['notebook*.ipynb']): | ||||
Jonathan Frederic
|
r12133 | self.call('nbconvert --log-level 0 --to python notebook2') | ||
Jonathan Frederic
|
r11639 | assert not os.path.isfile('notebook1.py') | ||
assert os.path.isfile('notebook2.py') | ||||
Jonathan Frederic
|
r11478 | |||
Paul Ivanov
|
r11991 | @dec.onlyif_cmds_exist('pdflatex') | ||
@dec.onlyif_cmds_exist('pandoc') | ||||
Richard Everson
|
r11990 | def test_filename_spaces(self): | ||
""" | ||||
Paul Ivanov
|
r11991 | Generate PDFs with graphics if notebooks have spaces in the name? | ||
Richard Everson
|
r11990 | """ | ||
Paul Ivanov
|
r11993 | with self.create_temp_cwd(['notebook2.ipynb']): | ||
os.rename('notebook2.ipynb', 'notebook with spaces.ipynb') | ||||
MinRK
|
r16418 | self.call('nbconvert --log-level 0 --to pdf' | ||
' "notebook with spaces"' | ||||
' --PDFExporter.latex_count=1' | ||||
' --PDFExporter.verbose=True' | ||||
) | ||||
Paul Ivanov
|
r11991 | assert os.path.isfile('notebook with spaces.pdf') | ||
Richard Everson
|
r11990 | |||
Jonathan Frederic
|
r11747 | def test_post_processor(self): | ||
MinRK
|
r16418 | """Do post processors work?""" | ||
Jonathan Frederic
|
r11747 | with self.create_temp_cwd(['notebook1.ipynb']): | ||
MinRK
|
r16418 | out, err = self.call('nbconvert --log-level 0 --to python notebook1 ' | ||
'--post IPython.nbconvert.tests.test_nbconvertapp.DummyPost') | ||||
self.assertIn('Dummy:notebook1.py', out) | ||||
Jonathan Frederic
|
r11747 | |||
MinRK
|
r12527 | @dec.onlyif_cmds_exist('pandoc') | ||
def test_spurious_cr(self): | ||||
"""Check for extra CR characters""" | ||||
with self.create_temp_cwd(['notebook2.ipynb']): | ||||
self.call('nbconvert --log-level 0 --to latex notebook2') | ||||
assert os.path.isfile('notebook2.tex') | ||||
with open('notebook2.tex') as f: | ||||
tex = f.read() | ||||
self.call('nbconvert --log-level 0 --to html notebook2') | ||||
assert os.path.isfile('notebook2.html') | ||||
with open('notebook2.html') as f: | ||||
html = f.read() | ||||
self.assertEqual(tex.count('\r'), tex.count('\r\n')) | ||||
self.assertEqual(html.count('\r'), html.count('\r\n')) | ||||
Jonathan Frederic
|
r11747 | |||
Paul Ivanov
|
r11833 | @dec.onlyif_cmds_exist('pandoc') | ||
stonebig <stonebig>
|
r12391 | def test_png_base64_html_ok(self): | ||
"""Is embedded png data well formed in HTML?""" | ||||
with self.create_temp_cwd(['notebook2.ipynb']): | ||||
self.call('nbconvert --log-level 0 --to HTML ' | ||||
Thomas Kluyver
|
r14047 | 'notebook2.ipynb --template full') | ||
stonebig <stonebig>
|
r12391 | assert os.path.isfile('notebook2.html') | ||
with open('notebook2.html') as f: | ||||
assert "data:image/png;base64,b'" not in f.read() | ||||
@dec.onlyif_cmds_exist('pandoc') | ||||
Jonathan Frederic
|
r11745 | def test_template(self): | ||
Jonathan Frederic
|
r11740 | """ | ||
Jonathan Frederic
|
r11745 | Do export templates work? | ||
Jonathan Frederic
|
r11740 | """ | ||
Jonathan Frederic
|
r11767 | with self.create_temp_cwd(['notebook2.ipynb']): | ||
Jonathan Frederic
|
r12133 | self.call('nbconvert --log-level 0 --to slides ' | ||
Thomas Kluyver
|
r14051 | 'notebook2.ipynb') | ||
Jonathan Frederic
|
r11767 | assert os.path.isfile('notebook2.slides.html') | ||
with open('notebook2.slides.html') as f: | ||||
Jonathan Frederic
|
r11740 | assert '/reveal.css' in f.read() | ||
MinRK
|
r17310 | def test_output_ext(self): | ||
"""test --output=outputfile[.ext]""" | ||||
with self.create_temp_cwd(['notebook1.ipynb']): | ||||
self.call('nbconvert --log-level 0 --to python ' | ||||
'notebook1.ipynb --output nb.py') | ||||
assert os.path.exists('nb.py') | ||||
self.call('nbconvert --log-level 0 --to python ' | ||||
'notebook1.ipynb --output nb2') | ||||
assert os.path.exists('nb2.py') | ||||
Jonathan Frederic
|
r11740 | |||
Jonathan Frederic
|
r11478 | def test_glob_explicit(self): | ||
""" | ||||
Can a search pattern be used along with matching explicit notebook names? | ||||
""" | ||||
with self.create_temp_cwd(['notebook*.ipynb']): | ||||
Jonathan Frederic
|
r12133 | self.call('nbconvert --log-level 0 --to python ' | ||
'*.ipynb notebook1.ipynb notebook2.ipynb') | ||||
Jonathan Frederic
|
r11639 | assert os.path.isfile('notebook1.py') | ||
assert os.path.isfile('notebook2.py') | ||||
Jonathan Frederic
|
r11478 | |||
def test_explicit_glob(self): | ||||
""" | ||||
Can explicit notebook names be used and then a matching search pattern? | ||||
""" | ||||
with self.create_temp_cwd(['notebook*.ipynb']): | ||||
Jonathan Frederic
|
r12133 | self.call('nbconvert --log-level 0 --to=python ' | ||
'notebook1.ipynb notebook2.ipynb *.ipynb') | ||||
Jonathan Frederic
|
r11639 | assert os.path.isfile('notebook1.py') | ||
assert os.path.isfile('notebook2.py') | ||||
Jonathan Frederic
|
r11478 | |||
def test_default_config(self): | ||||
""" | ||||
Does the default config work? | ||||
""" | ||||
with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']): | ||||
Jonathan Frederic
|
r12133 | self.call('nbconvert --log-level 0') | ||
Jonathan Frederic
|
r11639 | assert os.path.isfile('notebook1.py') | ||
assert not os.path.isfile('notebook2.py') | ||||
Jonathan Frederic
|
r11478 | |||
def test_override_config(self): | ||||
""" | ||||
Can the default config be overriden? | ||||
""" | ||||
Paul Ivanov
|
r11828 | with self.create_temp_cwd(['notebook*.ipynb', | ||
'ipython_nbconvert_config.py', | ||||
Jonathan Frederic
|
r11478 | 'override.py']): | ||
Jonathan Frederic
|
r12133 | self.call('nbconvert --log-level 0 --config="override.py"') | ||
Jonathan Frederic
|
r11639 | assert not os.path.isfile('notebook1.py') | ||
assert os.path.isfile('notebook2.py') | ||||
marcmolla
|
r13928 | |||
def test_accents_in_filename(self): | ||||
""" | ||||
Can notebook names include accents? | ||||
""" | ||||
MinRK
|
r14961 | with self.create_temp_cwd(): | ||
self.create_empty_notebook(u'nb1_análisis.ipynb') | ||||
marcmolla
|
r13928 | self.call('nbconvert --log-level 0 --to python nb1_*') | ||
assert os.path.isfile(u'nb1_análisis.py') | ||||
MinRK
|
r15440 | @dec.onlyif_cmds_exist('pdflatex', 'pandoc') | ||
def test_filename_accent_pdf(self): | ||||
marcmolla
|
r13928 | """ | ||
Generate PDFs if notebooks have an accent in their name? | ||||
""" | ||||
MinRK
|
r14961 | with self.create_temp_cwd(): | ||
self.create_empty_notebook(u'nb1_análisis.ipynb') | ||||
MinRK
|
r16418 | self.call('nbconvert --log-level 0 --to pdf "nb1_*"' | ||
' --PDFExporter.latex_count=1' | ||||
' --PDFExporter.verbose=True') | ||||
marcmolla
|
r13937 | assert os.path.isfile(u'nb1_análisis.pdf') | ||
jon
|
r16361 | |||
def test_cwd_plugin(self): | ||||
""" | ||||
Verify that an extension in the cwd can be imported. | ||||
""" | ||||
with self.create_temp_cwd(['hello.py']): | ||||
self.create_empty_notebook(u'empty.ipynb') | ||||
self.call('nbconvert empty --to html --NbConvertApp.writer_class=\'hello.HelloWriter\'') | ||||
assert os.path.isfile(u'hello.txt') | ||||