PV-Bench / experiments /t1 /tests /test_workspace_scripts.py
cielseven's picture
add the experiment harnesses for t1 to t4 and t3-flat
28adce7
Raw History Blame Contribute Delete
3.86 kB
"""The attempt workspace: what is mounted, and check.sh written for the image."""
from __future__ import annotations
import os
import subprocess
import tempfile
import unittest
from pathlib import Path
from unittest import mock
from helpers import FAKE_FLAGS, PROBLEM, assert_no_host_paths, make_cfg
from t1 import validity, workspace
from t1.config import BENCH, Case, Config
BACKEND = os.environ.get("QCP_BACKEND")
class WorkspaceTests(unittest.TestCase):
def setUp(self) -> None:
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.root = Path(tmp.name)
flags = mock.patch.object(Config, "container_coqflags", return_value=FAKE_FLAGS)
flags.start()
self.addCleanup(flags.stop)
self.cfg = make_cfg(self.root)
def build(self, runner: str = "codex") -> Path:
ws = self.root / "ws"
workspace.build(self.cfg, Case(PROBLEM, "m", runner=runner), ws)
return ws
def test_layout_and_mounts(self) -> None:
ws = self.build()
mounts = {m.name for m in self.cfg.task.mounts}
self.assertEqual(mounts, {"problem", "examples", "lib", "answer"})
self.assertEqual(sorted(p.name for p in ws.iterdir() if p.name != ".git"),
sorted([*mounts, "check.sh", "AGENTS.md"]))
self.assertEqual(sorted(p.name for p in (ws / "problem").iterdir()), ["problem.md", "solution.c"])
self.assertEqual(list((ws / "answer").iterdir()), [])
self.assertEqual(os.readlink(ws / "lib/QCP"), "/opt/qcp/Rocq")
self.assertTrue((ws / "examples/bubble_sort/rocq/spec_lib.v").is_file())
self.assertFalse(any("groundtruth" in p for p in workspace.listing(ws)))
self.assertFalse(any(PROBLEM.rsplit("/", 1)[1] in p for p in workspace.listing(ws) if p.startswith("examples/")))
self.assertEqual(set(validity.FIXED_FILES) & {p.name for p in ws.iterdir()}, {"check.sh", "AGENTS.md"})
self.assertTrue((self.build("claude") / "CLAUDE.md").is_file())
def test_check_script_uses_container_paths(self) -> None:
text = (self.build() / "check.sh").read_text()
assert_no_host_paths(self, self.cfg, text, "check.sh")
self.assertIn('cd "$(dirname "$0")/answer" && coqc -R /opt/qcp/Rocq/sets SetsClass', text)
self.assertNotIn("PVbench", text)
syntax = subprocess.run(["bash", "-n"], input=text, capture_output=True, text=True)
self.assertEqual(syntax.returncode, 0, syntax.stderr)
class RealMappingTests(unittest.TestCase):
@unittest.skipUnless(BACKEND and Path(BACKEND, "Rocq").is_dir(), "set QCP_BACKEND to a backend checkout")
@unittest.skipUnless((BENCH / "CONFIGURE").is_file(), "create benchmarks/CONFIGURE (QCP_ROCQ) from CONFIGURE.example")
def test_real_mappings_point_into_the_image(self) -> None:
cfg = Config.load()
flags = cfg.container_coqflags()
paths = [flags[i + 1] for i, token in enumerate(flags[:-1]) if token in ("-R", "-Q")]
self.assertGreater(len(paths), 5)
for path in paths:
self.assertTrue(path.startswith("/opt/qcp/Rocq/"), path)
self.assertTrue((cfg.backend / "Rocq" / path.removeprefix("/opt/qcp/Rocq/")).is_dir(), path)
self.assertNotIn("PVbench", flags)
args = cfg.container_symexec_args()
self.assertEqual(len(args), len(cfg.symexec_shared_args))
for arg in args:
self.assertNotIn(str(cfg.backend), arg)
for path in [a[2:] for a in args if a.startswith("-I")] + [args[i + 1] for i, a in enumerate(args) if a == "-slp"]:
self.assertTrue(path.startswith("/opt/qcp/QCP_examples/"), path)
self.assertTrue((cfg.backend / "QCP_examples" / path.removeprefix("/opt/qcp/QCP_examples/")).is_dir(), path)
if __name__ == "__main__":
unittest.main()