| 1 | #!/usr/bin/env python3 |
| 2 | """Hermetic tests for the bare-git gate.""" |
| 3 | |
| 4 | from __future__ import annotations |
| 5 | |
| 6 | import importlib.util |
| 7 | import sys |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | from pathlib import Path |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parents[1] |
| 13 | SCRIPT = ROOT / "scripts" / "check-bare-git.py" |
| 14 | SPEC = importlib.util.spec_from_file_location("bare_git", SCRIPT) |
| 15 | assert SPEC and SPEC.loader |
| 16 | mod = importlib.util.module_from_spec(SPEC) |
| 17 | sys.modules[SPEC.name] = mod |
| 18 | SPEC.loader.exec_module(mod) |
| 19 | |
| 20 | |
| 21 | def scan(files: dict[str, str], allowlist: dict | None = None) -> list[str]: |
| 22 | with tempfile.TemporaryDirectory() as tmp: |
| 23 | base = Path(tmp) |
| 24 | root = base / "crates" |
| 25 | for name, text in files.items(): |
| 26 | path = root / name |
| 27 | path.parent.mkdir(parents=True, exist_ok=True) |
| 28 | path.write_text(text, encoding="utf-8") |
| 29 | return mod.find_violations(root, base, allowlist or {}) |
| 30 | |
| 31 | |
| 32 | class BareGitTests(unittest.TestCase): |
| 33 | def test_product_spawn_fails(self) -> None: |
| 34 | hits = scan({"a/src/lib.rs": 'fn f() {\n Command::new("git");\n}\n'}) |
| 35 | self.assertEqual(hits, ["crates/a/src/lib.rs:2"]) |
| 36 | |
| 37 | def test_qualified_and_exe_spellings_fail(self) -> None: |
| 38 | hits = scan( |
| 39 | { |
| 40 | "a/src/lib.rs": 'fn f() {\n std::process::Command::new("git");\n Command::new("git.exe");\n}\n' |
| 41 | } |
| 42 | ) |
| 43 | self.assertEqual(len(hits), 2) |
| 44 | |
| 45 | def test_cfg_test_module_and_test_files_pass(self) -> None: |
| 46 | hits = scan( |
| 47 | { |
| 48 | "a/src/lib.rs": '#[cfg(test)]\nmod tests;\n#[cfg(test)]\n#[path = "x/delivery_tests.rs"]\nmod delivery;\n#[cfg(test)]\nmod inline {\n fn f() {\n Command::new("git");\n }\n}\n', |
| 49 | "a/src/tests.rs": 'mod deeper;\nfn f() { Command::new("git"); }\n', |
| 50 | "a/src/tests/deeper.rs": 'fn f() { Command::new("git"); }\n', |
| 51 | "a/src/x/delivery_tests.rs": 'fn f() { Command::new("git"); }\n', |
| 52 | "a/tests/it.rs": 'fn f() { Command::new("git"); }\n', |
| 53 | "a/src/inner.rs": '#![cfg(test)]\nfn f() { Command::new("git"); }\n', |
| 54 | } |
| 55 | ) |
| 56 | self.assertEqual(hits, []) |
| 57 | |
| 58 | def test_file_names_do_not_exempt_product_code(self) -> None: |
| 59 | hits = scan( |
| 60 | { |
| 61 | "a/src/lib.rs": "pub mod test_runner;\npub mod test_support;\nmod tests;\n", |
| 62 | "a/src/test_runner.rs": 'fn f() {\n Command::new("git");\n}\n', |
| 63 | "a/src/test_support.rs": 'pub fn f() {\n Command::new("git");\n}\n', |
| 64 | "a/src/tests.rs": 'fn f() {\n Command::new("git");\n}\n', |
| 65 | } |
| 66 | ) |
| 67 | self.assertEqual( |
| 68 | hits, |
| 69 | [ |
| 70 | "crates/a/src/test_runner.rs:2", |
| 71 | "crates/a/src/test_support.rs:2", |
| 72 | "crates/a/src/tests.rs:2", |
| 73 | ], |
| 74 | ) |
| 75 | |
| 76 | def test_a_cfg_test_module_declaration_exempts_only_that_file(self) -> None: |
| 77 | hits = scan( |
| 78 | { |
| 79 | "a/src/lib.rs": '#[cfg(test)]\nmod test_support;\nmod other;\n', |
| 80 | "a/src/test_support.rs": 'fn f() { Command::new("git"); }\n', |
| 81 | "a/src/other.rs": 'fn f() {\n Command::new("git");\n}\n', |
| 82 | } |
| 83 | ) |
| 84 | self.assertEqual(hits, ["crates/a/src/other.rs:2"]) |
| 85 | |
| 86 | def test_include_exempts_only_inside_cfg_test(self) -> None: |
| 87 | hits = scan( |
| 88 | { |
| 89 | "a/src/lib.rs": '#[cfg(test)]\nmod tests {\n include!("cases.rs");\n}\ninclude!("shared.rs");\n', |
| 90 | "a/src/cases.rs": 'fn f() { Command::new("git"); }\n', |
| 91 | "a/src/shared.rs": 'fn f() {\n Command::new("git");\n}\n', |
| 92 | } |
| 93 | ) |
| 94 | self.assertEqual(hits, ["crates/a/src/shared.rs:2"]) |
| 95 | |
| 96 | def test_one_line_cfg_test_items_do_not_swallow_product_code(self) -> None: |
| 97 | for gate in ( |
| 98 | "#[cfg(test)] impl Foo for Bar {}", |
| 99 | "#[cfg(test)]\nimpl Foo for Bar {}", |
| 100 | "#[cfg(test)]\nuse std::process::Command as Unused;", |
| 101 | "#[cfg(test)]\nstatic TABLE: [u8; 4] = [0; 4];", |
| 102 | ): |
| 103 | hits = scan( |
| 104 | { |
| 105 | "a/src/lib.rs": f'mod m {{\n {gate}\n fn f() {{\n Command::new("git");\n }}\n}}\n' |
| 106 | } |
| 107 | ) |
| 108 | self.assertEqual(len(hits), 1, gate) |
| 109 | |
| 110 | def test_cfg_test_on_a_field_or_expression_does_not_swallow_product_code(self) -> None: |
| 111 | text = ( |
| 112 | "struct S {\n #[cfg(test)]\n seen: u8,\n}\n" |
| 113 | "fn make() -> S {\n S {\n #[cfg(test)]\n seen: 0,\n }\n}\n" |
| 114 | 'fn f() {\n Command::new("git");\n}\n' |
| 115 | ) |
| 116 | self.assertEqual(scan({"a/src/lib.rs": text}), ["crates/a/src/lib.rs:12"]) |
| 117 | |
| 118 | def test_cfg_test_block_ignores_braces_in_literals(self) -> None: |
| 119 | text = ( |
| 120 | '#[cfg(test)]\nmod tests {\n fn f() {\n let _ = "{";\n let _ = \'{\';\n // {\n Command::new("git");\n }\n}\n' |
| 121 | 'fn g() {\n Command::new("git");\n}\n' |
| 122 | ) |
| 123 | self.assertEqual(scan({"a/src/lib.rs": text}), ["crates/a/src/lib.rs:11"]) |
| 124 | |
| 125 | def test_cfg_not_test_is_product_code(self) -> None: |
| 126 | hits = scan( |
| 127 | {"a/src/lib.rs": '#[cfg(not(test))]\nfn f() {\n Command::new("git");\n}\n'} |
| 128 | ) |
| 129 | self.assertEqual(hits, ["crates/a/src/lib.rs:3"]) |
| 130 | |
| 131 | def test_renamed_command_import_fails(self) -> None: |
| 132 | hits = scan( |
| 133 | { |
| 134 | "a/src/lib.rs": 'use std::process::Command as Spawn;\nfn f() {\n Spawn::new("git");\n}\n', |
| 135 | "a/src/b.rs": 'use tokio::process::{Child, Command as Run};\nfn f() {\n Run::new("git.exe");\n}\n', |
| 136 | } |
| 137 | ) |
| 138 | self.assertEqual(hits, ["crates/a/src/b.rs:3", "crates/a/src/lib.rs:3"]) |
| 139 | |
| 140 | def test_string_literals_do_not_count_as_spawns(self) -> None: |
| 141 | hits = scan( |
| 142 | { |
| 143 | "a/src/lib.rs": 'const SCRIPT: &str = r#"Command::new("git")"#;\nfn f() { let _ = "Command::new(\\"git\\")"; }\n' |
| 144 | } |
| 145 | ) |
| 146 | self.assertEqual(hits, []) |
| 147 | |
| 148 | def test_comments_and_other_programs_pass(self) -> None: |
| 149 | hits = scan( |
| 150 | { |
| 151 | "a/src/lib.rs": '// Command::new("git")\nfn f() { Command::new("tar"); Command::new(path); }\n' |
| 152 | } |
| 153 | ) |
| 154 | self.assertEqual(hits, []) |
| 155 | |
| 156 | def test_allowlist_matches_only_its_exact_count(self) -> None: |
| 157 | files = {"a/src/lib.rs": 'fn f() {\n Command::new("git");\n}\n'} |
| 158 | allowlist = {"crates/a/src/lib.rs": (1, "reason")} |
| 159 | self.assertEqual(scan(files, allowlist), []) |
| 160 | files["a/src/lib.rs"] += 'fn g() {\n Command::new("git");\n}\n' |
| 161 | self.assertEqual(len(scan(files, allowlist)), 2) |
| 162 | |
| 163 | def test_repository_has_no_violations(self) -> None: |
| 164 | self.assertEqual(mod.find_violations(), []) |
| 165 | |
| 166 | |
| 167 | if __name__ == "__main__": |
| 168 | unittest.main() |
| 169 |