|
| 1 | +import ast |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | + |
| 5 | +_TARGET_FILES = ( |
| 6 | + Path("hyperbrowser/client/managers/response_utils.py"), |
| 7 | + Path("hyperbrowser/transport/base.py"), |
| 8 | + Path("hyperbrowser/client/managers/list_parsing_utils.py"), |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def _collect_list_keys_calls(module: ast.AST) -> list[int]: |
| 13 | + key_calls: list[int] = [] |
| 14 | + for node in ast.walk(module): |
| 15 | + if not isinstance(node, ast.Call): |
| 16 | + continue |
| 17 | + if not isinstance(node.func, ast.Name) or node.func.id != "list": |
| 18 | + continue |
| 19 | + if len(node.args) != 1: |
| 20 | + continue |
| 21 | + argument = node.args[0] |
| 22 | + if not isinstance(argument, ast.Call): |
| 23 | + continue |
| 24 | + if not isinstance(argument.func, ast.Attribute): |
| 25 | + continue |
| 26 | + if argument.func.attr != "keys": |
| 27 | + continue |
| 28 | + key_calls.append(node.lineno) |
| 29 | + return key_calls |
| 30 | + |
| 31 | + |
| 32 | +def _collect_mapping_reader_calls(module: ast.AST) -> list[int]: |
| 33 | + reader_calls: list[int] = [] |
| 34 | + for node in ast.walk(module): |
| 35 | + if not isinstance(node, ast.Call): |
| 36 | + continue |
| 37 | + if isinstance(node.func, ast.Name) and node.func.id == "read_string_key_mapping": |
| 38 | + reader_calls.append(node.lineno) |
| 39 | + return reader_calls |
| 40 | + |
| 41 | + |
| 42 | +def test_core_mapping_parsers_use_shared_mapping_reader(): |
| 43 | + violations: list[str] = [] |
| 44 | + missing_reader_calls: list[str] = [] |
| 45 | + |
| 46 | + for relative_path in _TARGET_FILES: |
| 47 | + source = relative_path.read_text(encoding="utf-8") |
| 48 | + module = ast.parse(source, filename=str(relative_path)) |
| 49 | + list_keys_calls = _collect_list_keys_calls(module) |
| 50 | + if list_keys_calls: |
| 51 | + for line in list_keys_calls: |
| 52 | + violations.append(f"{relative_path}:{line}") |
| 53 | + reader_calls = _collect_mapping_reader_calls(module) |
| 54 | + if not reader_calls: |
| 55 | + missing_reader_calls.append(str(relative_path)) |
| 56 | + |
| 57 | + assert violations == [] |
| 58 | + assert missing_reader_calls == [] |
0 commit comments