Skip to content

Commit 7452b1d

Browse files
Add AST module-import helper usage guard
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent a5a02b0 commit 7452b1d

7 files changed

+59
-3
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ This runs lint, format checks, compile checks, tests, and package build.
9999
- `tests/test_ast_import_utils.py` (shared AST import-helper contract validation),
100100
- `tests/test_ast_import_utils_module_import_boundary.py` (shared AST import-helper module import boundary enforcement across test modules),
101101
- `tests/test_ast_module_import_helper_import_boundary.py` (shared AST module-import helper import boundary enforcement across test modules),
102+
- `tests/test_ast_module_import_helper_usage.py` (shared AST module-import helper usage enforcement across AST boundary guard suites),
102103
- `tests/test_ast_symbol_import_helper_import_boundary.py` (shared AST symbol-import helper import boundary enforcement across test modules),
103104
- `tests/test_ast_symbol_import_helper_usage.py` (shared AST symbol-import helper usage enforcement across AST boundary guard suites),
104105
- `tests/test_binary_file_open_helper_usage.py` (shared binary file open helper usage enforcement),

tests/test_architecture_marker_usage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"tests/test_ast_import_utils.py",
2929
"tests/test_ast_import_utils_module_import_boundary.py",
3030
"tests/test_ast_module_import_helper_import_boundary.py",
31+
"tests/test_ast_module_import_helper_usage.py",
3132
"tests/test_ast_symbol_import_helper_import_boundary.py",
3233
"tests/test_ast_symbol_import_helper_usage.py",
3334
"tests/test_guardrail_ast_utils.py",

tests/test_ast_call_symbol_helper_import_boundary.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"tests/test_ast_function_source_helper_usage.py",
1313
"tests/test_ast_import_helper_usage.py",
1414
"tests/test_ast_import_utils.py",
15+
"tests/test_ast_module_import_helper_usage.py",
1516
"tests/test_ast_symbol_import_helper_usage.py",
1617
)
1718

tests/test_ast_call_symbol_helper_usage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
AST_CALL_SYMBOL_GUARD_MODULES = (
1111
"tests/test_ast_function_source_helper_usage.py",
1212
"tests/test_ast_import_helper_usage.py",
13+
"tests/test_ast_module_import_helper_usage.py",
1314
"tests/test_ast_symbol_import_helper_usage.py",
1415
)
1516

tests/test_ast_import_utils_module_import_boundary.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"tests/test_ast_import_helper_import_boundary.py",
2121
"tests/test_ast_import_helper_secondary_import_boundary.py",
2222
"tests/test_ast_module_import_helper_import_boundary.py",
23+
"tests/test_ast_module_import_helper_usage.py",
2324
"tests/test_ast_symbol_import_helper_import_boundary.py",
2425
"tests/test_ast_symbol_import_helper_usage.py",
2526
"tests/test_ast_import_utils.py",

tests/test_ast_module_import_helper_import_boundary.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import pytest
44

55
from tests.ast_import_utils import imports_symbol_from_module
6+
from tests.test_ast_module_import_helper_usage import AST_MODULE_IMPORT_GUARD_MODULES
67

78
pytestmark = pytest.mark.architecture
89

910

10-
EXPECTED_IMPORTS_FROM_MODULE_IMPORTERS = (
11+
EXPECTED_EXTRA_IMPORTERS = (
1112
"tests/test_ast_import_utils.py",
12-
"tests/test_ast_import_utils_module_import_boundary.py",
13+
"tests/test_ast_module_import_helper_usage.py",
1314
)
1415

1516

@@ -25,4 +26,5 @@ def test_imports_from_module_imports_are_centralized():
2526
continue
2627
discovered_modules.append(module_path.as_posix())
2728

28-
assert discovered_modules == list(EXPECTED_IMPORTS_FROM_MODULE_IMPORTERS)
29+
expected_modules = sorted([*AST_MODULE_IMPORT_GUARD_MODULES, *EXPECTED_EXTRA_IMPORTERS])
30+
assert discovered_modules == expected_modules
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from tests.ast_import_utils import calls_symbol, imports_from_module
6+
7+
pytestmark = pytest.mark.architecture
8+
9+
10+
AST_MODULE_IMPORT_GUARD_MODULES = (
11+
"tests/test_ast_import_utils_module_import_boundary.py",
12+
)
13+
14+
15+
def test_ast_module_import_guard_modules_reuse_shared_helper():
16+
violating_modules: list[str] = []
17+
for module_path in AST_MODULE_IMPORT_GUARD_MODULES:
18+
module_text = Path(module_path).read_text(encoding="utf-8")
19+
if not imports_from_module(module_text, module="tests.ast_import_utils"):
20+
violating_modules.append(module_path)
21+
continue
22+
if not calls_symbol(module_text, "imports_from_module"):
23+
violating_modules.append(module_path)
24+
continue
25+
if "def _imports_from_module" in module_text:
26+
violating_modules.append(module_path)
27+
28+
assert violating_modules == []
29+
30+
31+
def test_ast_module_import_guard_inventory_stays_in_sync():
32+
excluded_modules = {
33+
"tests/test_ast_import_utils.py",
34+
"tests/test_ast_module_import_helper_import_boundary.py",
35+
"tests/test_ast_module_import_helper_usage.py",
36+
}
37+
discovered_modules: list[str] = []
38+
for module_path in sorted(Path("tests").glob("test_*.py")):
39+
normalized_path = module_path.as_posix()
40+
if normalized_path in excluded_modules:
41+
continue
42+
module_text = module_path.read_text(encoding="utf-8")
43+
if not imports_from_module(module_text, module="tests.ast_import_utils"):
44+
continue
45+
if not calls_symbol(module_text, "imports_from_module"):
46+
continue
47+
discovered_modules.append(normalized_path)
48+
49+
assert sorted(AST_MODULE_IMPORT_GUARD_MODULES) == discovered_modules

0 commit comments

Comments
 (0)