|
| 1 | +import unittest |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import importlib.util |
| 5 | +import subprocess |
| 6 | +import time |
| 7 | +from onnx_diagnostic import __file__ as onnx_diagnostic_file |
| 8 | +from onnx_diagnostic.ext_test_case import ( |
| 9 | + ExtTestCase, |
| 10 | + is_windows, |
| 11 | + has_torch, |
| 12 | + ignore_errors, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +VERBOSE = 0 |
| 17 | +ROOT = os.path.realpath(os.path.abspath(os.path.join(onnx_diagnostic_file, "..", ".."))) |
| 18 | + |
| 19 | + |
| 20 | +def import_source(module_file_path, module_name): |
| 21 | + if not os.path.exists(module_file_path): |
| 22 | + raise FileNotFoundError(module_file_path) |
| 23 | + module_spec = importlib.util.spec_from_file_location(module_name, module_file_path) |
| 24 | + if module_spec is None: |
| 25 | + raise FileNotFoundError( |
| 26 | + "Unable to find '{}' in '{}'.".format(module_name, module_file_path) |
| 27 | + ) |
| 28 | + module = importlib.util.module_from_spec(module_spec) |
| 29 | + return module_spec.loader.exec_module(module) |
| 30 | + |
| 31 | + |
| 32 | +class TestDocumentationRecipes(ExtTestCase): |
| 33 | + def run_test(self, fold: str, name: str, verbose=0) -> int: |
| 34 | + ppath = os.environ.get("PYTHONPATH", "") |
| 35 | + if not ppath: |
| 36 | + os.environ["PYTHONPATH"] = ROOT |
| 37 | + elif ROOT not in ppath: |
| 38 | + sep = ";" if is_windows() else ":" |
| 39 | + os.environ["PYTHONPATH"] = ppath + sep + ROOT |
| 40 | + perf = time.perf_counter() |
| 41 | + try: |
| 42 | + mod = import_source(fold, os.path.splitext(name)[0]) |
| 43 | + assert mod is not None |
| 44 | + except FileNotFoundError: |
| 45 | + # try another way |
| 46 | + cmds = [sys.executable, "-u", os.path.join(fold, name)] |
| 47 | + p = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 48 | + res = p.communicate() |
| 49 | + out, err = res |
| 50 | + st = err.decode("ascii", errors="ignore") |
| 51 | + if st and "Traceback" in st: |
| 52 | + if '"dot" not found in path.' in st: |
| 53 | + # dot not installed, this part |
| 54 | + # is tested in onnx framework |
| 55 | + if verbose: |
| 56 | + print(f"failed: {name!r} due to missing dot.") |
| 57 | + return 0 |
| 58 | + raise AssertionError( # noqa: B904 |
| 59 | + "Example '{}' (cmd: {} - exec_prefix='{}') " |
| 60 | + "failed due to\n{}" |
| 61 | + "".format(name, cmds, sys.exec_prefix, st) |
| 62 | + ) |
| 63 | + dt = time.perf_counter() - perf |
| 64 | + if verbose: |
| 65 | + print(f"{dt:.3f}: run {name!r}") |
| 66 | + return 1 |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def add_test_methods(cls): |
| 70 | + this = os.path.abspath(os.path.dirname(__file__)) |
| 71 | + fold = os.path.normpath(os.path.join(this, "..", "..", "_doc", "recipes")) |
| 72 | + found = os.listdir(fold) |
| 73 | + for name in found: |
| 74 | + if not name.endswith(".py") or not name.startswith("plot_"): |
| 75 | + continue |
| 76 | + reason = None |
| 77 | + |
| 78 | + if not reason and not has_torch("4.7"): |
| 79 | + reason = "torch<2.7" |
| 80 | + |
| 81 | + if reason: |
| 82 | + |
| 83 | + @unittest.skip(reason) |
| 84 | + def _test_(self, name=name): |
| 85 | + res = self.run_test(fold, name, verbose=VERBOSE) |
| 86 | + self.assertTrue(res) |
| 87 | + |
| 88 | + else: |
| 89 | + |
| 90 | + @ignore_errors(OSError) # connectivity issues |
| 91 | + def _test_(self, name=name): |
| 92 | + res = self.run_test(fold, name, verbose=VERBOSE) |
| 93 | + self.assertTrue(res) |
| 94 | + |
| 95 | + short_name = os.path.split(os.path.splitext(name)[0])[-1] |
| 96 | + setattr(cls, f"test_{short_name}", _test_) |
| 97 | + |
| 98 | + |
| 99 | +TestDocumentationRecipes.add_test_methods() |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + unittest.main(verbosity=2) |
0 commit comments