Skip to content

Commit ebaad79

Browse files
Add guard test for tool mapping helper usage
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent c1f991f commit ebaad79

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import ast
2+
from pathlib import Path
3+
4+
5+
TOOLS_MODULE = Path("hyperbrowser/tools/__init__.py")
6+
7+
8+
def _collect_keys_calls(module: ast.AST) -> list[int]:
9+
keys_calls: list[int] = []
10+
for node in ast.walk(module):
11+
if not isinstance(node, ast.Call):
12+
continue
13+
if not isinstance(node.func, ast.Attribute):
14+
continue
15+
if node.func.attr == "keys":
16+
keys_calls.append(node.lineno)
17+
return keys_calls
18+
19+
20+
def _collect_helper_calls(module: ast.AST, helper_name: str) -> list[int]:
21+
helper_calls: list[int] = []
22+
for node in ast.walk(module):
23+
if not isinstance(node, ast.Call):
24+
continue
25+
if not isinstance(node.func, ast.Name):
26+
continue
27+
if node.func.id == helper_name:
28+
helper_calls.append(node.lineno)
29+
return helper_calls
30+
31+
32+
def test_tools_module_uses_shared_mapping_read_helpers():
33+
source = TOOLS_MODULE.read_text(encoding="utf-8")
34+
module = ast.parse(source, filename=str(TOOLS_MODULE))
35+
36+
keys_calls = _collect_keys_calls(module)
37+
read_key_calls = _collect_helper_calls(module, "read_string_mapping_keys")
38+
copy_value_calls = _collect_helper_calls(
39+
module, "copy_mapping_values_by_string_keys"
40+
)
41+
42+
assert keys_calls == []
43+
assert read_key_calls != []
44+
assert copy_value_calls != []

0 commit comments

Comments
 (0)