[OCaml] Mobile-friendly clone of cgit.
1
(** Repository discovery: bare and non-bare layouts, descriptions, and directory
2
listings. *)
3
4
open Test_helpers
5
6
let test_description_missing () =
7
with_temp_file "ogit-description" ".txt" (fun file ->
8
Sys.remove file;
9
Alcotest.(check string)
10
"missing file" Ogit.Resolvers.default_repo_description
11
(Ogit.Resolvers.read_description_file file))
12
13
let test_description_empty () =
14
with_temp_file "ogit-description" ".txt" (fun file ->
15
Out_channel.with_open_text file (fun _ -> ());
16
Alcotest.(check string)
17
"empty file" Ogit.Resolvers.default_repo_description
18
(Ogit.Resolvers.read_description_file file))
19
20
let test_description_content () =
21
with_temp_file "ogit-description" ".txt" (fun file ->
22
Out_channel.with_open_text file (fun channel ->
23
output_string channel "A useful repository\n");
24
Alcotest.(check string)
25
"content" "A useful repository"
26
(Ogit.Resolvers.read_description_file file))
27
28
let test_layout_bare () =
29
with_temp_directory "ogit-repos" (fun root ->
30
let bare = Filename.concat root "bare.git" in
31
make_git_directory bare;
32
Alcotest.(check bool)
33
"is_repository" true
34
(Ogit.Resolvers.is_repository bare);
35
match Ogit.Resolvers.repository_layout_or_none bare with
36
| Some { worktree; git_dir } ->
37
Alcotest.(check string) "worktree" bare worktree;
38
Alcotest.(check string) "git_dir" bare git_dir
39
| None -> Alcotest.fail "expected bare layout")
40
41
let test_layout_non_bare () =
42
with_temp_directory "ogit-repos" (fun root ->
43
let clone = Filename.concat root "clone" in
44
Unix.mkdir clone 0o755;
45
make_git_directory (Filename.concat clone ".git");
46
Alcotest.(check bool)
47
"is_repository" true
48
(Ogit.Resolvers.is_repository clone);
49
match Ogit.Resolvers.repository_layout_or_none clone with
50
| Some { worktree; git_dir } ->
51
Alcotest.(check string) "worktree" clone worktree;
52
Alcotest.(check string)
53
"git_dir"
54
(Filename.concat clone ".git")
55
git_dir
56
| None -> Alcotest.fail "expected non-bare layout")
57
58
let test_layout_not_a_repo () =
59
with_temp_directory "ogit-repos" (fun root ->
60
let directory = Filename.concat root "not-a-repository" in
61
Unix.mkdir directory 0o755;
62
Alcotest.(check bool)
63
"not a repository" false
64
(Ogit.Resolvers.is_repository directory))
65
66
let test_layout_non_bare_with_shadowing_files () =
67
with_temp_directory "ogit-repos" (fun root ->
68
let repo = Filename.concat root "TAOH" in
69
Unix.mkdir repo 0o755;
70
make_git_directory (Filename.concat repo ".git");
71
(* Create HEAD file and objects/ directory in the worktree that could
72
confuse bare-repository detection. *)
73
Out_channel.with_open_text (Filename.concat repo "HEAD") (fun ch ->
74
output_string ch "ref: refs/heads/master\n");
75
Unix.mkdir (Filename.concat repo "objects") 0o755;
76
Alcotest.(check bool)
77
"is_repository" true
78
(Ogit.Resolvers.is_repository repo);
79
match Ogit.Resolvers.repository_layout_or_none repo with
80
| Some { worktree; git_dir } ->
81
Alcotest.(check string) "worktree" repo worktree;
82
Alcotest.(check string)
83
"git_dir uses .git"
84
(Filename.concat repo ".git")
85
git_dir
86
| None -> Alcotest.fail "expected non-bare layout")
87
88
let test_listing () =
89
with_temp_directory "ogit-listing" (fun root ->
90
make_git_directory (Filename.concat root "visible.git");
91
make_git_directory (Filename.concat root ".hidden.git");
92
let config = Ogit.Config.{ default with git_project_root = root } in
93
match Ogit.Resolvers.repositories config with
94
| Ok repositories ->
95
Alcotest.(check (list string))
96
"visible only" [ "visible.git" ] repositories
97
| Error error -> Alcotest.failf "%a" Ogit.Resolvers.pp_error error)
98
99
let test_fallback_branches () =
100
let config = Ogit.Config.{ default with default_branch = "trunk" } in
101
Alcotest.(check (list string))
102
"trunk first"
103
[ "trunk"; "main"; "master" ]
104
(Ogit.Resolvers.fallback_branch_candidates config);
105
let main = Ogit.Config.{ config with default_branch = "main" } in
106
Alcotest.(check (list string))
107
"main deduped" [ "main"; "master" ]
108
(Ogit.Resolvers.fallback_branch_candidates main)
109
110
let suite =
111
( "repository discovery",
112
[
113
Alcotest.test_case "description missing" `Quick test_description_missing;
114
Alcotest.test_case "description empty" `Quick test_description_empty;
115
Alcotest.test_case "description content" `Quick test_description_content;
116
Alcotest.test_case "bare layout" `Quick test_layout_bare;
117
Alcotest.test_case "non-bare layout" `Quick test_layout_non_bare;
118
Alcotest.test_case "non-bare with shadowing files" `Quick
119
test_layout_non_bare_with_shadowing_files;
120
Alcotest.test_case "not a repository" `Quick test_layout_not_a_repo;
121
Alcotest.test_case "listing" `Quick test_listing;
122
Alcotest.test_case "fallback branches" `Quick test_fallback_branches;
123
] )
124