File size: 2,438 Bytes
4bc63c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The contract of tools/ask.py: exit codes, temp workspace, verdict from transcript."""

import json
import os
import shutil
import subprocess
import sys
import tempfile
import unittest

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ASK = os.path.join(ROOT, "tools", "ask.py")
LAB = os.path.join(ROOT, "fixtures", "lab")


class AskContract(unittest.TestCase):
    def setUp(self):
        self.temp = tempfile.mkdtemp(prefix="askcontract_")
        self.work = os.path.join(self.temp, "lab")
        shutil.copytree(LAB, self.work)

    def tearDown(self):
        shutil.rmtree(self.temp, ignore_errors=True)

    def _run(self, request, lang="en", extra=()):
        out = os.path.join(self.temp, "session.json")
        proc = subprocess.run([sys.executable, ASK, "--workspace", self.work,
                               "--request", request, "--lang", lang, "--out", out]
                              + list(extra), capture_output=True, text=True, timeout=300)
        return proc, out

    def test_verified_exit_zero(self):
        proc, out = self._run("remove the mutable default in collect")
        self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
        report = json.load(open(out, "r", encoding="utf-8"))
        self.assertEqual(report["verdict"], "VERIFIED")
        self.assertEqual(report["assurance"], 3)
        self.assertEqual(len(report["transcript"]), 7)

    def test_workspace_untouched_without_in_process(self):
        before = open(os.path.join(self.work, "registry.py"), "r", encoding="utf-8").read()
        self._run("remove the mutable default in collect")
        after = open(os.path.join(self.work, "registry.py"), "r", encoding="utf-8").read()
        self.assertEqual(before, after)

    def test_out_of_catalog_is_exit_one(self):
        proc, out = self._run("make this module faster please")
        self.assertEqual(proc.returncode, 1)
        report = json.load(open(out, "r", encoding="utf-8"))
        self.assertEqual(report["verdict"], "REFUSED")

    def test_unknown_variety_is_exit_two(self):
        proc, _ = self._run("remove the mutable default in collect", lang="qq")
        self.assertEqual(proc.returncode, 2)

    def test_launchers_written_in_the_copy(self):
        # A copy is made only when not in process; check the marker in a manual copy.
        self.assertTrue(os.path.isfile(ASK))


if __name__ == "__main__":
    unittest.main()