View raw

1 (** Shared test fixtures. *) 2 3 let rec remove_path path = 4 try 5 if Sys.is_directory path then ( 6 Sys.readdir path 7 |> Array.iter (fun name -> remove_path (Filename.concat path name)); 8 Unix.rmdir path) 9 else Sys.remove path 10 with Sys_error _ -> () 11 12 let with_temp_file prefix suffix test = 13 let file = Filename.temp_file prefix suffix in 14 Fun.protect ~finally:(fun () -> remove_path file) (fun () -> test file) 15 16 let with_temp_directory prefix test = 17 with_temp_file prefix "" (fun root -> 18 Sys.remove root; 19 Unix.mkdir root 0o755; 20 test root) 21 22 let with_environment name value test = 23 let previous = Sys.getenv_opt name in 24 Unix.putenv name value; 25 Fun.protect 26 ~finally:(fun () -> Unix.putenv name (Option.value previous ~default:"")) 27 test 28 29 let make_git_directory path = 30 Unix.mkdir path 0o755; 31 Out_channel.with_open_text (Filename.concat path "HEAD") (fun _ -> ()); 32 Unix.mkdir (Filename.concat path "objects") 0o755 33 34 let git arguments = 35 let command = "git" in 36 let arguments = Array.of_list (command :: arguments) in 37 let channel = Unix.open_process_args_in command arguments in 38 let output = In_channel.input_all channel |> String.trim in 39 match Unix.close_process_in channel with 40 | Unix.WEXITED 0 -> output 41 | Unix.WEXITED code -> Alcotest.failf "git exited with %d" code 42 | Unix.WSIGNALED signal | Unix.WSTOPPED signal -> 43 Alcotest.failf "git stopped by signal %d" signal 44 45 let fail_config_error error = Alcotest.fail (Ogit.Config.show_load_error error) 46