SACB / source /tools /harness-tests /test_runner.py
ilintar's picture
Add corpus source: base repos, task overlays, hidden tests, and the authoring tools
9368cc4 verified
Raw
History Blame Contribute Delete
3.88 kB
#!/usr/bin/env python3
"""Test the Python language adapter: linter output and hidden-test scoring."""
import sys, tempfile, shutil
from pathlib import Path
EVAL = Path(__file__).resolve().parents[2] / "examples" / "llama-eval"
sys.path.insert(0, str(EVAL))
from eval_sandbox import Sandbox # noqa: E402
from agentic_eval import LANGS, Runner # noqa: E402
fails = []
def check(name, cond, detail=""):
print(f" {'PASS' if cond else 'FAIL'} {name}" + (f" [{detail}]" if not cond else ""))
if not cond:
fails.append(name)
lang = LANGS["python"]
sbx = Sandbox(name="agentic-python", packages=list(lang.packages))
sbx.ensure()
runner = Runner(sbx, lang)
print(f"isolation: {sbx.isolation}")
work = Path(tempfile.mkdtemp(prefix="runner-"))
try:
(work / "pkg").mkdir()
(work / "pkg" / "__init__.py").write_text("")
# a logic bug: discount is applied additively instead of multiplicatively
(work / "pkg" / "pricing.py").write_text(
"def apply_discount(price: float, pct: float) -> float:\n"
" return price - pct\n"
)
(work / "tests").mkdir()
(work / "tests" / "__init__.py").write_text("")
(work / "tests" / "test_pricing.py").write_text(
"import unittest\n"
"from pkg.pricing import apply_discount\n"
"class T(unittest.TestCase):\n"
" def test_ten_percent(self):\n"
" self.assertAlmostEqual(apply_discount(200.0, 10.0), 180.0)\n"
" def test_zero(self):\n"
" self.assertAlmostEqual(apply_discount(50.0, 0.0), 50.0)\n"
)
# -- lint on clean-but-wrong code -----------------------------------
out = runner.lint(work, "pkg")
check("lint: clean code reports no problems", out.strip() == "No problems found.", out[:200])
# -- tests detect the logic bug -------------------------------------
res = runner.test(work)
outcomes = res.get("tests", {})
check("test: discovered both tests", len(outcomes) == 2, str(res)[:250])
check("test: logic bug fails one test",
sum(v == "fail" for v in outcomes.values()) == 1, str(outcomes))
check("test: unaffected test still passes",
sum(v == "pass" for v in outcomes.values()) == 1, str(outcomes))
# -- fix it, tests must all pass ------------------------------------
(work / "pkg" / "pricing.py").write_text(
"def apply_discount(price: float, pct: float) -> float:\n"
" return price * (1.0 - pct / 100.0)\n"
)
res = runner.test(work)
check("test: fix makes all pass",
all(v == "pass" for v in res.get("tests", {}).values()) and len(res.get("tests", {})) == 2,
str(res)[:250])
# -- lint must catch a type error -----------------------------------
(work / "pkg" / "pricing.py").write_text(
"def apply_discount(price: float, pct: float) -> float:\n"
" return 'free'\n"
)
out = runner.lint(work, "pkg")
check("lint: mypy catches return-type error",
"str" in out and ("return-value" in out or "Incompatible" in out), out[:250])
# -- lint must catch a syntax error ---------------------------------
(work / "pkg" / "pricing.py").write_text("def broken(:\n pass\n")
out = runner.lint(work, "pkg")
check("lint: ruff catches syntax error",
"syntax" in out.lower() or "SyntaxError" in out, out[:250])
# -- a broken import must not vanish from the denominator -----------
res = runner.test(work)
outcomes = res.get("tests", {})
check("test: import failure counts as failure, not as zero tests",
len(outcomes) >= 1 and all(v in ("fail", "error") for v in outcomes.values()),
str(res)[:250])
finally:
shutil.rmtree(work, ignore_errors=True)
print(f"\n{'ALL PASS' if not fails else 'FAILURES: ' + ', '.join(fails)}")
sys.exit(1 if fails else 0)