(** Shared test fixtures. *) let rec remove_path path = try if Sys.is_directory path then ( Sys.readdir path |> Array.iter (fun name -> remove_path (Filename.concat path name)); Unix.rmdir path) else Sys.remove path with Sys_error _ -> () let with_temp_file prefix suffix test = let file = Filename.temp_file prefix suffix in Fun.protect ~finally:(fun () -> remove_path file) (fun () -> test file) let with_temp_directory prefix test = with_temp_file prefix "" (fun root -> Sys.remove root; Unix.mkdir root 0o755; test root) let with_environment name value test = let previous = Sys.getenv_opt name in Unix.putenv name value; Fun.protect ~finally:(fun () -> Unix.putenv name (Option.value previous ~default:"")) test let make_git_directory path = Unix.mkdir path 0o755; Out_channel.with_open_text (Filename.concat path "HEAD") (fun _ -> ()); Unix.mkdir (Filename.concat path "objects") 0o755 let git arguments = let command = "git" in let arguments = Array.of_list (command :: arguments) in let channel = Unix.open_process_args_in command arguments in let output = In_channel.input_all channel |> String.trim in match Unix.close_process_in channel with | Unix.WEXITED 0 -> output | Unix.WEXITED code -> Alcotest.failf "git exited with %d" code | Unix.WSIGNALED signal | Unix.WSTOPPED signal -> Alcotest.failf "git stopped by signal %d" signal let fail_config_error error = Alcotest.fail (Ogit.Config.show_load_error error)