File size: 3,857 Bytes
28adce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()