import copy import json import os from pathlib import Path import subprocess import sys import tempfile import unittest import diagnose import observe class DiagnosticTests(unittest.TestCase): def test_retained_rows_and_hashes(self): evidence = json.loads(Path(__file__).with_name("diagnostics.json").read_text()) self.assertEqual(len(evidence["runs"]), 2) for name, digest in evidence["codeSha256"].items(): self.assertEqual(observe.digest(Path(__file__).with_name(name).read_bytes()), digest) for run in evidence["runs"]: self.assertEqual({row["mode"] for row in run["cases"]}, {"inplace", "temporary"}) for row in run["cases"]: self.assertTrue(diagnose.validate_case(row)) for mutation in ("changed-target", "selection", "not-dry", "raw-output", "duplicate"): changed = copy.deepcopy(row) check = changed["inspections"][0] if mutation == "changed-target": check["destinationAfter"]["sha256"] = "bad" elif mutation == "selection": check["selected"] = not check["selected"] elif mutation == "not-dry": check["command"].remove("--dry-run") elif mutation == "raw-output": check["stdout"] = "unrelated" else: changed["inspections"][1] = copy.deepcopy(check) with self.assertRaises(RuntimeError, msg=mutation): diagnose.validate_case(changed) def test_refuses_existing_output(self): with tempfile.TemporaryDirectory() as root: output = Path(root) / "keep.json" output.write_text("preserve") result = subprocess.run([sys.executable, "-B", diagnose.__file__, "--binary", "/usr/bin/rsync", "--scratch", root, "--output", str(output)], capture_output=True) self.assertNotEqual(result.returncode, 0) self.assertEqual(output.read_text(), "preserve") @unittest.skipUnless(os.environ.get("HESELA_RSYNC_LIVE") == "1", "Opt-in native checks") def test_native_inspections(self): binary = os.environ.get("HESELA_RSYNC_BINARY", "/usr/bin/rsync") with tempfile.TemporaryDirectory() as root: for mode in ("inplace", "temporary"): self.assertTrue(diagnose.validate_case(diagnose.inspect_case(binary, root, mode))) if __name__ == "__main__": unittest.main()