|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from tests.guardrail_ast_utils import collect_name_call_lines, read_module_ast |
| 6 | + |
| 7 | +pytestmark = pytest.mark.architecture |
| 8 | + |
| 9 | +HYPERBROWSER_ROOT = Path(__file__).resolve().parents[1] / "hyperbrowser" |
| 10 | +ALLOWED_PARSE_MAPPING_LIST_CALL_FILES = { |
| 11 | + Path("client/managers/extension_utils.py"), |
| 12 | + Path("client/managers/list_parsing_utils.py"), |
| 13 | + Path("client/managers/session_utils.py"), |
| 14 | +} |
| 15 | +ALLOWED_READ_PLAIN_LIST_CALL_FILES = { |
| 16 | + Path("client/managers/extension_utils.py"), |
| 17 | + Path("client/managers/list_parsing_utils.py"), |
| 18 | + Path("client/managers/session_utils.py"), |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +def _python_files() -> list[Path]: |
| 23 | + return sorted(HYPERBROWSER_ROOT.rglob("*.py")) |
| 24 | + |
| 25 | + |
| 26 | +def test_parse_mapping_list_items_usage_is_centralized(): |
| 27 | + violations: list[str] = [] |
| 28 | + |
| 29 | + for path in _python_files(): |
| 30 | + relative_path = path.relative_to(HYPERBROWSER_ROOT) |
| 31 | + module = read_module_ast(path) |
| 32 | + helper_calls = collect_name_call_lines(module, "parse_mapping_list_items") |
| 33 | + if not helper_calls: |
| 34 | + continue |
| 35 | + if relative_path in ALLOWED_PARSE_MAPPING_LIST_CALL_FILES: |
| 36 | + continue |
| 37 | + for line in helper_calls: |
| 38 | + violations.append(f"{relative_path}:{line}") |
| 39 | + |
| 40 | + assert violations == [] |
| 41 | + |
| 42 | + |
| 43 | +def test_read_plain_list_items_usage_is_centralized(): |
| 44 | + violations: list[str] = [] |
| 45 | + |
| 46 | + for path in _python_files(): |
| 47 | + relative_path = path.relative_to(HYPERBROWSER_ROOT) |
| 48 | + module = read_module_ast(path) |
| 49 | + helper_calls = collect_name_call_lines(module, "read_plain_list_items") |
| 50 | + if not helper_calls: |
| 51 | + continue |
| 52 | + if relative_path in ALLOWED_READ_PLAIN_LIST_CALL_FILES: |
| 53 | + continue |
| 54 | + for line in helper_calls: |
| 55 | + violations.append(f"{relative_path}:{line}") |
| 56 | + |
| 57 | + assert violations == [] |
0 commit comments