"""Product and personal data are separate: the overlay, generated rulebook, migration, update, export/import, install migration, and the property that sisi never writes inside the product.""" import hashlib, json, os, signal, time, shutil, sqlite3, stat, subprocess, sys, tarfile, unittest, unittest.mock from support import * from sisi import lifecycle, overlay, paths, pdata, wiring RULES = [("alpha", "Alpha rule", "Do alpha things.\n\nWhy: alpha."), ("beta", "Beta rule", "Do beta things.\n\nWhy: beta."), ("gamma", "Gamma rule", "Do gamma things.\n\nWhy: gamma.")] def core_text(rules): out = ["# test rulebook", "", "Intro line."] for n, (i, t, b) in enumerate(rules, 1): out += ["", "## %d. %s {#%s}" % (n, t, i), "", b] return "\n".join(out) + "\n" def rule_file(title, body): return "## %s\n\n%s\n" % (title, body) class PersonalCase(SisiCase): def make_product(self, name, rules=RULES, edit_library=False): root = os.path.join(self.tmp, name) os.makedirs(os.path.join(root, "canon")) for lib in ("verifiers", "done"): # linked (read only) unless the test changes a library file if edit_library: shutil.copytree(os.path.join(REPO, lib), os.path.join(root, lib)) else: os.symlink(os.path.join(REPO, lib), os.path.join(root, lib)) self.write_product_core(root, rules) return root def write_product_core(self, root, rules): with open(os.path.join(root, "canon", "core.md"), "w") as f: f.write(core_text(rules)) def use_product(self, root): old = (paths.REPO, os.environ.get("SISI_VERIFIERS"), os.environ.get("SISI_DONE")) paths.REPO = root os.environ["SISI_VERIFIERS"] = os.path.join(root, "verifiers") os.environ["SISI_DONE"] = os.path.join(root, "done") def undo(): paths.REPO = old[0] for k, v in zip(("SISI_VERIFIERS", "SISI_DONE"), old[1:]): os.environ.pop(k, None) if v is None else os.environ.__setitem__(k, v) self.addCleanup(undo) def setUp(self): super().setUp() self.p1 = self.make_product("p1") self.use_product(self.p1) def ok(self, *a, **kw): r = self.sisi(*a, **kw) self.assertEqual(r.returncode, 0, r.stdout + r.stderr) return r def generated(self): with open(paths.generated()) as f: return f.read() def git_log(self): return pdata.git("log", "--format=%s").stdout.splitlines() def verifier_text(self, id, title="Mine", src="safety/shellcheck"): with open(os.path.join(REPO, "verifiers", src + ".md")) as f: t = f.read() t = t.replace("id: shellcheck", "id: " + id, 1) return "\n".join("title: " + title if l.startswith("title:") else l for l in t.split("\n")) def recipe_text(self, id, title="Mine"): with open(os.path.join(REPO, "done", "bugfix.md")) as f: t = f.read() t = t.replace("id: bugfix", "id: " + id, 1) return "\n".join("title: " + title if l.startswith("title:") else l for l in t.split("\n")) class OverlayTests(PersonalCase): def test_first_use_creates_personal_repo(self): self.ok("build-canon") self.assertTrue(os.path.isdir(os.path.join(self.data, ".git"))) self.assertEqual(open(os.path.join(self.data, "VERSION")).read().strip(), str(pdata.FORMAT_VERSION)) self.assertIn("retro.db", open(os.path.join(self.data, ".gitignore")).read()) g = self.generated() self.assertTrue(g.startswith("