Skip to content

Commit 0210f85

Browse files
committed
temp amend method to run tutorial
Signed-off-by: Xu, He <[email protected]>
1 parent 836bcc3 commit 0210f85

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

tests/lava/tutorials/test_tutorials.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TestTutorials(unittest.TestCase):
2525

2626
def _execute_notebook(
2727
self, base_dir: str, path: str
28-
) -> int:
28+
) -> ty.Tuple[ty.Type[nbformat.NotebookNode], ty.List[str]]:
2929
"""Execute a notebook via nbconvert and collect output.
3030
3131
Parameters
@@ -37,22 +37,23 @@ def _execute_notebook(
3737
3838
Returns
3939
-------
40-
int
41-
(return code)
40+
Tuple
41+
(parsed nbformat.NotebookNode object, list of execution errors)
4242
"""
4343

4444
cwd = os.getcwd()
4545
dir_name, notebook = os.path.split(path)
4646
try:
4747
env = self._update_pythonpath(base_dir, dir_name)
48-
result = self._convert_and_execute_notebook(notebook, env)
49-
errors = self._collect_errors_from_all_cells(result)
48+
nb = self._convert_and_execute_notebook(notebook, env)
49+
errors = self._collect_errors_from_all_cells(nb)
5050
except Exception as e:
51-
errors = -1
51+
nb = None
52+
errors = str(e)
5253
finally:
5354
os.chdir(cwd)
5455

55-
return errors
56+
return nb, errors
5657

5758
def _update_pythonpath(
5859
self, base_dir: str, dir_name: str
@@ -90,7 +91,7 @@ def _update_pythonpath(
9091

9192
def _convert_and_execute_notebook(
9293
self, notebook: str, env: ty.Dict[str, str]
93-
):
94+
) -> ty.Type[nbformat.NotebookNode]:
9495
"""Covert notebook and execute it.
9596
9697
Parameters
@@ -105,23 +106,25 @@ def _convert_and_execute_notebook(
105106
nb : nbformat.NotebookNode
106107
Notebook dict-like node with attribute-access
107108
"""
108-
with tempfile.NamedTemporaryFile(mode="w+t", suffix=".py") as fout:
109+
with tempfile.NamedTemporaryFile(mode="w+t", suffix=".ipynb") as fout:
109110
args = [
110111
"jupyter",
111112
"nbconvert",
112113
"--to",
113-
"python",
114+
"notebook",
115+
"--execute",
116+
"--ExecutePreprocessor.timeout=-1",
114117
"--output",
115-
fout.name[0:-3],
118+
fout.name,
116119
notebook,
117120
]
118121
subprocess.check_call(args, env=env) # noqa: S603
122+
119123
fout.seek(0)
120-
return subprocess.run(["ipython", "-c", fout.read()], # noqa # nosec
121-
env=env) # noqa # nosec
124+
return nbformat.read(fout, nbformat.current_nbformat)
122125

123126
def _collect_errors_from_all_cells(
124-
self, result
127+
self, nb: nbformat.NotebookNode
125128
) -> ty.List[str]:
126129
"""Collect errors from executed notebook.
127130
@@ -135,9 +138,13 @@ def _collect_errors_from_all_cells(
135138
List
136139
Collection of errors
137140
"""
138-
if result.returncode != 0:
139-
result.check_returncode()
140-
return result.returncode
141+
errors = []
142+
for cell in nb.cells:
143+
if "outputs" in cell:
144+
for output in cell["outputs"]:
145+
if output.output_type == "error":
146+
errors.append(output)
147+
return errors
141148

142149
def _run_notebook(self, notebook: str, e2e_tutorial: bool = False):
143150
"""Run a specific notebook
@@ -176,10 +183,22 @@ def _run_notebook(self, notebook: str, e2e_tutorial: bool = False):
176183

177184
# If the notebook is found execute it and store any errors
178185
for notebook_name in discovered_notebooks:
179-
errors = self._execute_notebook(
186+
nb, errors = self._execute_notebook(
180187
str(tutorials_directory), notebook_name
181188
)
182-
self.assertEqual(errors, 0)
189+
errors_joined = (
190+
"\n".join(errors) if isinstance(errors, list) else errors
191+
)
192+
if errors:
193+
errors_record[notebook_name] = (errors_joined, nb)
194+
195+
self.assertFalse(
196+
errors_record,
197+
"Failed to execute Jupyter Notebooks \
198+
with errors: \n {}".format(
199+
errors_record
200+
),
201+
)
183202
finally:
184203
os.chdir(cwd)
185204

0 commit comments

Comments
 (0)