返回 CodeWhale
test_check_command_config_proof.py
根目录 / scripts / test_check_command_config_proof.py
1 """Executable negative controls for the complete config-group extraction proof.
2
3 FEAT-028 proves the WHOLE config group, so the controls must reject a
4 selected-leaf proof, a cfg(test)-only mount, a missing root or helper, a host
5 import anywhere in the mounted closure (including `config.rs`), a dev-dependency
6 swap, and forbidden normal graph edges.
7 """
8 import importlib.util
9 from pathlib import Path
10 import re
11 import tempfile
12 import unittest
13
14 path = Path(__file__).with_name("check-command-config-proof.py")
15 spec = importlib.util.spec_from_file_location("config_proof", path)
16 proof = importlib.util.module_from_spec(spec)
17 spec.loader.exec_module(proof)
18
19
20 class ConfigProofTests(unittest.TestCase):
21 def fixture(self, root):
22 for relative in proof.REQUIRED | {proof.PROOF, proof.MANIFEST}:
23 target = root / relative
24 target.parent.mkdir(parents=True, exist_ok=True)
25 target.write_text((proof.ROOT / relative).read_text())
26
27 def test_actual_complete_group_is_included(self):
28 self.assertEqual(proof.violations(proof.ROOT), [])
29
30 def test_selected_leaf_stub_and_test_only_root_are_rejected(self):
31 for source in [
32 'pub mod config_group {}',
33 '#[path = "config.rs"] pub mod config_group;',
34 '#[cfg(test)]\n' + (proof.ROOT / proof.PROOF).read_text(),
35 ]:
36 with self.subTest(source=source), tempfile.TemporaryDirectory() as tmp:
37 root = Path(tmp)
38 self.fixture(root)
39 (root / proof.PROOF).write_text(source)
40 self.assertTrue(proof.violations(root))
41
42 def test_omitted_root_or_helper_is_rejected(self):
43 group_dir = "crates/tui/src/commands/groups/config"
44 for name in ["config", "status", "policy_messages", "money", "tests"]:
45 with self.subTest(name=name), tempfile.TemporaryDirectory() as tmp:
46 root = Path(tmp)
47 self.fixture(root)
48 if name == "config":
49 # The group root must include its children.
50 group_root = root / proof.ROOTS[0]
51 group_root.write_text(
52 group_root.read_text().replace("pub mod config;\n", ""))
53 elif name == "money":
54 policy = root / group_dir / "policy.rs"
55 policy.write_text(policy.read_text().replace(
56 "use codewhale_command_contract::money;", ""))
57 else:
58 policy = root / group_dir / "policy.rs"
59 policy.write_text(re.sub(
60 r'#\[path = "[^"]+"\]\s*(?:pub(?:\([^)]*\))?\s+)?mod ' + name + r';',
61 '', policy.read_text()))
62 self.assertTrue(proof.violations(root))
63
64 def test_host_import_in_any_mounted_file_is_rejected(self):
65 for source in [
66 'use crate::tui::app::App;',
67 'use codewhale_config::Config;',
68 'use crate::commands::CommandResult;',
69 'use std::fs;',
70 'use codewhale_secrets::Store;',
71 ]:
72 with self.subTest(source=source), tempfile.TemporaryDirectory() as tmp:
73 root = Path(tmp)
74 self.fixture(root)
75 group_root = root / proof.ROOTS[0]
76 group_root.write_text(group_root.read_text() + "\n" + source + "\n")
77 self.assertTrue(proof.violations(root))
78
79 def test_host_import_in_unmounted_group_file_is_rejected(self):
80 # A new file in the group directory that is not reachable from the
81 # mounted root must still be rejected by the whole-directory scan, which
82 # is the selected-source gap FEAT-026 corrected.
83 with tempfile.TemporaryDirectory() as tmp:
84 root = Path(tmp)
85 self.fixture(root)
86 stray = root / "crates/tui/src/commands/groups/config/stray_helper.rs"
87 stray.write_text("use crate::tui::app::App;\n")
88 self.assertTrue(proof.violations(root))
89
90 def test_shapes_cannot_move_to_dev_dependencies(self):
91 with tempfile.TemporaryDirectory() as tmp:
92 root = Path(tmp)
93 self.fixture(root)
94 manifest = root / proof.MANIFEST
95 manifest.write_text(
96 manifest.read_text().replace("[dev-dependencies]", "[build-dependencies]")
97 .replace("[dependencies]", "[dev-dependencies]"))
98 self.assertTrue(proof.violations(root))
99
100 def test_direct_and_transitive_runtime_or_storage_edges_are_rejected(self):
101 allowed = ("codewhale-portable-config v0.10.2\ncodewhale-command-contract v0.10.2\n"
102 "codewhale-protocol v0.10.2\ncodewhale-sanitize v0.10.2\nserde v1.0.0\n")
103 self.assertEqual(proof.graph_violations(allowed), [])
104 for package in ["codewhale-tui", "codewhale-config", "codewhale-core", "codewhale-runtime",
105 "codewhale-execpolicy", "codewhale-secrets", "codewhale-state",
106 "codewhale-localization", "tokio", "reqwest", "rusqlite", "keyring",
107 "ratatui", "crossterm"]:
108 with self.subTest(package=package):
109 self.assertEqual(
110 len(proof.graph_violations(allowed + f"{package} v1.0.0\n")), 1)
111
112
113 if __name__ == '__main__':
114 unittest.main(verbosity=2)
115
115 lines PYTHON