Uploaded Test files

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-12 11:05:57 -05:00
parent f584ad9d97
commit 2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions

View file

@ -0,0 +1,180 @@
"""Base test class for nbconvert"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import io
import os
import glob
import shlex
import shutil
import sys
import unittest
import nbconvert
from subprocess import Popen, PIPE
from nbformat import v4, write
from testpath.tempdir import TemporaryWorkingDirectory
class TestsBase(unittest.TestCase):
"""Base tests class. Contains useful fuzzy comparison and nbconvert
functions."""
def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True,
fuzzy_spacing=True, ignore_spaces=False,
ignore_newlines=False, case_sensitive=False, leave_padding=False):
"""
Performs a fuzzy comparison of two strings. A fuzzy comparison is a
comparison that ignores insignificant differences in the two comparands.
The significance of certain differences can be specified via the keyword
parameters of this method.
"""
if not leave_padding:
a = a.strip()
b = b.strip()
if ignore_newlines:
a = a.replace('\n', '')
b = b.replace('\n', '')
if newlines_are_spaces:
a = a.replace('\n', ' ')
b = b.replace('\n', ' ')
if tabs_are_spaces:
a = a.replace('\t', ' ')
b = b.replace('\t', ' ')
if ignore_spaces:
a = a.replace(' ', '')
b = b.replace(' ', '')
if fuzzy_spacing:
a = self.recursive_replace(a, ' ', ' ')
b = self.recursive_replace(b, ' ', ' ')
if not case_sensitive:
a = a.lower()
b = b.lower()
self.assertEqual(a, b)
def recursive_replace(self, text, search, replacement):
"""
Performs a recursive replacement operation. Replaces all instances
of a search string in a text string with a replacement string until
the search string no longer exists. Recursion is needed because the
replacement string may generate additional search strings.
For example:
Replace "ii" with "i" in the string "Hiiii" yields "Hii"
Another replacement cds "Hi" (the desired output)
Parameters
----------
text : string
Text to replace in.
search : string
String to search for within "text"
replacement : string
String to replace "search" with
"""
while search in text:
text = text.replace(search, replacement)
return text
def create_temp_cwd(self, copy_filenames=None):
temp_dir = TemporaryWorkingDirectory()
#Copy the files if requested.
if copy_filenames is not None:
self.copy_files_to(copy_filenames, dest=temp_dir.name)
#Return directory handler
return temp_dir
@classmethod
def merge_dicts(cls, *dict_args):
# Because this is annoying to do inline
outcome = {}
for d in dict_args:
outcome.update(d)
return outcome
def create_empty_notebook(self, path):
nb = v4.new_notebook()
with io.open(path, 'w', encoding='utf-8') as f:
write(nb, f, 4)
def copy_files_to(self, copy_filenames, dest='.'):
"Copy test files into the destination directory"
if not os.path.isdir(dest):
os.makedirs(dest)
files_path = self._get_files_path()
for pattern in copy_filenames:
files = glob.glob(os.path.join(files_path, pattern))
assert files
for match in files:
shutil.copyfile(match, os.path.join(dest, os.path.basename(match)))
def _get_files_path(self):
#Get the relative path to this module in the IPython directory.
names = self.__module__.split('.')[1:-1]
names.append('files')
#Build a path using the nbconvert directory and the relative path we just
#found.
path = os.path.dirname(nbconvert.__file__)
return os.path.join(path, *names)
def nbconvert(self, parameters, ignore_return_code=False, stdin=None):
"""
Run nbconvert as a shell command, listening for both Errors and
non-zero return codes. Returns the tuple (stdout, stderr) of
output produced during the nbconvert run.
Parameters
----------
parameters : str, list(str)
List of parameters to pass to IPython.
ignore_return_code : optional bool (default False)
Throw an OSError if the return code
"""
cmd = [sys.executable, '-m', 'nbconvert']
if sys.platform == 'win32':
if isinstance(parameters, (str,)):
cmd = ' '.join(cmd) + ' ' + parameters
else:
cmd = ' '.join(cmd + parameters)
else:
if isinstance(parameters, (str,)):
parameters = shlex.split(parameters)
cmd += parameters
p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
stdout, stderr = p.communicate(input=stdin)
if not (p.returncode == 0 or ignore_return_code):
raise OSError(stderr.decode('utf8', 'replace'))
return stdout.decode('utf8', 'replace'), stderr.decode('utf8', 'replace')
def assert_big_text_equal(a, b, chunk_size=80):
"""assert that large strings are equal
Zooms in on first chunk that differs,
to give better info than vanilla assertEqual for large text blobs.
"""
for i in range(0, len(a), chunk_size):
chunk_a = a[i:i + chunk_size]
chunk_b = b[i:i + chunk_size]
assert chunk_a == chunk_b, "[offset: %i]\n%r != \n%r" % (i, chunk_a, chunk_b)
if len(a) > len(b):
raise AssertionError("Length doesn't match (%i > %i). Extra text:\n%r" % (
len(a), len(b), a[len(b):]))
elif len(a) < len(b):
raise AssertionError("Length doesn't match (%i < %i). Extra text:\n%r" % (
len(a), len(b), a[len(b):]))

View file

@ -0,0 +1,5 @@
[nbconvert.exporters]
entrypoint_test = eptest:DummyExporter
[nbconvert.exporters.script]
dummy = eptest:DummyScriptExporter

View file

@ -0,0 +1,8 @@
from nbconvert.exporters import Exporter
class DummyExporter(Exporter):
pass
class DummyScriptExporter(Exporter):
def from_notebook_node(self, nb, resources=None, **kw):
return 'dummy-script-exported', resources

View file

@ -0,0 +1,25 @@
"""
Module that define a custom exporter just to test the ability to invoke
nbconvert with full qualified name
"""
from traitlets import default
from nbconvert.exporters.html import HTMLExporter
class MyExporter(HTMLExporter):
"""
My custom exporter
"""
@default('file_extension')
def _file_extension_default(self):
"""
The new file extension is `.test_ext`
"""
return '.test_ext'
@default('template_extension')
def _template_extension_default(self):
return '.html.j2'

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -0,0 +1,7 @@
from nbconvert.writers.base import WriterBase
class HelloWriter(WriterBase):
def write(self, output, resources, notebook_name=None, **kw):
with open('hello.txt', 'w') as outfile:
outfile.write('hello world')

View file

@ -0,0 +1,6 @@
c = get_config()
#Export all the notebooks in the current directory to the sphinx_howto format.
c.NbConvertApp.notebooks = ['notebook1.ipynb']
c.NbConvertApp.export_format = 'python'

View file

@ -0,0 +1,32 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![image](testimage.png)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# A simple SymPy example"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"First we import SymPy and initialize printing:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"from sympy import init_printing\n",
"from sympy import *\n",
"init_printing()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Create a few symbols:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"x,y,z = symbols('x y z')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Here is a basic expression:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAKMAAAAZBAMAAACvE4OgAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHarIkSJZt3NVLsy\nme8Q6PJIAAACz0lEQVRIDa1UTWjUQBT+ZpvdzW7TGlrxItjYSg/C6vbiDwjmoCgUpHioPYhdqig9\nFJYiPYmW4klB14NgFGnw4EHpj7UgUtTFXhSEBgVBxIOFggWVrrUqiMY3mZkkLNIK7oN575vvvfky\n8yYJIGzgkSlRrULKrivVSkvq6LbxtcaSjV3aSo0lgWyl5pK69V+SRlEsPxNTGYhhDrV3M2Ue2etc\nEDmuMmM+IjolrCuHXNoLoQDNSAXdzbjsfFVKTY1vCgFXFIxenG4cFSSzRewAPnN0FugXjPDr45MQ\nJwoKtitgXL9zT+CsJeIHYG+Z4H1gwhRU4G/FcAQbbYU3KdDo+0sCK8lRU0guA72uKqMYk9RehHxP\niDIu0NS2v90KGShJYi7T7tgvkrQ2vIT2XtRISWNra6lzGc8/PW3ji4PL7Vmge095YIX0iB71NCaZ\n5N3XyM0VCuNIyFNIyY3AMG/KDUvjn90DGmwq9wpIl5AyU5WsTYy0aJf6JFGB5An3Der5jExKHjNR\n4JKPge/EXqDBoOXpkxkmkJHFfAFRVhDIveWA0S57N2Me6yw+DSX1n1uCq3sIfCF2IcjNkjeWyKli\nginHubboOB4vSNAjyaiXE26ygrkyTfod55Lj3CTE+n2P73ImJpnk6wJJKjYJSwt3OQbNJu4icM5s\nKGGbzMuD70N6JSbJD44x7pLDyJrbkfiLpOEhYVMJSVEj83x5YFLyNrAzJsmvJ+uhLrieXvcJDshy\nHtQuD54c2IWWEnSXfUTDZJJfAjcpOW5imp9aHvw4ZZ4NDV4FGjw0tzadKgbFwinJUd//AT0P1tdW\nBtuRU39oKdk9ONQ163fM+nvu/s4D/FX30otdQIZGlSnJKpq6KUxKVqV1WxGHFIhishjhEO1Gi3r4\nkZCMg+hH1henV8EjmFoly1PTMs/Uadaox+FceY2STpmvt9co/Pe0Jvt1GvgDK/Osw/4jQ4wAAAAA\nSUVORK5CYII=\n",
"text/latex": [
"$$x^{2} + 2.0 y + \\sin{\\left (z \\right )}$$"
],
"text/plain": [
" 2 \n",
"x + 2.0⋅y + sin(z)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e = x**2 + 2.0*y + sin(z); e"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAABQAAAAOBAMAAADd6iHDAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAAgElEQVQIHWNgVDJ2YICAMAb2H1BmKgPDTChzFgNDvgOEvT8AzgQKrA9gPZPYUwNk\ncXxnCGd4dWA1kMllwFDKUB9wEchUZmAIYNgMZDDwJIDIPyDiEgOjAAPLFwZWBhYFBh6BqzwfGI4y\nSJUXZXH8Zf7A+IBh////v1hzjh5/xwAAW80hUDE8HYkAAAAASUVORK5CYII=\n",
"text/latex": [
"$$2 x$$"
],
"text/plain": [
"2⋅x"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"diff(e, x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAALsAAAAZBAMAAACbakK8AAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHarIkSJZt3NVLsy\nme8Q6PJIAAADAklEQVRIDbVVS2gTURQ90/wmk0k6tCJCsR1SKShIsxE3CgNWBKUxq9qFmqFqShfF\nUKQrkaDiF0pcCKYgBBcuBLV+wIWKARe6kQ4UhNKKWdiF4KIptmA/xPvmzZuMxdYUzIPcd+655568\nvLlJAL6G32oOasQWNHz5Rvg6nrKh/mygfSzlX2ygPaBUGmov6//NXs1yq4sex2EPrsHemTd2snNg\ntkb+Cx1zBL6SqwxZLvQAKYHzKZaPY4fh4TeHd0S5Nox9OClItm/jiU9DrEwwVEawpiVis9VkimqX\nAOr4o2cCs/0BT2I5+FYJRhJbePQxgzcD7QLEqtV5gdnu2Icr3L45gcCyt74Z7neL4SLQ0nm4S+dM\nYCz1gSPHnhKZDWyHhcCCNKwjqaF/TkwGl0L6nClie/wc1D1xdoNsSLhT0IJkhi7Lzr22xb8keE/N\nPm0Sc9yEuhRUyuiG9HzvFNeImCyq39SriOhtQI7IV/TiTqE8glqwohjE0NJwiANxOZTdZoxtfzSa\nx2tI8DtHcKQoQFmV6f1XT2swibxFL+6k5EgenhBCqKLTPX3ULnaYdDlaTMcCSd8zuXTvBq2bJUJr\nlE4WgSV5ZRdBzLFgO6nzhJp1ltvrlB2HCoWxQuG+jTvt2GxBWUZaU2mMApZNuSHA3vJpCliRhqqs\nZtvbTrb9ZIk+i70Ut1OcnpgeKskTCFUwjaYy8Jhr3eiefq0HIfa7yC6HOwVyULRuNDn21JngbcL+\nE8A+MNnSxb+w59+Cj2tELJBbjEZr8SGwn0j2aLkTPdp08R2OcKV6fXB3ikPH3n8tM5WTfrETtZcw\ng3QWH0dH7nKNiMkszqo/EDafaHhJ5Bm6ee4UtdAabxnMcmUUl0SnYx+uVqs5XAGN9QGgdeCrASv0\n3TmCsJcOdhnozexD38goK9HXynEKr1OKDs9guhQD039kGySyIQpJAdbvJ9YTlPvyUl3/aLUf34G/\nuGxIyXpE37DoLbAHwJaU53t9MRCfrU8o/k4iRn36Lar8Wd5wAfgN4R6xelyy/ssAAAAASUVORK5C\nYII=\n",
"text/latex": [
"$$x^{2} z + 2.0 y z - \\cos{\\left (z \\right )}$$"
],
"text/plain": [
" 2 \n",
"x ⋅z + 2.0⋅y⋅z - cos(z)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"integrate(e, z)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": []
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 1
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,55 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Notebook with errors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook contains a cell which deliberately throws an exception. This is to test if `nbconvert` stops conversion if the flag `--execute` is given without `--allow-errors`. In the cells before and after the one which raises the exception we compute a couple of numbers. If they exist in the output we know that the respective cells were executed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(\"Hello world, my number is {}\".format(24 - 1))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(\"Some text before the error\")\n",
"raise RuntimeError(\"This is a deliberate exception\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(\"The answer to the question about life, the universe and everything is: {}\".format(43 - 1))"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,30 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"function foobar(x)\n",
" 100x\n",
"end"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.4.0-dev",
"language": "julia",
"name": "julia-0.4"
},
"language_info": {
"name": "julia",
"version": "0.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View file

@ -0,0 +1,89 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"mycelltag",
"mysecondcelltag"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this cell should have tags in html output\n"
]
}
],
"source": [
"print(\"this cell should have tags in html output\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this cell should NOT have tags in html output\n"
]
}
],
"source": [
"print(\"this cell should NOT have tags in html output\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": [
"mymarkdowncelltag"
]
},
"source": [
"This markdown cell should have tags in the html output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This markdown cell should **not** have tags in the html output"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View file

@ -0,0 +1,6 @@
c = get_config()
#Export all the notebooks in the current directory to the sphinx_howto format.
c.NbConvertApp.notebooks = ['notebook2.ipynb']
c.NbConvertApp.export_format = 'python'

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -0,0 +1,550 @@
# -*- coding: utf-8 -*-
"""Test NbConvertApp"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import io
import nbformat
from .base import TestsBase
from ..postprocessors import PostProcessorBase
from ..tests.utils import onlyif_cmds_exist
from nbconvert import nbconvertapp
from nbconvert.exporters import Exporter
from traitlets.tests.utils import check_help_all_output
from testpath import tempdir
import pytest
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
class DummyPost(PostProcessorBase):
def postprocess(self, filename):
print("Dummy:%s" % filename)
class TestNbConvertApp(TestsBase):
"""Collection of NbConvertApp tests"""
def test_notebook_help(self):
"""Will help show if no notebooks are specified?"""
with self.create_temp_cwd():
out, err = self.nbconvert('--log-level 0', ignore_return_code=True)
self.assertIn("--help-all", out)
def test_help_output(self):
"""ipython nbconvert --help-all works"""
check_help_all_output('nbconvert')
def test_glob(self):
"""
Do search patterns work for notebook names?
"""
with self.create_temp_cwd(['notebook*.ipynb']):
self.nbconvert('--to python *.ipynb --log-level 0')
assert os.path.isfile('notebook1.py')
assert os.path.isfile('notebook2.py')
def test_glob_subdir(self):
"""
Do search patterns work for subdirectory notebook names?
"""
with self.create_temp_cwd():
self.copy_files_to(['notebook*.ipynb'], 'subdir/')
self.nbconvert('--to python --log-level 0 ' +
os.path.join('subdir', '*.ipynb'))
assert os.path.isfile(os.path.join('subdir', 'notebook1.py'))
assert os.path.isfile(os.path.join('subdir', 'notebook2.py'))
def test_build_dir(self):
"""build_directory affects export location"""
with self.create_temp_cwd():
self.copy_files_to(['notebook*.ipynb'], 'subdir/')
self.nbconvert('--to python --log-level 0 --output-dir . ' +
os.path.join('subdir', '*.ipynb'))
assert os.path.isfile('notebook1.py')
assert os.path.isfile('notebook2.py')
def test_convert_full_qualified_name(self):
"""
Test that nbconvert can convert file using a full qualified name for a
package, import and use it.
"""
with self.create_temp_cwd():
self.copy_files_to(['notebook*.ipynb'], 'subdir')
self.nbconvert('--to nbconvert.tests.fake_exporters.MyExporter --log-level 0 ' +
os.path.join('subdir', '*.ipynb'))
assert os.path.isfile(os.path.join('subdir', 'notebook1.test_ext'))
assert os.path.isfile(os.path.join('subdir', 'notebook2.test_ext'))
def test_explicit(self):
"""
Do explicit notebook names work?
"""
with self.create_temp_cwd(['notebook*.ipynb']):
self.nbconvert('--log-level 0 --to python notebook2')
assert not os.path.isfile('notebook1.py')
assert os.path.isfile('notebook2.py')
def test_clear_output(self):
"""
Can we clear outputs?
"""
with self.create_temp_cwd(['notebook*.ipynb']) as td:
self.nbconvert('--clear-output notebook1')
assert os.path.isfile('notebook1.ipynb')
with open('notebook1.ipynb', encoding='utf8') as f:
nb = nbformat.read(f, 4)
for cell in nb.cells:
# Skip markdown cells
if 'outputs' in cell:
assert cell.outputs == []
def test_absolute_template_file(self):
"""--template-file '/path/to/template.tpl'"""
with self.create_temp_cwd(['notebook*.ipynb']), tempdir.TemporaryDirectory() as td:
template = os.path.join(td, 'mytemplate.tpl')
test_output = 'success!'
with open(template, 'w') as f:
f.write(test_output)
self.nbconvert('--log-level 0 notebook2 --to html --template-file %s' % template)
assert os.path.isfile('notebook2.html')
with open('notebook2.html') as f:
text = f.read()
assert text == test_output
def test_relative_template_file(self):
"""Test --template-file 'relative/path.tpl'"""
with self.create_temp_cwd(['notebook*.ipynb']):
os.mkdir('relative')
template = os.path.join('relative', 'path.tpl')
test_output = 'success!'
with open(template, 'w') as f:
f.write(test_output)
self.nbconvert('--log-level 0 notebook2 --to html --template-file %s' % template)
assert os.path.isfile('notebook2.html')
with open('notebook2.html') as f:
text = f.read()
assert text == test_output
@onlyif_cmds_exist('pandoc', 'xelatex')
def test_filename_spaces(self):
"""
Generate PDFs with graphics if notebooks have spaces in the name?
"""
with self.create_temp_cwd(['notebook2.ipynb']):
os.rename('notebook2.ipynb', 'notebook with spaces.ipynb')
self.nbconvert('--log-level 0 --to pdf'
' "notebook with spaces"'
' --PDFExporter.latex_count=1'
' --PDFExporter.verbose=True'
)
assert os.path.isfile('notebook with spaces.pdf')
def test_webpdf_with_chromium(self):
"""
Generate PDFs if chromium allowed to be downloaded?
"""
with self.create_temp_cwd(['notebook2.ipynb']):
self.nbconvert('--to webpdf '
'--allow-chromium-download '
'"notebook2"'
)
assert os.path.isfile('notebook2.pdf')
@onlyif_cmds_exist('pandoc', 'xelatex')
def test_pdf(self):
"""
Check to see if pdfs compile, even if strikethroughs are included.
"""
with self.create_temp_cwd(['notebook2.ipynb']):
self.nbconvert('--log-level 0 --to pdf'
' "notebook2"'
' --PDFExporter.latex_count=1'
' --PDFExporter.verbose=True'
)
assert os.path.isfile('notebook2.pdf')
def test_post_processor(self):
"""Do post processors work?"""
with self.create_temp_cwd(['notebook1.ipynb']):
out, err = self.nbconvert('--log-level 0 --to python notebook1 '
'--post nbconvert.tests.test_nbconvertapp.DummyPost')
self.assertIn('Dummy:notebook1.py', out)
@onlyif_cmds_exist('pandoc')
def test_spurious_cr(self):
"""Check for extra CR characters"""
with self.create_temp_cwd(['notebook2.ipynb']):
self.nbconvert('--log-level 0 --to latex notebook2')
assert os.path.isfile('notebook2.tex')
with open('notebook2.tex') as f:
tex = f.read()
self.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'))
@onlyif_cmds_exist('pandoc')
def test_png_base64_html_ok(self):
"""Is embedded png data well formed in HTML?"""
with self.create_temp_cwd(['notebook2.ipynb']):
self.nbconvert('--log-level 0 --to HTML '
'notebook2.ipynb --template lab')
assert os.path.isfile('notebook2.html')
with open('notebook2.html') as f:
assert "data:image/png;base64,b'" not in f.read()
@onlyif_cmds_exist('pandoc')
def test_template(self):
"""
Do export templates work?
"""
with self.create_temp_cwd(['notebook2.ipynb']):
self.nbconvert('--log-level 0 --to slides '
'notebook2.ipynb')
assert os.path.isfile('notebook2.slides.html')
with open('notebook2.slides.html') as f:
assert '/reveal.css' in f.read()
def test_output_ext(self):
"""test --output=outputfile[.ext]"""
with self.create_temp_cwd(['notebook1.ipynb']):
self.nbconvert('--log-level 0 --to python '
'notebook1.ipynb --output nb.py')
assert os.path.exists('nb.py')
self.nbconvert('--log-level 0 --to python '
'notebook1.ipynb --output nb2')
assert os.path.exists('nb2.py')
def test_glob_explicit(self):
"""
Can a search pattern be used along with matching explicit notebook names?
"""
with self.create_temp_cwd(['notebook*.ipynb']):
self.nbconvert('--log-level 0 --to python '
'*.ipynb notebook1.ipynb notebook2.ipynb')
assert os.path.isfile('notebook1.py')
assert os.path.isfile('notebook2.py')
def test_explicit_glob(self):
"""
Can explicit notebook names be used and then a matching search pattern?
"""
with self.create_temp_cwd(['notebook*.ipynb']):
self.nbconvert('--log-level 0 --to=python '
'notebook1.ipynb notebook2.ipynb *.ipynb')
assert os.path.isfile('notebook1.py')
assert os.path.isfile('notebook2.py')
def test_default_config(self):
"""
Does the default config work?
"""
with self.create_temp_cwd(['notebook*.ipynb', 'jupyter_nbconvert_config.py']):
self.nbconvert('--log-level 0')
assert os.path.isfile('notebook1.py')
assert not os.path.isfile('notebook2.py')
def test_override_config(self):
"""
Can the default config be overridden?
"""
with self.create_temp_cwd(['notebook*.ipynb',
'jupyter_nbconvert_config.py',
'override.py']):
self.nbconvert('--log-level 0 --config="override.py"')
assert not os.path.isfile('notebook1.py')
assert os.path.isfile('notebook2.py')
def test_accents_in_filename(self):
"""
Can notebook names include accents?
"""
with self.create_temp_cwd():
self.create_empty_notebook(u'nb1_análisis.ipynb')
self.nbconvert('--log-level 0 --to Python nb1_*')
assert os.path.isfile(u'nb1_análisis.py')
@onlyif_cmds_exist('xelatex', 'pandoc')
def test_filename_accent_pdf(self):
"""
Generate PDFs if notebooks have an accent in their name?
"""
with self.create_temp_cwd():
self.create_empty_notebook(u'nb1_análisis.ipynb')
self.nbconvert('--log-level 0 --to pdf "nb1_*"'
' --PDFExporter.latex_count=1'
' --PDFExporter.verbose=True')
assert os.path.isfile(u'nb1_análisis.pdf')
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.nbconvert('empty --to html --NbConvertApp.writer_class=\'hello.HelloWriter\'')
assert os.path.isfile(u'hello.txt')
def test_output_suffix(self):
"""
Verify that the output suffix is applied
"""
with self.create_temp_cwd():
self.create_empty_notebook('empty.ipynb')
self.nbconvert('empty.ipynb --to notebook')
assert os.path.isfile('empty.nbconvert.ipynb')
def test_different_build_dir(self):
"""
Verify that the output suffix is not applied
"""
with self.create_temp_cwd():
self.create_empty_notebook('empty.ipynb')
os.mkdir('output')
self.nbconvert(
'empty.ipynb --to notebook '
'--FilesWriter.build_directory=output')
assert os.path.isfile('output/empty.ipynb')
def test_inplace(self):
"""
Verify that the notebook is converted in place
"""
with self.create_temp_cwd():
self.create_empty_notebook('empty.ipynb')
self.nbconvert('empty.ipynb --inplace')
assert os.path.isfile('empty.ipynb')
assert not os.path.isfile('empty.nbconvert.ipynb')
assert not os.path.isfile('empty.html')
def test_no_prompt(self):
"""
Verify that the html has no prompts when given --no-prompt.
"""
with self.create_temp_cwd(["notebook1.ipynb"]):
self.nbconvert('notebook1.ipynb --log-level 0 --no-prompt --to html')
assert os.path.isfile('notebook1.html')
with open("notebook1.html",'r') as f:
text = f.read()
assert "In&nbsp;[" not in text
assert "Out[6]" not in text
self.nbconvert('notebook1.ipynb --log-level 0 --to html')
assert os.path.isfile('notebook1.html')
with open("notebook1.html",'r') as f:
text2 = f.read()
assert "In&nbsp;[" in text2
assert "Out[6]" in text2
def test_cell_tag_output(self):
"""
Verify that the html has tags in cell attributes if they exist.
"""
with self.create_temp_cwd(["notebook_tags.ipynb"]):
self.nbconvert('notebook_tags.ipynb --log-level 0 --to html')
assert os.path.isfile('notebook_tags.html')
with open("notebook_tags.html",'r') as f:
text = f.read()
assert 'celltag_mycelltag celltag_mysecondcelltag' in text
assert 'celltag_mymarkdowncelltag' in text
def test_no_input(self):
"""
Verify that the html has no input when given --no-input.
"""
with self.create_temp_cwd(["notebook1.ipynb"]):
self.nbconvert('notebook1.ipynb --log-level 0 --no-input --to html')
assert os.path.isfile('notebook1.html')
with open("notebook1.html",'r') as f:
text = f.read()
assert "In&nbsp;[" not in text
assert "Out[6]" not in text
assert ('<span class="n">x</span>'
'<span class="p">,</span>'
'<span class="n">y</span>'
'<span class="p">,</span>'
'<span class="n">z</span> '
'<span class="o">=</span> '
'<span class="n">symbols</span>'
'<span class="p">(</span>'
'<span class="s1">&#39;x y z&#39;</span>'
'<span class="p">)</span>') not in text
self.nbconvert('notebook1.ipynb --log-level 0 --to html')
assert os.path.isfile('notebook1.html')
with open("notebook1.html",'r') as f:
text2 = f.read()
assert "In&nbsp;[" in text2
assert "Out[6]" in text2
assert ('<span class="n">x</span>'
'<span class="p">,</span>'
'<span class="n">y</span>'
'<span class="p">,</span>'
'<span class="n">z</span> '
'<span class="o">=</span> '
'<span class="n">symbols</span>'
'<span class="p">(</span>'
'<span class="s1">&#39;x y z&#39;</span>'
'<span class="p">)</span>') in text2
def test_allow_errors(self):
"""
Verify that conversion is aborted with '--execute' if an error is
encountered, but that conversion continues if '--allow-errors' is
used in addition.
"""
with self.create_temp_cwd(['notebook3*.ipynb']):
# Convert notebook containing a cell that raises an error,
# both without and with cell execution enabled.
output1, _ = self.nbconvert('--to markdown --stdout notebook3*.ipynb') # no cell execution
output2, _ = self.nbconvert('--to markdown --allow-errors --stdout notebook3*.ipynb') # no cell execution; --allow-errors should have no effect
output3, _ = self.nbconvert('--execute --allow-errors --to markdown --stdout notebook3*.ipynb') # with cell execution; errors are allowed
# Un-executed outputs should not contain either
# of the two numbers computed in the notebook.
assert '23' not in output1
assert '42' not in output1
assert '23' not in output2
assert '42' not in output2
# Executed output should contain both numbers.
assert '23' in output3
assert '42' in output3
# Executing the notebook should raise an exception if --allow-errors is not specified
with pytest.raises(OSError):
self.nbconvert('--execute --to markdown --stdout notebook3*.ipynb')
def test_errors_print_traceback(self):
"""
Verify that the stderr output contains the traceback of the cell execution exception.
"""
with self.create_temp_cwd(['notebook3_with_errors.ipynb']):
_, error_output = self.nbconvert('--execute --to markdown --stdout notebook3_with_errors.ipynb',
ignore_return_code=True)
assert 'print("Some text before the error")' in error_output
assert 'raise RuntimeError("This is a deliberate exception")' in error_output
assert 'RuntimeError: This is a deliberate exception' in error_output
def test_fenced_code_blocks_markdown(self):
"""
Verify that input cells use fenced code blocks with the language
name in nb.metadata.kernelspec.language, if that exists
"""
with self.create_temp_cwd(["notebook1*.ipynb"]):
# this notebook doesn't have nb.metadata.kernelspec, so it should
# just do a fenced code block, with no language
output1, _ = self.nbconvert('--to markdown --stdout notebook1.ipynb')
assert '```python' not in output1 # shouldn't have language
assert "```" in output1 # but should have fenced blocks
with self.create_temp_cwd(["notebook_jl*.ipynb"]):
output2, _ = self.nbconvert('--to markdown --stdout notebook_jl.ipynb')
assert '```julia' in output2 # shouldn't have language
assert "```" in output2 # but should also plain ``` to close cell
def test_convert_from_stdin_to_stdout(self):
"""
Verify that conversion can be done via stdin to stdout
"""
with self.create_temp_cwd(["notebook1.ipynb"]):
with io.open('notebook1.ipynb') as f:
notebook = f.read().encode()
output1, _ = self.nbconvert('--to markdown --stdin --stdout', stdin=notebook)
assert '```python' not in output1 # shouldn't have language
assert "```" in output1 # but should have fenced blocks
def test_convert_from_stdin(self):
"""
Verify that conversion can be done via stdin.
"""
with self.create_temp_cwd(["notebook1.ipynb"]):
with io.open('notebook1.ipynb') as f:
notebook = f.read().encode()
self.nbconvert('--to markdown --stdin', stdin=notebook)
assert os.path.isfile("notebook.md") # default name for stdin input
with io.open('notebook.md') as f:
output1 = f.read()
assert '```python' not in output1 # shouldn't have language
assert "```" in output1 # but should have fenced blocks
@onlyif_cmds_exist('pandoc', 'xelatex')
def test_linked_images(self):
"""
Generate PDFs with an image linked in a markdown cell
"""
with self.create_temp_cwd(['latex-linked-image.ipynb', 'testimage.png']):
self.nbconvert('--to pdf latex-linked-image.ipynb')
assert os.path.isfile('latex-linked-image.pdf')
@onlyif_cmds_exist('pandoc')
def test_embedded_jpeg(self):
"""
Verify that latex conversion succeeds
with a notebook with an embedded .jpeg
"""
with self.create_temp_cwd(['notebook4_jpeg.ipynb',
'containerized_deployments.jpeg']):
self.nbconvert('--to latex notebook4_jpeg.ipynb')
assert os.path.isfile('notebook4_jpeg.tex')
@onlyif_cmds_exist('pandoc')
def test_markdown_display_priority(self):
"""
Check to see if markdown conversion embeds PNGs,
even if an (unsupported) PDF is present.
"""
with self.create_temp_cwd(['markdown_display_priority.ipynb']):
self.nbconvert('--log-level 0 --to markdown '
'"markdown_display_priority.ipynb"')
assert os.path.isfile('markdown_display_priority.md')
with io.open('markdown_display_priority.md') as f:
markdown_output = f.read()
assert ("markdown_display_priority_files/"
"markdown_display_priority_0_1.png") in markdown_output
@onlyif_cmds_exist('pandoc')
def test_write_figures_to_custom_path(self):
"""
Check if figure files are copied to configured path.
"""
def fig_exists(path):
return (len(os.listdir(path)) > 0)
# check absolute path
with self.create_temp_cwd(['notebook4_jpeg.ipynb',
'containerized_deployments.jpeg']):
output_dir = tempdir.TemporaryDirectory()
path = os.path.join(output_dir.name, 'files')
self.nbconvert(
'--log-level 0 notebook4_jpeg.ipynb --to rst '
'--NbConvertApp.output_files_dir={}'
.format(path))
assert fig_exists(path)
output_dir.cleanup()
# check relative path
with self.create_temp_cwd(['notebook4_jpeg.ipynb',
'containerized_deployments.jpeg']):
self.nbconvert(
'--log-level 0 notebook4_jpeg.ipynb --to rst '
'--NbConvertApp.output_files_dir=output')
assert fig_exists('output')
# check default path with notebook name
with self.create_temp_cwd(['notebook4_jpeg.ipynb',
'containerized_deployments.jpeg']):
self.nbconvert(
'--log-level 0 notebook4_jpeg.ipynb --to rst')
assert fig_exists('notebook4_jpeg_files')

View file

@ -0,0 +1,12 @@
import pytest
from shutil import which
def onlyif_cmds_exist(*commands):
"""
Decorator to skip test when at least one of `commands` is not found.
"""
for cmd in commands:
if not which(cmd):
return pytest.mark.skip("This test runs only if command '{0}' "
"is installed".format(cmd))
return lambda f: f