Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9e1617

Browse files
committed
Isolate nbagg test from user ipython profile.
My ~/.ipython profile causes test_backend_nbagg to fail (but is otherwise valid for my own use). To fix that, point the ipython directory to a temporary directory when running that test (per https://ipython.readthedocs.io/en/stable/config/intro.html#the-ipython-directory). While we're at it, also create the temporary output file in that directory -- that's quite likely to be necessary for the test to actually work on Windows, anyways (due to vagaries with writing from multiple processes to a NamedTemporaryFile).
1 parent 3320e23 commit f9e1617

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import os
12
from pathlib import Path
23
import subprocess
3-
import tempfile
4+
from tempfile import TemporaryDirectory
45

56
import pytest
67

@@ -9,25 +10,20 @@
910
# From https://blog.thedataincubator.com/2016/06/testing-jupyter-notebooks/
1011

1112

12-
def _notebook_run(nb_file):
13-
"""Execute a notebook via nbconvert and collect output.
14-
:returns (parsed nb object, execution errors)
15-
"""
16-
with tempfile.NamedTemporaryFile(suffix=".ipynb", mode='w+t') as fout:
17-
subprocess.check_call([
18-
"jupyter", "nbconvert", "--to", "notebook",
19-
"--execute", "--ExecutePreprocessor.timeout=500",
20-
"--output", fout.name, nb_file,
21-
])
22-
fout.seek(0)
23-
nb = nbformat.read(fout, nbformat.current_nbformat)
13+
def test_ipynb():
14+
nb_path = Path(__file__).parent / 'test_nbagg_01.ipynb'
15+
16+
with TemporaryDirectory() as tmpdir:
17+
out_path = Path(tmpdir, "out.ipynb")
18+
subprocess.check_call(
19+
["jupyter", "nbconvert", "--to", "notebook",
20+
"--execute", "--ExecutePreprocessor.timeout=500",
21+
"--output", str(out_path), str(nb_path)],
22+
env={**os.environ, "IPYTHONDIR": tmpdir})
23+
with out_path.open() as out:
24+
nb = nbformat.read(out, nbformat.current_nbformat)
2425

2526
errors = [output for cell in nb.cells if "outputs" in cell
2627
for output in cell["outputs"]
2728
if output.output_type == "error"]
28-
return nb, errors
29-
30-
31-
def test_ipynb():
32-
nb, errors = _notebook_run(Path(__file__).parent / 'test_nbagg_01.ipynb')
33-
assert errors == []
29+
assert not errors

0 commit comments

Comments
 (0)