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, make_cfg from t3 import workspace from t3.config import BENCH, HOME, ROOT, T3_DIR, Case, Config PROBLEM = "Algorithms/climbing_stairs" BACKEND = os.environ.get("QCP_BACKEND") class CheckerScriptTests(unittest.TestCase): def generate(self, cfg: Config, root: Path) -> dict[str, str]: case = Case(PROBLEM, "m") ws = root / "ws" ws.mkdir() workspace._write_checker(cfg, case, ws) return {name: (ws / name).read_text() for name in ("check.sh", "rocq.sh")} def assert_container_only(self, cfg: Config, scripts: dict[str, str]) -> None: host_paths = {str(cfg.backend), str(BENCH), str(ROOT), str(T3_DIR), str(HOME)} for name, text in scripts.items(): for host in host_paths: self.assertNotIn(host, text, f"{name} contains host path {host}") self.assertNotIn("/Users/", text, name) self.assertIn("/opt/qcp/Rocq", text, name) self.assertIn("coq_makefile -f _CoqProject", text, name) self.assertNotIn("/coq_makefile", text, name) syntax = subprocess.run(["bash", "-n"], input=text, capture_output=True, text=True) self.assertEqual(syntax.returncode, 0, f"{name}: {syntax.stderr}") self.assertIn('ROOT=$(cd -- "$(dirname -- "$0")" && pwd)', scripts["check.sh"]) self.assertIn("exec coqtop -quiet", scripts["rocq.sh"]) def test_scripts_use_container_paths(self) -> None: with tempfile.TemporaryDirectory() as tmp: cfg = make_cfg(Path(tmp)) with mock.patch.object(Config, "container_coqflags", return_value=FAKE_FLAGS): scripts = self.generate(cfg, Path(tmp)) self.assert_container_only(cfg, scripts) @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) # the same subtree exists in the host backend the image was built from self.assertTrue((cfg.backend / "Rocq" / path.removeprefix("/opt/qcp/Rocq/")).is_dir(), path) self.assertNotIn("PVbench", flags) with tempfile.TemporaryDirectory() as tmp: self.assert_container_only(cfg, self.generate(cfg, Path(tmp))) class ImportScanTests(unittest.TestCase): def test_require_export_is_followed(self) -> None: text = ( "From PVbench.Algorithms.x.rocq Require Export spec_lib helper_lib.\n" "Require Import PVbench.Algorithms.y.rocq.groundtruth.proof_lib.\n" "(* From PVbench.Hidden Require Import gone. *)\n" ) self.assertEqual(workspace._pvbench_imports(text), { "PVbench.Algorithms.x.rocq.spec_lib", "PVbench.Algorithms.x.rocq.helper_lib", "PVbench.Algorithms.y.rocq.groundtruth.proof_lib", }) if __name__ == "__main__": unittest.main()