[OCaml] Mobile-friendly clone of cgit.
1
(** The examination cap on filtered history walks: that a walk stopped by the
2
cap reports truncation, and an exhausted history does not. *)
3
4
open Test_helpers
5
6
let with_history_repository test =
7
with_temp_directory "ogit-walk" (fun root ->
8
let name = "project" in
9
let path = Filename.concat root name in
10
Unix.mkdir path 0o755;
11
ignore (git [ "-C"; path; "init"; "-q"; "-b"; "main" ]);
12
ignore (git [ "-C"; path; "config"; "user.name"; "Test" ]);
13
ignore (git [ "-C"; path; "config"; "user.email"; "t@t.invalid" ]);
14
for index = 1 to 8 do
15
Out_channel.with_open_text (Filename.concat path "f.txt") (fun ch ->
16
Printf.fprintf ch "revision %d\n" index);
17
ignore (git [ "-C"; path; "add"; "." ]);
18
ignore
19
(git [ "-C"; path; "commit"; "-q"; "-m"; Printf.sprintf "c%d" index ])
20
done;
21
let config = Ogit.Config.{ default with git_project_root = root } in
22
let repository =
23
match Lwt_main.run (Ogit.Resolvers.open_repository config name) with
24
| Ok r -> r
25
| Error e -> Alcotest.failf "%a" Ogit.Resolvers.pp_error e
26
in
27
Fun.protect
28
~finally:(fun () ->
29
Lwt_main.run (Ogit.Resolvers.close_repository repository))
30
(fun () -> test repository))
31
32
let recent_matching ?max_examined repository count predicate =
33
match
34
Lwt_main.run
35
(Ogit.Resolvers.Commit.recent_matching ?max_examined repository count
36
predicate)
37
with
38
| Ok result -> result
39
| Error e -> Alcotest.failf "%a" Ogit.Resolvers.pp_error e
40
41
let never_matches _ = false
42
43
let test_cap_reports_truncation () =
44
with_history_repository (fun repository ->
45
let commits, truncated =
46
recent_matching ~max_examined:3 repository 10 never_matches
47
in
48
Alcotest.(check int) "no matches" 0 (List.length commits);
49
Alcotest.(check bool) "truncated" true truncated)
50
51
let test_exhausted_history_is_complete () =
52
with_history_repository (fun repository ->
53
let commits, truncated =
54
recent_matching ~max_examined:100 repository 10 never_matches
55
in
56
Alcotest.(check int) "no matches" 0 (List.length commits);
57
Alcotest.(check bool) "not truncated" false truncated)
58
59
let test_cap_keeps_collected_matches () =
60
with_history_repository (fun repository ->
61
(* Every commit matches; the cap stops the walk after three of eight. *)
62
let commits, truncated =
63
recent_matching ~max_examined:3 repository 10 (fun _ -> true)
64
in
65
Alcotest.(check int) "three collected" 3 (List.length commits);
66
Alcotest.(check bool) "truncated" true truncated)
67
68
let suite =
69
( "commit walk",
70
[
71
Alcotest.test_case "cap reports truncation" `Slow
72
test_cap_reports_truncation;
73
Alcotest.test_case "exhausted history is complete" `Slow
74
test_exhausted_history_is_complete;
75
Alcotest.test_case "cap keeps collected matches" `Slow
76
test_cap_keeps_collected_matches;
77
] )
78