(** The examination cap on filtered history walks: that a walk stopped by the cap reports truncation, and an exhausted history does not. *) open Test_helpers let with_history_repository test = with_temp_directory "ogit-walk" (fun root -> let name = "project" in let path = Filename.concat root name in Unix.mkdir path 0o755; ignore (git [ "-C"; path; "init"; "-q"; "-b"; "main" ]); ignore (git [ "-C"; path; "config"; "user.name"; "Test" ]); ignore (git [ "-C"; path; "config"; "user.email"; "t@t.invalid" ]); for index = 1 to 8 do Out_channel.with_open_text (Filename.concat path "f.txt") (fun ch -> Printf.fprintf ch "revision %d\n" index); ignore (git [ "-C"; path; "add"; "." ]); ignore (git [ "-C"; path; "commit"; "-q"; "-m"; Printf.sprintf "c%d" index ]) done; let config = Ogit.Config.{ default with git_project_root = root } in let repository = match Lwt_main.run (Ogit.Resolvers.open_repository config name) with | Ok r -> r | Error e -> Alcotest.failf "%a" Ogit.Resolvers.pp_error e in Fun.protect ~finally:(fun () -> Lwt_main.run (Ogit.Resolvers.close_repository repository)) (fun () -> test repository)) let recent_matching ?max_examined repository count predicate = match Lwt_main.run (Ogit.Resolvers.Commit.recent_matching ?max_examined repository count predicate) with | Ok result -> result | Error e -> Alcotest.failf "%a" Ogit.Resolvers.pp_error e let never_matches _ = false let test_cap_reports_truncation () = with_history_repository (fun repository -> let commits, truncated = recent_matching ~max_examined:3 repository 10 never_matches in Alcotest.(check int) "no matches" 0 (List.length commits); Alcotest.(check bool) "truncated" true truncated) let test_exhausted_history_is_complete () = with_history_repository (fun repository -> let commits, truncated = recent_matching ~max_examined:100 repository 10 never_matches in Alcotest.(check int) "no matches" 0 (List.length commits); Alcotest.(check bool) "not truncated" false truncated) let test_cap_keeps_collected_matches () = with_history_repository (fun repository -> (* Every commit matches; the cap stops the walk after three of eight. *) let commits, truncated = recent_matching ~max_examined:3 repository 10 (fun _ -> true) in Alcotest.(check int) "three collected" 3 (List.length commits); Alcotest.(check bool) "truncated" true truncated) let suite = ( "commit walk", [ Alcotest.test_case "cap reports truncation" `Slow test_cap_reports_truncation; Alcotest.test_case "exhausted history is complete" `Slow test_exhausted_history_is_complete; Alcotest.test_case "cap keeps collected matches" `Slow test_cap_keeps_collected_matches; ] )