| 1 | #!/usr/bin/env python3 |
| 2 | """Hermetic tests for scripts/measure-persistence-backlog.py.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | import importlib.util |
| 7 | import io |
| 8 | import json |
| 9 | import subprocess |
| 10 | import sys |
| 11 | import tempfile |
| 12 | import unittest |
| 13 | from contextlib import redirect_stdout |
| 14 | from pathlib import Path |
| 15 | from unittest import mock |
| 16 | |
| 17 | ROOT = Path(__file__).resolve().parents[1] |
| 18 | SCRIPT = ROOT / "scripts" / "measure-persistence-backlog.py" |
| 19 | |
| 20 | SPEC = importlib.util.spec_from_file_location("measure_persistence_backlog", SCRIPT) |
| 21 | assert SPEC and SPEC.loader |
| 22 | mod = importlib.util.module_from_spec(SPEC) |
| 23 | sys.modules[SPEC.name] = mod |
| 24 | SPEC.loader.exec_module(mod) |
| 25 | |
| 26 | |
| 27 | class PersistenceBacklogMeasurementTests(unittest.TestCase): |
| 28 | def test_runner_targets_exact_library_test(self) -> None: |
| 29 | completed = subprocess.CompletedProcess( |
| 30 | args=[], |
| 31 | returncode=0, |
| 32 | stdout=( |
| 33 | "running 1 test\n" |
| 34 | f"test {mod.TEST_NAME} ... ok\n\n" |
| 35 | "test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; " |
| 36 | "0 filtered out\n" |
| 37 | ), |
| 38 | stderr="", |
| 39 | ) |
| 40 | receipt = {"document_kind": "fixture"} |
| 41 | |
| 42 | with tempfile.TemporaryDirectory() as root: |
| 43 | receipt_path = Path(root) / "receipt.json" |
| 44 | receipt_path.write_text(json.dumps(receipt), encoding="utf-8") |
| 45 | with mock.patch.object( |
| 46 | mod.subprocess, "run", return_value=completed |
| 47 | ) as run: |
| 48 | measured = mod.run_measurement(receipt_path, {"FIXTURE": "1"}) |
| 49 | |
| 50 | self.assertEqual(measured, receipt) |
| 51 | self.assertEqual( |
| 52 | run.call_args.args[0], |
| 53 | [ |
| 54 | "cargo", |
| 55 | "test", |
| 56 | "--locked", |
| 57 | "--all-features", |
| 58 | "-p", |
| 59 | "codewhale-tui", |
| 60 | "--lib", |
| 61 | mod.TEST_NAME, |
| 62 | "--", |
| 63 | "--exact", |
| 64 | "--ignored", |
| 65 | "--test-threads=1", |
| 66 | ], |
| 67 | ) |
| 68 | self.assertEqual(run.call_args.kwargs["cwd"], mod.ROOT) |
| 69 | |
| 70 | def test_successful_zero_test_run_is_rejected(self) -> None: |
| 71 | completed = subprocess.CompletedProcess( |
| 72 | args=[], |
| 73 | returncode=0, |
| 74 | stdout=( |
| 75 | "running 0 tests\n\n" |
| 76 | "test result: ok. 0 passed; 0 failed; 1 ignored; 0 measured; " |
| 77 | "1 filtered out\n" |
| 78 | ), |
| 79 | stderr="", |
| 80 | ) |
| 81 | |
| 82 | with tempfile.TemporaryDirectory() as root: |
| 83 | receipt_path = Path(root) / "receipt.json" |
| 84 | receipt_path.write_text("{}", encoding="utf-8") |
| 85 | with ( |
| 86 | mock.patch.object(mod.subprocess, "run", return_value=completed), |
| 87 | redirect_stdout(io.StringIO()), |
| 88 | self.assertRaisesRegex( |
| 89 | mod.PersistenceBacklogMeasurementError, "ran zero tests" |
| 90 | ), |
| 91 | ): |
| 92 | mod.run_measurement(receipt_path, {}) |
| 93 | |
| 94 | def test_successful_test_without_receipt_is_rejected(self) -> None: |
| 95 | completed = subprocess.CompletedProcess( |
| 96 | args=[], |
| 97 | returncode=0, |
| 98 | stdout=( |
| 99 | "running 1 test\n" |
| 100 | f"test {mod.TEST_NAME} ... ok\n\n" |
| 101 | "test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; " |
| 102 | "0 filtered out\n" |
| 103 | ), |
| 104 | stderr="", |
| 105 | ) |
| 106 | |
| 107 | with tempfile.TemporaryDirectory() as root: |
| 108 | with ( |
| 109 | mock.patch.object(mod.subprocess, "run", return_value=completed), |
| 110 | self.assertRaisesRegex( |
| 111 | mod.PersistenceBacklogMeasurementError, |
| 112 | "emitted no valid receipt", |
| 113 | ), |
| 114 | ): |
| 115 | mod.run_measurement(Path(root) / "missing.json", {}) |
| 116 | |
| 117 | |
| 118 | if __name__ == "__main__": |
| 119 | raise SystemExit(unittest.main()) |
| 120 |