File size: 1,206 Bytes
bb58820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import json, os, pathlib, sys
import nbformat
import pytest
from nbclient import NotebookClient

NB_DIR = pathlib.Path(__file__).resolve().parent.parent / "notebooks"
NOTEBOOKS = sorted(NB_DIR.glob("*.ipynb"))
NETWORK = {"01_query_the_mcp_registry.ipynb", "02_validate_a_server_json.ipynb"}


def test_six_notebooks_clean():
    assert len(NOTEBOOKS) == 6
    for p in NOTEBOOKS:
        nb = json.loads(p.read_text())
        assert set(nb["metadata"]) <= {"kernelspec", "language_info"}
        for c in nb["cells"]:
            assert c["metadata"] == {} and c.get("outputs", []) == [] and c.get("execution_count") is None
        src = " ".join("".join(c["source"]) for c in nb["cells"]).lower()
        assert "api_key" not in src and "token" not in src.replace("next_token", "")
        assert "MIT License" in "".join(nb["cells"][0]["source"])


@pytest.mark.parametrize("path", NOTEBOOKS, ids=lambda p: p.name)
def test_notebook_runs(path):
    if path.name in NETWORK and os.environ.get("OFFLINE"):
        pytest.skip("network notebook")
    nb = nbformat.read(path, as_version=4)
    NotebookClient(nb, timeout=120, kernel_name="python3").execute()  # runs on a copy in memory; file stays clean