Files
sisi/tests/test_risk.py
T

284 lines
14 KiB
Python

"""Risk profiles (R-5.3-1, R-5.3-2, R-5.3-3..R-5.3-10)."""
import json, os, random, unittest
from support import *
from sisi import risk
class FloorTests(SisiCase):
def test_docs_only_is_trivial(self):
"""R-5.3-4: documentation/text/styling only is trivial."""
p, reasons = risk.assess(["README.md", "docs/guide.txt", "web/site.css"])
self.assertEqual(p, "trivial")
self.assertTrue(reasons)
def test_single_module_is_isolated(self):
"""R-5.3-5: one module's behaviour is isolated."""
p, _ = risk.assess(["src/calc.py"])
self.assertEqual(p, "isolated")
def test_logic_kinds(self):
"""R-5.3-6: algorithms, parsing and money are logic."""
for path in ("src/parser/lexer.py", "src/money/rounding.py", "src/algo/sort.py"):
with self.subTest(path=path):
p, _ = risk.assess([path])
self.assertEqual(p, "logic")
def test_two_packages_is_integration(self):
"""R-5.3-7, R-5.3-11: several top-level packages is integration."""
p, reasons = risk.assess(["src/alpha/a.py", "src/beta/b.py"])
self.assertEqual(p, "integration")
self.assertTrue(any("package" in r for r in reasons))
def test_migration_is_data(self):
"""R-5.3-8: schema/persistence/migration is data."""
p, _ = risk.assess(["db/migrations/0002_add_col.sql"])
self.assertEqual(p, "data")
def test_auth_is_critical(self):
"""R-5.3-9: authorization/secrets/concurrency primitives are critical."""
for path in ("src/auth/login.py", "src/crypto/keys.py", "src/sync/mutex.py"):
with self.subTest(path=path):
p, _ = risk.assess([path])
self.assertEqual(p, "critical")
def test_diff_keyword_raises_floor(self):
"""R-5.3-1: content keywords count, not only paths."""
p, _ = risk.assess(["src/util.py"], diff_text="+ encrypt the password at rest")
self.assertEqual(p, "critical")
def test_adding_path_never_lowers_floor(self):
"""R-5.3-1: property — the floor is monotone in the change set."""
rng = random.Random(7)
pool = ["README.md", "src/a/x.py", "src/b/y.py", "db/migrations/m.sql",
"src/auth/login.py", "src/parser/p.py", "VERSION", "web/site.css"]
order = {name: i for i, name in enumerate(risk.PROFILES)}
for _ in range(80):
base = rng.sample(pool, rng.randint(0, 4))
extra = rng.choice(pool)
before, _ = risk.assess(base)
after, _ = risk.assess(base + [extra])
self.assertGreaterEqual(order[after], order[before], (base, extra))
def test_project_override_and_requirements_table(self):
"""R-5.3-3: a project may override; each profile carries its requirements."""
p, _ = risk.assess(["src/special/thing.py"],
extra_patterns=[{"glob": "**/special/**", "profile": "data",
"reason": "project override: special holds stored data"}])
self.assertEqual(p, "data")
req = risk.requirements_for("trivial")
self.assertFalse(req["contract"])
self.assertTrue(risk.requirements_for("critical")["independent_evidence"])
self.assertTrue(risk.requirements_for("release")["contract"])
LABELS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "risk_labels.json")
HELDOUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "risk_labels_heldout.json")
def _labels():
with open(LABELS, encoding="utf-8") as f:
return json.load(f)
BLIND2 = os.path.join(os.path.dirname(os.path.abspath(__file__)), "risk_labels_blind2.json")
def _blind2():
with open(BLIND2, encoding="utf-8") as f:
return [{"path": r["path"], "floor": r["floor"], "why": r["reason"]} for r in json.load(f)]
def _heldout():
with open(HELDOUT, encoding="utf-8") as f:
return [{"path": r["path"], "floor": r["floor"], "why": r["reason"]} for r in json.load(f)]
class LabelledSetTests(SisiCase):
"""The default table against paths a careful senior engineer has already judged."""
def test_labelled_set_is_large_and_covers_every_profile(self):
"""R-5.3-3: the set is big enough to mean something and spans every profile."""
rows = _labels()
self.assertGreaterEqual(len(rows), 120)
self.assertEqual({r["floor"] for r in rows}, set(risk.PROFILES))
self.assertEqual(len({r["path"] for r in rows}), len(rows))
for r in rows:
self.assertTrue(r["why"].strip(), r)
def test_every_labelled_path_gets_its_labelled_floor(self):
"""R-5.3-1, R-5.3-2: exact accuracy. Too high wastes a contract and a review on a
trivial change (SPEC 5.3); too low leaves a dangerous change unexamined. Seen failing
first: the old table got 91 of 177 right (lockfiles critical, React hooks critical,
AUTHORS.md critical, release-notes.md release, db/README.md data, sparse.py logic)."""
wrong = []
for r in _labels():
got, _ = risk.assess([r["path"]])
if got != r["floor"]:
wrong.append("%s: labelled %s, got %s (%s)" % (r["path"], r["floor"], got, r["why"]))
self.assertEqual(wrong, [], "\n".join(wrong))
def test_no_data_or_critical_path_is_ever_under_classified(self):
"""R-5.3-8, R-5.3-9: data and critical paths must never get a lower floor, even if
someone loosens the exact test above. Seen failing first: the old table gave eight of
them a lower floor."""
order = {name: i for i, name in enumerate(risk.PROFILES)}
under = []
for r in _labels():
if r["floor"] in ("data", "critical"):
got, _ = risk.assess([r["path"]])
if order[got] < order[r["floor"]]:
under.append("%s: %s < %s" % (r["path"], got, r["floor"]))
self.assertEqual(under, [], "\n".join(under))
def test_second_labelled_set_gets_its_labelled_floor(self):
"""R-5.3-1, R-5.3-2: the second set, written blind by a different worker, also scores
exactly. It is NO LONGER HELD OUT: the table was fixed against its 16 disagreements
(class by class, with new cases added to the first set), so its score says nothing about
generalisation. A fresh blind set must be commissioned to measure that. Seen failing
first: 73 of 89 right before the fixes."""
order = {name: i for i, name in enumerate(risk.PROFILES)}
rows = _heldout()
self.assertGreaterEqual(len(rows), 80)
wrong = []
for r in rows:
got, _ = risk.assess([r["path"]])
if got != r["floor"]:
wrong.append("%s: labelled %s, got %s (%s)" % (r["path"], r["floor"], got, r["why"]))
self.assertEqual(wrong, [], "\n".join(wrong))
for r in rows: # R-5.3-8, R-5.3-9: never under-rate data or critical
if r["floor"] in ("data", "critical"):
self.assertGreaterEqual(order[risk.assess([r["path"]])[0]], order[r["floor"]], r["path"])
def test_third_labelled_set_gets_its_labelled_floor(self):
"""R-5.3-1, R-5.3-2: tests/risk_labels_blind2.json scores exactly. This set was blind
when first measured (89 of 106 right, six data-or-critical paths under-rated) and has
been used for tuning since, so it no longer measures generalisation: only a fresh blind
set can. Two of its labels were revised after review (checkout_machine.ex critical,
docker/entrypoint.sh integration), each carrying a "relabelled" note. Seen failing
first: see the count in the commit that added this test."""
order = {name: i for i, name in enumerate(risk.PROFILES)}
rows = _blind2()
self.assertGreaterEqual(len(rows), 100)
wrong = []
for r in rows:
got, _ = risk.assess([r["path"]])
if got != r["floor"]:
wrong.append("%s: labelled %s, got %s (%s)" % (r["path"], r["floor"], got, r["why"]))
elif r["floor"] in ("data", "critical"):
self.assertGreaterEqual(order[got], order[r["floor"]], r["path"])
self.assertEqual(wrong, [], "\n".join(wrong))
def test_source_code_is_never_trivial(self):
"""R-5.3-2, R-5.3-4: file kind comes from the extension first. A file with a
source-code extension is never `trivial`, whatever its name or directory:
documentation-like and asset-like names make a file trivial only when it is not code.
Cases are generated from the code extensions the table lists, crossed with names and
directories. Seen failing first: AuthorList.tsx, docs/conf.py, locales/index.ts and
every LICENSE*/AUTHORS*/NOTICE*/docs/ code file came out trivial."""
import re
exts = set()
for pat in risk.default_table()["patterns"]:
if pat.get("stage") == "code":
for g in pat["glob"] if isinstance(pat["glob"], list) else [pat["glob"]]:
for alt in risk._expand_braces(g):
exts.add(alt.rsplit(".", 1)[1])
self.assertGreaterEqual(len(exts), 20, "the table must list the code extensions")
dirs = ["", "docs/", "doc/", "documentation/", "src/docs/", "assets/", "images/", "static/",
"public/img/", "fonts/", "locales/", "i18n/", "translations/", "styles/", "web/src/components/Authors/"]
names = ["index", "README", "LICENSE", "AUTHORS", "NOTICE", "CHANGELOG", "AuthorList",
"contributors", "copying", "readme_helper", "logo", "theme"]
bad = []
for e in sorted(exts):
for d in dirs:
for n in names:
path = "%s%s.%s" % (d, n, e)
got, _ = risk.assess([path])
if got == "trivial":
bad.append(path)
self.assertEqual(bad, [], "source files rated trivial: %s" % bad[:10])
def test_whole_words_not_prefixes(self):
"""R-5.3-2: parsley is not parsing, a secretary is not a secret."""
for path in ("src/recipes/parsley.py", "src/secretary/office_hours.py", "lib/secretariat/minutes.rb"):
self.assertEqual(risk.assess([path])[0], "isolated", path)
for path in ("src/parser.py", "src/parsing/x.go", "src/secrets/store.py"):
self.assertGreaterEqual(risk.PROFILES.index(risk.assess([path])[0]), 2, path)
def test_named_false_positives(self):
"""R-5.3-2: the known false positives are not escalated."""
expect = {
"package-lock.json": "integration", "Cargo.lock": "integration", "poetry.lock": "integration",
"flake.lock": "integration", "src/hooks/useCart.ts": "isolated", "AUTHORS.md": "trivial",
"src/components/AuthorCard.tsx": "isolated", "docs/release-notes.md": "trivial",
"src/db/README.md": "trivial", "src/sparse.py": "isolated", "docs/installation.md": "trivial"}
for path, floor in expect.items():
with self.subTest(path=path):
self.assertEqual(risk.assess([path])[0], floor)
def test_dependency_change_is_integration_not_trivial_or_critical(self):
"""R-5.3-7, R-5.3-11: a manifest or lockfile changes code you did not write."""
for path in ("package.json", "yarn.lock", "go.mod", "Gemfile.lock", "requirements.txt"):
with self.subTest(path=path):
self.assertEqual(risk.assess([path])[0], "integration")
def test_test_of_critical_code_is_logic(self):
"""R-5.3-9: a test of critical code is not critical, and not trivial."""
self.assertEqual(risk.assess(["tests/test_auth.py"])[0], "logic")
self.assertEqual(risk.assess(["tests/test_widget.py"])[0], "isolated")
def test_code_with_its_test_is_not_a_cross_package_change(self):
"""R-5.3-2: the commonest change, code plus its test, is not an integration."""
self.assertEqual(risk.assess(["sisi/risk.py", "tests/test_risk.py"])[0], "isolated")
self.assertEqual(risk.assess(["src/a/x.py", "docs/a.md", "src/a/x.test.ts"])[0], "isolated")
def test_keywords_in_docs_and_lockfiles_do_not_escalate(self):
"""R-5.3-2: a README that says 'password' is not a credentials change; code that does is."""
doc = "diff --git a/README.md b/README.md\n+ reset your password\n"
lock = "diff --git a/package-lock.json b/package-lock.json\n+ \"crypto-js\": \"4.2.0\"\n"
self.assertEqual(risk.assess(["README.md"], diff_text=doc)[0], "trivial")
self.assertEqual(risk.assess(["package-lock.json"], diff_text=lock)[0], "integration")
code = "diff --git a/src/a.py b/src/a.py\n+ reset your password\n"
self.assertEqual(risk.assess(["src/a.py"], diff_text=code)[0], "critical")
self.assertEqual(risk.assess(["src/a.py", "README.md"], diff_text=doc + code)[0], "critical")
class RiskCliTests(SisiCase):
def _repo_with(self, files):
self.git_init()
for rel, text in files.items():
p = os.path.join(self.proj, rel)
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(p, "w") as f:
f.write(text)
def test_risk_json_readme_change(self):
"""R-5.3-2: a README edit does not get the full process."""
self._repo_with({"README.md": "# hi\nchanged\n"})
r = self.sisi("risk", "--json")
self.assertEqual(r.returncode, 0, r.stderr)
d = json.loads(r.stdout)
self.assertEqual(d["floor"], "trivial")
self.assertIn("requirements", d)
def test_risk_json_migration_change(self):
"""R-5.3-8: a migrations change floors at data."""
self._repo_with({"db/migrations/0001.sql": "ALTER TABLE t ADD c INT;\n"})
d = json.loads(self.sisi("risk", "--json").stdout)
self.assertEqual(d["floor"], "data")
def test_risk_declared_profile_from_contract(self):
"""R-5.3-3: the declared profile in .sisi/contract.json is reported."""
self._repo_with({"src/a.py": "x = 1\n"})
os.makedirs(os.path.join(self.proj, ".sisi"), exist_ok=True)
with open(os.path.join(self.proj, ".sisi", "contract.json"), "w") as f:
json.dump({"id": "t", "goal": "g", "checks": [], "risk": {"profile": "logic"}}, f)
d = json.loads(self.sisi("risk", "--json").stdout)
self.assertEqual(d["declared"], "logic")
if __name__ == "__main__":
unittest.main()