Repositories / gitweb2.git
test/test_gitweb2.ml
Clone (read-only): git clone http://git.guha-anderson.com/git/gitweb2.git
let work_root : string = ".build/test-work"
let repo_root : string = work_root ^ "/repo"
let repo_name : string = "repo"
let sh (command : string) : unit =
match Sys.command command with
| 0 -> ()
| code -> Alcotest.failf "command failed with %d: %s" code command
let reset_repo () : unit =
sh ("rm -rf " ^ work_root ^ " && mkdir -p " ^ repo_root);
sh ("git -C " ^ repo_root ^ " init -b main >/dev/null 2>&1");
sh ("git -C " ^ repo_root ^ " config user.name 'Fixture Author'");
sh ("git -C " ^ repo_root ^ " config user.email fixture@example.com");
sh ("printf 'plugins { }\\n' > " ^ repo_root ^ "/build.gradle.kts");
sh ("printf '# gitweb2\\n\\nA Kotlin/Native, read-only web viewer for local Git repositories.\\n' > " ^ repo_root ^ "/README.md");
sh ("printf 'pluginManagement { }\\n' > " ^ repo_root ^ "/settings.gradle.kts");
sh ("git -C " ^ repo_root ^ " add . && git -C " ^ repo_root ^ " commit -m 'Initial fixture commit' >/dev/null 2>&1")
let contains (haystack : string) (needle : string) : bool =
let hlen = String.length haystack and nlen = String.length needle in
let rec loop index =
index + nlen <= hlen && (String.sub haystack index nlen = needle || loop (index + 1))
in
nlen = 0 || loop 0
let assert_contains (label : string) (body : string) (needle : string) : unit = Alcotest.(check bool) label true (contains body needle)
let directory_entries_stay_alphabetical () : unit =
reset_repo ();
let response = Gitweb2.route ~root:work_root ("/repo/" ^ repo_name) in
Alcotest.(check int) "status" 200 response.status;
let index needle = try String.index_from response.body 0 needle.[0] with _ -> -1 in
let find needle =
let rec loop index =
if index + String.length needle > String.length response.body then -1
else if String.sub response.body index (String.length needle) = needle then index
else loop (index + 1)
in
loop 0
in
let build_index = find ">build.gradle.kts</a>" in
let settings_index = find ">settings.gradle.kts</a>" in
let readme_index = find ">README.md</a>" in
ignore index;
Alcotest.(check bool) "build present" true (build_index >= 0);
Alcotest.(check bool) "settings present" true (settings_index >= 0);
Alcotest.(check bool) "readme present" true (readme_index >= 0);
Alcotest.(check bool) "readme after build" true (readme_index > build_index);
Alcotest.(check bool) "readme before settings" true (readme_index < settings_index)
let directories_render_their_readme_below_the_listing () : unit =
reset_repo ();
let response = Gitweb2.route ~pygments_command:"uv run --with pygments pygmentize" ~root:work_root ("/repo/" ^ repo_name) in
Alcotest.(check int) "status" 200 response.status;
let find needle =
let rec loop index =
if index + String.length needle > String.length response.body then -1
else if String.sub response.body index (String.length needle) = needle then index
else loop (index + 1)
in
loop 0
in
let listing_index = find ">README.md</a>" in
let rendered_index = find "<h2>README.md</h2>" in
Alcotest.(check bool) "listing present" true (listing_index >= 0);
Alcotest.(check bool) "rendered present" true (rendered_index >= 0);
Alcotest.(check bool) "below listing" true (rendered_index > listing_index);
assert_contains "highlight" response.body "<div class=\"highlight\">";
assert_contains "name" response.body "gitweb2";
assert_contains "readme text" response.body "read-only web viewer"
let pygments_fallback () : unit =
let html = Gitweb2.file_render ~pygments_command:"__missing_pygmentize__" ~file_path:"README.md" "<plain>" in
Alcotest.(check string) "fallback" "<pre><code><plain></code></pre>" html
let () : unit =
Alcotest.run "gitweb2"
[
( "routing",
[
Alcotest.test_case "directory entries stay alphabetical" `Quick directory_entries_stay_alphabetical;
Alcotest.test_case "directories render their readme below the listing" `Quick directories_render_their_readme_below_the_listing;
Alcotest.test_case "pygments fallback" `Quick pygments_fallback;
] );
]