File size: 3,884 Bytes
9368cc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/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)