2024-07-01 15:25:30 -06:00
|
|
|
# Copyright (c) 2024 Microsoft Corporation.
|
|
|
|
# Licensed under the MIT License
|
|
|
|
import subprocess
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import nbformat
|
|
|
|
import pytest
|
|
|
|
|
2024-08-20 16:19:37 -07:00
|
|
|
NOTEBOOKS_PATH = Path("examples_notebooks")
|
2024-08-23 15:18:35 -06:00
|
|
|
EXCLUDED_PATH = NOTEBOOKS_PATH / "community_contrib"
|
2024-07-01 15:25:30 -06:00
|
|
|
|
2024-08-23 15:18:35 -06:00
|
|
|
notebooks_list = [
|
|
|
|
notebook
|
|
|
|
for notebook in NOTEBOOKS_PATH.rglob("*.ipynb")
|
|
|
|
if EXCLUDED_PATH not in notebook.parents
|
|
|
|
]
|
2024-07-01 15:25:30 -06:00
|
|
|
|
|
|
|
|
|
|
|
def _notebook_run(filepath: Path):
|
|
|
|
"""Execute a notebook via nbconvert and collect output.
|
|
|
|
:returns execution errors
|
|
|
|
"""
|
2024-08-20 16:19:37 -07:00
|
|
|
args = [
|
|
|
|
"jupyter",
|
|
|
|
"nbconvert",
|
|
|
|
"--to",
|
|
|
|
"notebook",
|
|
|
|
"--execute",
|
|
|
|
"-y",
|
|
|
|
"--no-prompt",
|
|
|
|
"--stdout",
|
|
|
|
filepath.absolute().as_posix(),
|
|
|
|
]
|
|
|
|
notebook = subprocess.check_output(args)
|
|
|
|
nb = nbformat.reads(notebook, nbformat.current_nbformat)
|
2024-07-01 15:25:30 -06:00
|
|
|
|
|
|
|
return [
|
|
|
|
output
|
|
|
|
for cell in nb.cells
|
|
|
|
if "outputs" in cell
|
|
|
|
for output in cell["outputs"]
|
|
|
|
if output.output_type == "error"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("notebook_path", notebooks_list)
|
|
|
|
def test_notebook(notebook_path: Path):
|
|
|
|
assert _notebook_run(notebook_path) == []
|