Skip to content

Commit 4a3c3f0

Browse files
test(cli): resolve every template import against the installed package
Parsing the rendered templates lets one keep naming a symbol the SDK removed, so every scaffolded agent fails on its first start instead of in CI. Each `from agentex... import X` now has to resolve. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 62b97be commit 4a3c3f0

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

‎tests/lib/cli/test_init_templates.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import annotations
1515

1616
import ast
17+
import importlib
1718
from pathlib import Path
1819

1920
import pytest
@@ -47,6 +48,32 @@ def _render_project(tmp_path: Path, template_type: TemplateType, use_uv: bool =
4748
return tmp_path / context["project_name"]
4849

4950

51+
def _agentex_imports(source: str, filename: str) -> list[tuple[str, str]]:
52+
"""(module, name) for every `from agentex... import name` in the source."""
53+
found: list[tuple[str, str]] = []
54+
for node in ast.walk(ast.parse(source, filename=filename)):
55+
if isinstance(node, ast.ImportFrom) and node.module and node.module.startswith("agentex"):
56+
found.extend((node.module, alias.name) for alias in node.names)
57+
return found
58+
59+
60+
@pytest.mark.parametrize("template_type", list(TemplateType))
61+
def test_all_templates_import_existing_agentex_symbols(tmp_path: Path, template_type: TemplateType):
62+
"""Every `from agentex... import X` a template renders resolves at import time.
63+
64+
Parsing alone lets a template keep naming a symbol the SDK removed, so every scaffolded
65+
agent fails on its first start instead of here.
66+
"""
67+
project_dir = _render_project(tmp_path, template_type)
68+
69+
missing: list[str] = []
70+
for py_file in project_dir.rglob("*.py"):
71+
for module, name in _agentex_imports(py_file.read_text(), str(py_file)):
72+
if not hasattr(importlib.import_module(module), name):
73+
missing.append(f"{py_file.relative_to(project_dir)}: from {module} import {name}")
74+
assert not missing, "\n".join(missing)
75+
76+
5077
@pytest.mark.parametrize("template_type", list(TemplateType))
5178
def test_all_templates_render_to_valid_python(tmp_path: Path, template_type: TemplateType):
5279
"""Every template renders, and every rendered .py file is syntactically valid."""

0 commit comments

Comments
 (0)