返回 CodeWhale
test_check_command_core_navigation_proof.py
根目录 / scripts / test_check_command_core_navigation_proof.py
1 import importlib.util
2 from pathlib import Path
3 import re
4 import tempfile
5 import unittest
6
7 path = Path(__file__).with_name("check-command-core-navigation-proof.py")
8 spec = importlib.util.spec_from_file_location("navigation_proof", path)
9 proof = importlib.util.module_from_spec(spec)
10 spec.loader.exec_module(proof)
11
12
13 class NavigationProofTests(unittest.TestCase):
14 def fixture(self, root):
15 for relative in proof.REQUIRED | {proof.PROOF, proof.MANIFEST, proof.HOST}:
16 target = root / relative
17 target.parent.mkdir(parents=True, exist_ok=True)
18 target.write_text((proof.ROOT / relative).read_text())
19
20 def test_actual_complete_group_is_included(self):
21 self.assertEqual(proof.violations(proof.ROOT), [])
22 with tempfile.TemporaryDirectory() as tmp:
23 root = Path(tmp)
24 self.fixture(root)
25 host = root / proof.HOST
26 source = host.read_text()
27 unrelated = 'Box::new(FunctionCommand::new(extra::ExtraCmd::info(), extra::ExtraCmd::execute)),\n'
28 host.write_text(source.replace(
29 'crate::commands::core_navigation_host::registration::<0>(),',
30 unrelated + 'crate::commands::core_navigation_host::registration::<0>(),',
31 ))
32 self.assertEqual(proof.violations(root), [])
33
34 def test_selected_leaf_stub_and_test_only_root_are_rejected(self):
35 actual = (proof.ROOT / proof.PROOF).read_text()
36 for source in [
37 "pub mod navigation {}",
38 actual.replace("navigation.rs", "hotbar.rs"),
39 "#[cfg(test)]\n" + actual,
40 actual + "struct App;\n",
41 ]:
42 with self.subTest(source=source), tempfile.TemporaryDirectory() as tmp:
43 root = Path(tmp)
44 self.fixture(root)
45 (root / proof.PROOF).write_text(source)
46 self.assertTrue(proof.violations(root))
47 with tempfile.TemporaryDirectory() as tmp:
48 root = Path(tmp)
49 self.fixture(root)
50 source = root / proof.ROOTS[0]
51 source.write_text("#[cfg(test)]\n" + source.read_text())
52 self.assertTrue(any("conditional" in error for error in proof.violations(root)))
53
54 def test_omitted_root_or_helper_is_rejected(self):
55 for case in ["anchor", "voice", "inventory", "host_order", "host_duplicate", "host_reorder", "metadata", "include"]:
56 with self.subTest(case=case), tempfile.TemporaryDirectory() as tmp:
57 root = Path(tmp)
58 self.fixture(root)
59 source = root / proof.ROOTS[0]
60 if case in {"anchor", "voice"}:
61 source.write_text(re.sub(r'#\[path = "' + case + r'\.rs"\]\s*pub mod ' + case + r';', '', source.read_text()))
62 elif case == "inventory":
63 source.write_text(source.read_text().replace("(anchor::AnchorCmd::info(), anchor::AnchorCmd::handler()),", ""))
64 elif case == "host_order":
65 host = root / proof.HOST
66 host.write_text(host.read_text().replace("registration::<0>()", "registration::<99>()"))
67 elif case in {"host_duplicate", "host_reorder"}:
68 host = root / proof.HOST
69 text = host.read_text()
70 if case == "host_duplicate":
71 text = text.replace('registration::<1>()', 'registration::<0>()')
72 else:
73 text = text.replace('registration::<0>()', 'registration::<TEMP>()')
74 text = text.replace('registration::<1>()', 'registration::<0>()')
75 text = text.replace('registration::<TEMP>()', 'registration::<1>()')
76 host.write_text(text)
77 self.assertTrue(any('relative order' in error for error in proof.violations(root)))
78 elif case == "include":
79 source.write_text(source.read_text() + '\ninclude!("untracked-helper.rs");\n')
80 else:
81 leaf = root / proof.GROUP / "anchor.rs"
82 leaf.write_text(leaf.read_text().replace('name: "anchor"', 'name: "wrong"'))
83 self.assertTrue(proof.violations(root))
84
85 def test_host_import_in_any_mounted_file_is_rejected(self):
86 for name in sorted(proof.REQUIRED):
87 with self.subTest(name=name), tempfile.TemporaryDirectory() as tmp:
88 root = Path(tmp)
89 self.fixture(root)
90 leaf = root / name
91 leaf.write_text(leaf.read_text() + "\nuse crate::tui::app::App;\n")
92 self.assertTrue(proof.violations(root))
93
94 def test_host_import_in_unmounted_group_file_is_rejected(self):
95 with tempfile.TemporaryDirectory() as tmp:
96 root = Path(tmp)
97 self.fixture(root)
98 source = root / proof.ROOTS[0]
99 source.write_text(re.sub(r'#\[path = "voice\.rs"\]\s*pub mod voice;', '', source.read_text()))
100 leaf = root / proof.GROUP / "voice.rs"
101 leaf.write_text(leaf.read_text() + "\nuse codewhale_runtime::Runtime;\n")
102 errors = proof.violations(root)
103 self.assertTrue(any("host/service in declared movable source" in error for error in errors))
104
105 def test_shapes_cannot_move_to_dev_dependencies(self):
106 with tempfile.TemporaryDirectory() as tmp:
107 root = Path(tmp)
108 self.fixture(root)
109 manifest = root / proof.MANIFEST
110 manifest.write_text(manifest.read_text().replace("[dependencies]", "[dev-dependencies]"))
111 self.assertTrue(proof.violations(root))
112
113 def test_direct_and_transitive_runtime_or_storage_edges_are_rejected(self):
114 allowed = "codewhale-portable-core-navigation v0.10.2\ncodewhale-command-contract v0.10.2\ncodewhale-protocol v0.10.2\nserde v1.0.0\n"
115 self.assertEqual(proof.graph_violations(allowed), [])
116 for package in ["codewhale-tui", "codewhale-core", "codewhale-runtime", "codewhale-config",
117 "codewhale-secrets", "codewhale-state", "codewhale-localization", "tokio",
118 "reqwest", "rusqlite", "keyring", "ratatui", "crossterm", "unreviewed-helper"]:
119 with self.subTest(package=package):
120 self.assertEqual(len(proof.graph_violations(allowed + f"{package} v1.0.0\n")), 1)
121 self.assertTrue(proof.graph_violations("serde v1.0.0\n"))
122
123
124 if __name__ == "__main__":
125 unittest.main(verbosity=2)
126
126 lines PYTHON