"""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()