|
| 1 | +"""Doc <-> allowlist parity guard for the filesystem tools (issue #1965). |
| 2 | +
|
| 3 | +Every directory in the component's ``ALLOWED_READ_DIRS`` / ``ALLOWED_WRITE_DIRS`` |
| 4 | +must be documented in both the server tool docstrings |
| 5 | +(``ha_read_file`` / ``ha_list_files`` / ``ha_write_file`` / ``ha_delete_file`` in |
| 6 | +``src/ha_mcp/tools/tools_filesystem.py``) and the component's ``services.yaml`` |
| 7 | +``path`` field descriptions. This catches the drift where a directory is added to |
| 8 | +the allowlist but a doc surface is forgotten -- exactly what happened for |
| 9 | +``dashboards/`` and is being corrected here for ``blueprints/``. |
| 10 | +
|
| 11 | +Self-contained on purpose: it parses the sources with ``ast`` / ``yaml`` and |
| 12 | +imports nothing from Home Assistant or fastmcp, so it runs in CI *and* locally |
| 13 | +without either installed. |
| 14 | +""" |
| 15 | + |
| 16 | +import ast |
| 17 | +import importlib.util |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +import pytest |
| 21 | +import yaml |
| 22 | + |
| 23 | +_REPO_ROOT = Path(__file__).resolve().parents[3] |
| 24 | +_COMPONENT_DIR = _REPO_ROOT / "custom_components" / "ha_mcp_tools" |
| 25 | +_TOOLS_FILE = _REPO_ROOT / "src" / "ha_mcp" / "tools" / "tools_filesystem.py" |
| 26 | +_SERVICES_FILE = _COMPONENT_DIR / "services.yaml" |
| 27 | + |
| 28 | + |
| 29 | +def _load_const_standalone(): |
| 30 | + """Load ``const.py`` directly, bypassing the HA-dependent package __init__.""" |
| 31 | + spec = importlib.util.spec_from_file_location( |
| 32 | + "_ha_mcp_tools_const_parity", _COMPONENT_DIR / "const.py" |
| 33 | + ) |
| 34 | + module = importlib.util.module_from_spec(spec) |
| 35 | + spec.loader.exec_module(module) |
| 36 | + return module |
| 37 | + |
| 38 | + |
| 39 | +_CONST = _load_const_standalone() |
| 40 | +ALLOWED_READ_DIRS = _CONST.ALLOWED_READ_DIRS |
| 41 | +ALLOWED_WRITE_DIRS = _CONST.ALLOWED_WRITE_DIRS |
| 42 | + |
| 43 | + |
| 44 | +def _tool_docstrings(): |
| 45 | + """Map every top-level/def name in tools_filesystem.py to its docstring.""" |
| 46 | + tree = ast.parse(_TOOLS_FILE.read_text(encoding="utf-8")) |
| 47 | + docs = {} |
| 48 | + for node in ast.walk(tree): |
| 49 | + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
| 50 | + doc = ast.get_docstring(node) |
| 51 | + if doc is not None: |
| 52 | + docs[node.name] = doc |
| 53 | + return docs |
| 54 | + |
| 55 | + |
| 56 | +def _service_path_descriptions(): |
| 57 | + """Map each service name to its ``path`` field description in services.yaml.""" |
| 58 | + data = yaml.safe_load(_SERVICES_FILE.read_text(encoding="utf-8")) |
| 59 | + return { |
| 60 | + name: (svc.get("fields", {}).get("path", {}).get("description", "") or "") |
| 61 | + for name, svc in data.items() |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +_DOCS = _tool_docstrings() |
| 66 | +_SERVICE_PATH_DESCS = _service_path_descriptions() |
| 67 | + |
| 68 | +_READ_TOOLS = ("ha_read_file", "ha_list_files") |
| 69 | +_WRITE_TOOLS = ("ha_write_file", "ha_delete_file") |
| 70 | +_READ_SERVICES = ("read_file", "list_files") |
| 71 | +_WRITE_SERVICES = ("write_file", "delete_file") |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize("tool", _READ_TOOLS) |
| 75 | +@pytest.mark.parametrize("directory", ALLOWED_READ_DIRS) |
| 76 | +def test_read_dirs_documented_in_tool_docstrings(directory, tool): |
| 77 | + assert f"{directory}/" in _DOCS[tool], ( |
| 78 | + f"{directory!r} is in ALLOWED_READ_DIRS but not documented in the " |
| 79 | + f"{tool} docstring (tools_filesystem.py)" |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize("tool", _WRITE_TOOLS) |
| 84 | +@pytest.mark.parametrize("directory", ALLOWED_WRITE_DIRS) |
| 85 | +def test_write_dirs_documented_in_tool_docstrings(directory, tool): |
| 86 | + assert f"{directory}/" in _DOCS[tool], ( |
| 87 | + f"{directory!r} is in ALLOWED_WRITE_DIRS but not documented in the " |
| 88 | + f"{tool} docstring (tools_filesystem.py)" |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize("service", _READ_SERVICES) |
| 93 | +@pytest.mark.parametrize("directory", ALLOWED_READ_DIRS) |
| 94 | +def test_read_dirs_documented_in_services_yaml(directory, service): |
| 95 | + assert f"{directory}/" in _SERVICE_PATH_DESCS[service], ( |
| 96 | + f"{directory!r} is in ALLOWED_READ_DIRS but not documented in the " |
| 97 | + f"services.yaml {service} path description" |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize("service", _WRITE_SERVICES) |
| 102 | +@pytest.mark.parametrize("directory", ALLOWED_WRITE_DIRS) |
| 103 | +def test_write_dirs_documented_in_services_yaml(directory, service): |
| 104 | + assert f"{directory}/" in _SERVICE_PATH_DESCS[service], ( |
| 105 | + f"{directory!r} is in ALLOWED_WRITE_DIRS but not documented in the " |
| 106 | + f"services.yaml {service} path description" |
| 107 | + ) |
0 commit comments