File size: 1,838 Bytes
ef20ebe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Lightweight test runner. pytest is not installed in this build sandbox
and there is no network access to install it (see README) -- this
script discovers tests/test_*.py, runs every top-level test_* function,
and reports real PASS/FAIL based on actual execution (spec section 6:
real execution validation, not static syntax checking).

If you have pytest available (e.g. after `pip install -r requirements.txt`
somewhere with network access), just run `pytest tests/` instead --
these test files use plain assert and need nothing pytest-specific.
"""
import importlib.util
import sys
import traceback
from pathlib import Path

ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "tests"))


def run() -> bool:
    test_files = sorted((ROOT / "tests").glob("test_*.py"))
    total = 0
    failed = 0
    for f in test_files:
        spec = importlib.util.spec_from_file_location(f.stem, f)
        module = importlib.util.module_from_spec(spec)
        try:
            spec.loader.exec_module(module)
        except Exception:
            print(f"IMPORT ERROR  {f.name}")
            traceback.print_exc()
            failed += 1
            total += 1
            continue
        for name in dir(module):
            if name.startswith("test_"):
                fn = getattr(module, name)
                if callable(fn):
                    total += 1
                    try:
                        fn()
                        print(f"PASS  {f.name}::{name}")
                    except Exception:
                        failed += 1
                        print(f"FAIL  {f.name}::{name}")
                        traceback.print_exc()
    print(f"\n{total - failed}/{total} passed")
    return failed == 0


if __name__ == "__main__":
    ok = run()
    sys.exit(0 if ok else 1)