forked from napari/napari.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_docs.py
40 lines (37 loc) · 1.27 KB
/
test_docs.py
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
30
31
32
33
34
35
36
37
38
39
40
import re
from glob import glob
from pathlib import Path
import pytest
@pytest.mark.parametrize(
"fname",
[
f
for f in glob("**/*.md", recursive=True)
if "_build" not in f and Path(f).read_text(encoding="utf-8").startswith("---")
],
)
def test_doc_code_cells(fname, qapp, globalns=globals()):
"""Make sure that all code cells in documentation perform as expected."""
text = Path(fname).read_text()
code_cells = re.findall(r"```{code-cell}[^\n]+\n(.*?)`{3}", text, re.S)
for cell in code_cells:
nontags = [ln for ln in cell.splitlines() if not ln.startswith(":tags:")]
cell = "\n".join(nontags)
header = re.search(r"-{3}(.+?)-{3}", cell, re.S)
if header:
print("header", header)
cell = cell.replace(header.group(), "")
if "warns" in header.group():
with pytest.warns(None):
exec(cell, globalns)
continue
if "raises-exception" in header.group():
with pytest.raises(Exception):
exec(cell, globalns)
continue
exec(cell, globalns)
for window in qapp.topLevelWindows():
try:
window.close()
except Exception:
pass