-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtest_module_dir.py
63 lines (54 loc) · 1.85 KB
/
test_module_dir.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
from __future__ import annotations
import importlib
import importlib.machinery
import importlib.util
import inspect
import pkgutil
from pathlib import Path
from typing import Generator
def on_all_modules(
name: str, base_path: Path | None = None, *, pkg: bool
) -> Generator[str, None, None]:
if base_path is None:
base_module = importlib.import_module(name)
base_path = Path(inspect.getfile(base_module)).parent
for module_info in pkgutil.iter_modules([str(base_path)]):
package_name = f"{name}.{module_info.name}"
if module_info.ispkg:
if pkg:
yield package_name
yield from on_all_modules(
package_name, base_path / module_info.name, pkg=pkg
)
else:
yield package_name
def test_all_modules_filter_all():
all_modules = on_all_modules("scikit_build_core", pkg=False)
all_modules = (
n
for n in all_modules
if not n.split(".")[-1].startswith("__") and "resources" not in n
)
for name in all_modules:
module = importlib.import_module(name)
try:
dir_module = set(dir(module))
except Exception:
print(f"dir() failed on {name}")
raise
items = ["annotations", "os", "sys"]
for item in items:
assert item not in dir_module, f"{module.__file__} has {item!r}"
def test_all_modules_has_all():
all_modules = on_all_modules("scikit_build_core", pkg=True)
all_modules = (
n
for n in all_modules
if not n.split(".")[-1].startswith("_") and "resources" not in n
)
for name in all_modules:
module = importlib.import_module(name)
dir_module = module.__dict__
items = ["__all__"]
for item in items:
assert item in dir_module, f"{module.__file__} missing {item!r}"