This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest_basic.py
82 lines (68 loc) · 1.85 KB
/
test_basic.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
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
"""
Basic sanity-check unit tests
"""
# stdlib
import importlib.util
import os
from pathlib import Path
import sys
import time
from typing import List
# third-party
import pytest
sep = os.path.sep
nthreads = 4
class BadSyntax(Exception):
pass
# Tests
@pytest.mark.unit
def test_import_syntax():
srcdir = Path(__file__).parent.parent / "src"
items = find_python_modules(srcdir)
errs = []
for item in items:
try:
import_item(item)
except BadSyntax as err:
errs.append(str(err))
if errs:
errmsg = "\n".join(errs)
assert False, f"Syntax errors encountered: {errmsg}"
def import_item(item):
try:
import_python_file(*item)
# report syntax errors
except SyntaxError as err:
raise BadSyntax(f"[{type(err)}] {item[0]}: {err}")
# ignore everything else
except Exception:
pass
# Helper functions
def find_python_modules(target_dir: Path) -> List:
"""Find all python modules from target_dir, on down, that contain a
Python module or sub-package.
"""
tgtdir_len = len(str(target_dir))
modules = []
for path in target_dir.rglob("*.py"):
spath = str(path)
if spath.endswith("__init__.py"):
continue
if ".ipynb_checkpoints" in spath:
continue
name = module_name_from_path(path, tgtdir_len)
modules.append((path, name))
return modules
def module_name_from_path(p, n):
# strip leading target dir and separator
s = str(p)[n + 1:]
# strip ext
s = s[:-3]
# change separator to .
s = s.replace(sep, ".")
return s
def import_python_file(file_path, module_name):
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)