|
| 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