返回 CodeWhale
test_measure_runtime_contract.py
根目录 / scripts / test_measure_runtime_contract.py
1 #!/usr/bin/env python3
2 """Hermetic tests for scripts/measure-runtime-contract.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 unittest
12 from contextlib import redirect_stdout
13 from pathlib import Path
14 from unittest import mock
15
16 ROOT = Path(__file__).resolve().parents[1]
17 SCRIPT = ROOT / "scripts" / "measure-runtime-contract.py"
18
19 SPEC = importlib.util.spec_from_file_location("measure_runtime_contract", SCRIPT)
20 assert SPEC and SPEC.loader
21 mod = importlib.util.module_from_spec(SPEC)
22 sys.modules[SPEC.name] = mod
23 SPEC.loader.exec_module(mod)
24
25
26 class RuntimeContractMeasurementTests(unittest.TestCase):
27 def test_metric_runner_targets_exact_library_test(self) -> None:
28 short_name = "print_mode_tool_catalog_metrics"
29 exact_name = f"core::engine::tests::{short_name}"
30 marker = "TOOL_CATALOG_METRICS "
31 payload = {"modes": {}, "surface_profile": "fixture"}
32 completed = subprocess.CompletedProcess(
33 args=[],
34 returncode=0,
35 stdout=(
36 "running 1 test\n"
37 f"test {exact_name} ... {marker}{json.dumps(payload)}\n"
38 "ok\n\n"
39 "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; "
40 "0 filtered out\n"
41 ),
42 stderr="",
43 )
44
45 with mock.patch.object(mod.subprocess, "run", return_value=completed) as run:
46 measured = mod.run_metric(short_name, marker)
47
48 self.assertEqual(measured, payload)
49 self.assertEqual(
50 run.call_args.args[0],
51 [
52 "cargo",
53 "test",
54 "--locked",
55 "-p",
56 "codewhale-tui",
57 "--lib",
58 exact_name,
59 "--",
60 "--ignored",
61 "--exact",
62 "--nocapture",
63 "--test-threads=1",
64 ],
65 )
66
67 def test_successful_zero_test_run_is_rejected(self) -> None:
68 completed = subprocess.CompletedProcess(
69 args=[],
70 returncode=0,
71 stdout=(
72 "running 0 tests\n\n"
73 "test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; "
74 "0 filtered out\n"
75 ),
76 stderr="",
77 )
78
79 with (
80 mock.patch.object(mod.subprocess, "run", return_value=completed),
81 redirect_stdout(io.StringIO()),
82 self.assertRaisesRegex(RuntimeError, "ran zero tests"),
83 ):
84 mod.run_metric(
85 "print_mode_tool_catalog_metrics", "TOOL_CATALOG_METRICS "
86 )
87
88 def test_successful_test_without_expected_marker_is_rejected(self) -> None:
89 completed = subprocess.CompletedProcess(
90 args=[],
91 returncode=0,
92 stdout=(
93 "running 1 test\n"
94 "test core::engine::tests::print_mode_tool_catalog_metrics ... ok\n\n"
95 "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; "
96 "0 filtered out\n"
97 ),
98 stderr="",
99 )
100
101 with (
102 mock.patch.object(mod.subprocess, "run", return_value=completed),
103 redirect_stdout(io.StringIO()),
104 self.assertRaisesRegex(
105 RuntimeError,
106 "missing TOOL_CATALOG_METRICS marker from exact library metric test",
107 ),
108 ):
109 mod.run_metric(
110 "print_mode_tool_catalog_metrics", "TOOL_CATALOG_METRICS "
111 )
112
113
114 if __name__ == "__main__":
115 raise SystemExit(unittest.main())
116
116 lines PYTHON