Persistent store with Git semantics: lazy reads, delayed writes, content-addressing
at main 60 lines 2.4 kB view raw
1open Irmin 2 3let rec rm_rf path = 4 if Eio.Path.is_directory path then begin 5 List.iter 6 (fun name -> rm_rf Eio.Path.(path / name)) 7 (Eio.Path.read_dir path); 8 Eio.Path.rmdir path 9 end 10 else if Eio.Path.is_file path then Eio.Path.unlink path 11 12let with_temp_dir f = 13 Eio_main.run @@ fun env -> 14 let fs = Eio.Stdenv.fs env in 15 let cwd = Eio.Stdenv.cwd env in 16 Eio.Switch.run @@ fun sw -> 17 let tmp_name = Printf.sprintf "irmin-git-test-%d" (Random.int 100000) in 18 let tmp_path = Eio.Path.(cwd / tmp_name) in 19 Eio.Path.mkdirs ~exists_ok:true ~perm:0o755 tmp_path; 20 Fun.protect ~finally:(fun () -> rm_rf tmp_path) (fun () -> f ~sw ~fs tmp_path) 21 22let test_init_git () = 23 with_temp_dir @@ fun ~sw ~fs tmp_path -> 24 let fpath = Fpath.v (Eio.Path.native_exn tmp_path) in 25 let _store = Git_interop.init_git ~sw ~fs ~path:fpath in 26 (* Verify .git directory was created *) 27 let git_dir = Eio.Path.(tmp_path / ".git") in 28 Alcotest.(check bool) "git dir exists" true (Eio.Path.is_directory git_dir) 29 30let test_write_read_object () = 31 with_temp_dir @@ fun ~sw ~fs tmp_path -> 32 let fpath = Fpath.v (Eio.Path.native_exn tmp_path) in 33 let _store = Git_interop.init_git ~sw ~fs ~path:fpath in 34 let git_dir = Fpath.(fpath / ".git") in 35 let data = "hello world" in 36 let hash = Git_interop.write_object ~sw ~fs ~git_dir ~typ:"blob" data in 37 match Git_interop.read_object ~sw ~fs ~git_dir hash with 38 | Ok (typ, content) -> 39 Alcotest.(check string) "type" "blob" typ; 40 Alcotest.(check string) "content" data content 41 | Error (`Msg msg) -> Alcotest.fail msg 42 43let test_write_read_ref () = 44 with_temp_dir @@ fun ~sw ~fs tmp_path -> 45 let fpath = Fpath.v (Eio.Path.native_exn tmp_path) in 46 let _store = Git_interop.init_git ~sw ~fs ~path:fpath in 47 let git_dir = Fpath.(fpath / ".git") in 48 let hash = Git_interop.write_object ~sw ~fs ~git_dir ~typ:"blob" "content" in 49 Git_interop.write_ref ~sw ~fs ~git_dir "refs/heads/test" hash; 50 match Git_interop.read_ref ~sw ~fs ~git_dir "refs/heads/test" with 51 | Some h -> Alcotest.(check bool) "ref matches" true (Hash.equal hash h) 52 | None -> Alcotest.fail "ref not found" 53 54let suite = 55 ( "Git_interop", 56 [ 57 Alcotest.test_case "init git" `Quick test_init_git; 58 Alcotest.test_case "write/read object" `Quick test_write_read_object; 59 Alcotest.test_case "write/read ref" `Quick test_write_read_ref; 60 ] )