Enforce Rust code health gate
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
CHECKER = ROOT / "scripts" / "check-rust-code-health.sh"
|
||||
|
||||
|
||||
class RustCodeHealthCheckTests(unittest.TestCase):
|
||||
def run_checker(self, root: Path, max_lines: int = 20) -> subprocess.CompletedProcess[str]:
|
||||
env = os.environ.copy()
|
||||
env["CRANK_RUST_HEALTH_ROOT"] = str(root)
|
||||
env["CRANK_RUST_HEALTH_DEFAULT_MAX_LINES"] = str(max_lines)
|
||||
return subprocess.run(
|
||||
[str(CHECKER)],
|
||||
check=False,
|
||||
text=True,
|
||||
env=env,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
|
||||
def prepare_root(self, root: Path) -> None:
|
||||
(root / "apps" / "demo" / "src").mkdir(parents=True)
|
||||
(root / "crates" / "demo" / "src").mkdir(parents=True)
|
||||
|
||||
def test_passes_for_small_rust_files(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
self.prepare_root(root)
|
||||
(root / "crates" / "demo" / "src" / "lib.rs").write_text(
|
||||
"pub fn ok() -> bool {\n true\n}\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
result = self.run_checker(root)
|
||||
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn("checking file size limits", result.stdout)
|
||||
|
||||
def test_fails_for_new_large_rust_file(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
self.prepare_root(root)
|
||||
(root / "apps" / "demo" / "src" / "large.rs").write_text(
|
||||
"\n".join("// line" for _ in range(21)) + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
result = self.run_checker(root)
|
||||
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("apps/demo/src/large.rs has 21 lines", result.stderr)
|
||||
|
||||
def test_fails_for_large_inline_test_module(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
self.prepare_root(root)
|
||||
source = "\n".join(
|
||||
[
|
||||
"pub fn ok() -> bool { true }",
|
||||
"#[cfg(test)]",
|
||||
"mod tests {",
|
||||
" #[test]",
|
||||
" fn works() { assert!(super::ok()); }",
|
||||
"}",
|
||||
*["// test debt" for _ in range(8)],
|
||||
]
|
||||
)
|
||||
(root / "crates" / "demo" / "src" / "lib.rs").write_text(
|
||||
source + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
result = self.run_checker(root)
|
||||
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("contains inline tests", result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user