-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_documentation_examples.py
More file actions
82 lines (71 loc) · 2.99 KB
/
test_documentation_examples.py
File metadata and controls
82 lines (71 loc) · 2.99 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import unittest
import os
import sys
import importlib
import subprocess
import time
from onnx_array_api import __file__ as onnx_array_api_file
from onnx_array_api.ext_test_case import ExtTestCase, is_windows
VERBOSE = 0
ROOT = os.path.realpath(os.path.abspath(os.path.join(onnx_array_api_file, "..", "..")))
def import_source(module_file_path, module_name):
if not os.path.exists(module_file_path):
raise FileNotFoundError(module_file_path)
module_spec = importlib.util.spec_from_file_location(module_name, module_file_path)
if module_spec is None:
raise FileNotFoundError(
"Unable to find '{}' in '{}'.".format(module_name, module_file_path)
)
module = importlib.util.module_from_spec(module_spec)
return module_spec.loader.exec_module(module)
class TestDocumentationExamples(ExtTestCase):
def run_test(self, fold: str, name: str, verbose=0) -> int:
ppath = os.environ.get("PYTHONPATH", "")
if not ppath:
os.environ["PYTHONPATH"] = ROOT
elif ROOT not in ppath:
sep = ";" if is_windows() else ":"
os.environ["PYTHONPATH"] = ppath + sep + ROOT
perf = time.perf_counter()
try:
mod = import_source(fold, os.path.splitext(name)[0])
assert mod is not None
except FileNotFoundError:
# try another way
cmds = [sys.executable, "-u", os.path.join(fold, name)]
p = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
res = p.communicate()
_out, err = res
st = err.decode("ascii", errors="ignore")
if st and "Traceback" in st:
if '"dot" not found in path.' in st:
# dot not installed, this part
# is tested in onnx framework
if verbose:
print(f"failed: {name!r} due to missing dot.")
return 0
raise AssertionError( # noqa: B904
"Example '{}' (cmd: {} - exec_prefix='{}') "
"failed due to\n{}"
"".format(name, cmds, sys.exec_prefix, st)
)
dt = time.perf_counter() - perf
if verbose:
print(f"{dt:.3f}: run {name!r}")
return 1
@classmethod
def add_test_methods(cls):
this = os.path.abspath(os.path.dirname(__file__))
fold = os.path.normpath(os.path.join(this, "..", "..", "_doc", "examples"))
found = os.listdir(fold)
for name in found:
if not name.startswith("plot_") or not name.endswith(".py"):
continue
short_name = os.path.split(os.path.splitext(name)[0])[-1]
def _test_(self, name=name):
res = self.run_test(fold, name, verbose=VERBOSE)
self.assertTrue(res)
setattr(cls, f"test_{short_name}", _test_)
TestDocumentationExamples.add_test_methods()
if __name__ == "__main__":
unittest.main(verbosity=2)