fix: harden codebase test generator output (#667)
This commit is contained in:
55
tests/test_codebase_test_generator.py
Normal file
55
tests/test_codebase_test_generator.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
SCRIPT = ROOT / "scripts" / "codebase_test_generator.py"
|
||||
|
||||
|
||||
def load_module():
|
||||
spec = importlib.util.spec_from_file_location("codebase_test_generator", str(SCRIPT))
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
return mod
|
||||
|
||||
|
||||
def test_generate_test_suite_uses_dynamic_loader_for_numbered_paths():
|
||||
mod = load_module()
|
||||
func = mod.FunctionInfo(
|
||||
name="linkify",
|
||||
module_path="reports/notebooklm/2026-03-27-hermes-openclaw/render_reports.py",
|
||||
lineno=12,
|
||||
args=["text"],
|
||||
has_return=True,
|
||||
)
|
||||
gap = mod.CoverageGap(func=func, reason="no test found", test_priority=1)
|
||||
|
||||
suite = mod.generate_test_suite([gap], max_tests=1)
|
||||
|
||||
assert "import importlib.util" in suite
|
||||
assert "_load_symbol(" in suite
|
||||
assert "from reports.notebooklm" not in suite
|
||||
assert "2026-03-27-hermes-openclaw/render_reports.py" in suite
|
||||
|
||||
|
||||
def test_generate_test_handles_async_and_runtime_args_safely():
|
||||
mod = load_module()
|
||||
func = mod.FunctionInfo(
|
||||
name="keypress",
|
||||
module_path="angband/mcp_server.py",
|
||||
lineno=200,
|
||||
args=["key", "wait_ms", "session_name"],
|
||||
is_async=True,
|
||||
has_return=True,
|
||||
calls=["send_key"],
|
||||
)
|
||||
gap = mod.CoverageGap(func=func, reason="no test found", test_priority=1)
|
||||
|
||||
test_code = mod.generate_test(gap)
|
||||
|
||||
assert "@pytest.mark.asyncio" in test_code
|
||||
assert "async def" in test_code
|
||||
assert "await target(" in test_code
|
||||
assert "key='test'" in test_code
|
||||
assert "wait_ms=1" in test_code
|
||||
assert "session_name='test'" in test_code
|
||||
assert "pytest.raises((RuntimeError, ValueError, TypeError))" in test_code
|
||||
Reference in New Issue
Block a user