Installs pre-commit hooks for OCaml projects that run dune fmt automatically
1(** Tests for precommit. *)
2
3let contains s sub =
4 let len = String.length sub in
5 let rec check i =
6 if i + len > String.length s then false
7 else if String.sub s i len = sub then true
8 else check (i + 1)
9 in
10 check 0
11
12let test_pre_commit_hook_valid () =
13 let hook = Precommit.pre_commit_hook in
14 Alcotest.(check bool) "is shell script" true (String.sub hook 0 2 = "#!");
15 Alcotest.(check bool) "runs dune fmt" true (contains hook "dune fmt");
16 Alcotest.(check bool)
17 "checks staged files" true
18 (contains hook "git diff --cached")
19
20let test_commit_msg_hook_valid () =
21 let hook = Precommit.commit_msg_hook in
22 Alcotest.(check bool) "is shell script" true (String.sub hook 0 2 = "#!");
23 Alcotest.(check bool) "filters AI lines" true (contains hook "Co-Authored-By");
24 Alcotest.(check bool) "checks emojis" true (contains hook "emoji")
25
26let suite =
27 ( "precommit",
28 [
29 Alcotest.test_case "pre_commit_hook valid" `Quick
30 test_pre_commit_hook_valid;
31 Alcotest.test_case "commit_msg_hook valid" `Quick
32 test_commit_msg_hook_valid;
33 ] )