File size: 5,263 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
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Execute the generated check.sh with fake POSIX tools (no Python inside the scripts)."""
import json
import subprocess
from unittest.mock import patch

from t4helpers import GOAL, Fixture, render_local_scripts

SH_SYMEXEC = ('#!/bin/sh\nprintf "%b" "$GOAL" > goal.v\n'
              'printf "Lemma proof_of_toy : toy_goal. Proof. Admitted.\\n" > manual.v\n'
              'printf "(* auto *)\\n" > proof_auto.v\nprintf "(* check *)\\n" > goal_check.v\n')


class CheckScriptTests(Fixture):
    def setUp(self):
        super().setUp()
        self.coqc.write_text('#!/bin/sh\ncase "$*" in *assumptions.v) echo "Axioms:"; echo "CNotation.sizeof_struct_type : nat" ;; esac\nexit 0\n')
        self.qcp.write_text(SH_SYMEXEC.replace('$GOAL', GOAL.replace('\n', '\\n')))
        with patch('sys.executable', '/unavailable/venv/python'):
            self.prepared()
        self.ws = self.case.workspace
        render_local_scripts(self.cfg, self.ws)
        self.proof = self.ws / 'answer/proof'

    def invoke(self):
        result = subprocess.run([str(self.ws / 'check.sh')], cwd=self.root, capture_output=True, text=True, timeout=30)
        latest = self.ws / 'answer/feedback/latest-proof.json'
        state = json.loads(latest.read_text()) if latest.exists() else None
        return result, state

    def test_complete_proof_is_checked_in_order(self):
        (self.proof / 'manual.v').write_text('Lemma proof_of_toy : toy_goal. Proof. exact I. Qed.\n')
        (self.proof / 'proof_lib.v').write_text('Lemma aux : True. Proof. exact I. Qed.\n')
        result, state = self.invoke()
        self.assertEqual(result.returncode, 0, result.stderr)
        self.assertEqual(state['status'], 'checked')
        self.assertEqual(state['steps'], ['proof_auto', 'proof_lib', 'manual', 'goal_check', 'assumptions'])
        inv = self.ws / 'answer/feedback' / state['invocation']
        self.assertEqual((inv / 'manual.v').read_text(), 'Lemma proof_of_toy : toy_goal. Proof. exact I. Qed.\n')
        self.assertIn('Admitted', (inv / 'manual.skel.v').read_text())
        self.assertTrue((inv / 'support/proof_lib.v').is_file())
        self.assertIn('Print Assumptions T2Eval.goal_check.VC_Correctness.proof_of_toy.', (inv / 'proof_assumptions.v').read_text())
        self.assertEqual(json.loads((inv / 'proof.json').read_text()), state)
        self.assertIn('CNotation.sizeof_struct_type', result.stdout)
        self.assertIn('proof OK', result.stdout)
        flags = (inv / 'coqflags.txt').read_text().splitlines()
        self.assertIn('T2Eval', flags)
        self.assertFalse((self.ws / 'answer/feedback/.proof-lock').exists())
        self.assertFalse((self.ws / 'answer/feedback/.lock').exists())

    def test_missing_manual_bypass_and_failing_step(self):
        result, state = self.invoke()
        self.assertEqual(result.returncode, 1)
        self.assertEqual(state['status'], 'failed')
        self.assertIn('missing answer/proof/manual.v', state['error'])
        (self.proof / 'manual.v').write_text('Lemma proof_of_toy : toy_goal. Proof. Admitted.\n')
        result, state = self.invoke()
        self.assertEqual(result.returncode, 1)
        self.assertIn('unproved assumption', state['error'])
        self.assertEqual(state['steps'], [])
        (self.proof / 'manual.v').write_text('Lemma proof_of_toy : toy_goal. Proof. exact I. Qed.\n')
        self.coqc.write_text('#!/bin/sh\ncase "$*" in *manual.v) echo "Error: tactic failure" >&2; exit 1 ;; esac\nexit 0\n')
        result, state = self.invoke()
        self.assertEqual(result.returncode, 1)
        self.assertEqual(state['failed_step'], 'manual')
        self.assertEqual(state['steps'], ['proof_auto', 'proof_lib'])
        self.assertIn('tactic failure', result.stderr)
        self.assertIn('proof-manual.stderr.log', result.stderr)

    def test_lock_arguments_and_feedback_failure(self):
        (self.proof / 'manual.v').write_text('Lemma proof_of_toy : toy_goal. Proof. exact I. Qed.\n')
        result = subprocess.run([str(self.ws / 'check.sh'), '-I/forbidden'], capture_output=True, text=True, timeout=30)
        self.assertEqual(result.returncode, 2)
        (self.ws / 'answer/feedback/.proof-lock').mkdir()
        result, state = self.invoke()
        self.assertEqual(result.returncode, 2)
        self.assertIsNone(state)
        (self.ws / 'answer/feedback/.proof-lock').rmdir()
        self.qcp.write_text('#!/bin/sh\nexit 7\n')
        result, state = self.invoke()
        self.assertEqual(result.returncode, 1)
        self.assertIn('annotation feedback failed', result.stderr)
        self.assertIsNone(state)

    def test_timeout_stops_the_step(self):
        (self.proof / 'manual.v').write_text('Lemma proof_of_toy : toy_goal. Proof. exact I. Qed.\n')
        check = self.ws / 'check.sh'
        check.write_text(check.read_text().replace('proof_timeout=4\n', 'proof_timeout=1\n'))
        self.coqc.write_text('#!/bin/sh\ncase "$*" in *manual.v) sleep 5 ;; esac\nexit 0\n')
        result, state = self.invoke()
        self.assertEqual(result.returncode, 1)
        self.assertEqual(state['failed_step'], 'manual')
        self.assertIn('timeout', state['error'])
        self.assertFalse((self.ws / 'answer/feedback/.proof-lock').exists())