Git object storage and pack files for Eio
1(* Copyright (c) 2024-2026 Thomas Gazagnaire <thomas@gazagnaire.org>
2
3 Permission to use, copy, modify, and distribute this software for any
4 purpose with or without fee is hereby granted, provided that the above
5 copyright notice and this permission notice appear in all copies.
6
7 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *)
14
15let hash = Test_helpers.hash
16
17let test_roundtrip () =
18 let content = "test content" in
19 let blob = Git.Blob.of_string content in
20 let value = Git.Value.blob blob in
21 let s = Git.Value.to_string value in
22 match Git.Value.of_string_with_header s with
23 | Ok value' -> (
24 match value' with
25 | Git.Value.Blob b ->
26 Alcotest.(check string) "content" content (Git.Blob.to_string b)
27 | _ -> Alcotest.fail "expected blob")
28 | Error (`Msg m) -> Alcotest.fail m
29
30let test_bytesrw_roundtrip () =
31 let content = "bytesrw test content" in
32 let blob = Git.Blob.of_string content in
33 let value = Git.Value.blob blob in
34 (* Write to buffer *)
35 let buf = Buffer.create 64 in
36 let writer = Bytesrw.Bytes.Writer.of_buffer buf in
37 Git.Value.write writer value;
38 let written = Buffer.contents buf in
39 (* Read back *)
40 let reader = Bytesrw.Bytes.Reader.of_string written in
41 match Git.Value.read reader with
42 | Ok value' -> (
43 match value' with
44 | Git.Value.Blob b ->
45 Alcotest.(check string) "content" content (Git.Blob.to_string b)
46 | _ -> Alcotest.fail "expected blob")
47 | Error (`Msg m) -> Alcotest.fail m
48
49let test_bytesrw_commit () =
50 let tree_hash = Git.Hash.of_hex "da39a3ee5e6b4b0d3255bfef95601890afd80709" in
51 let author =
52 Git.User.v ~name:"Alice" ~email:"alice@example.com" ~date:1234567890L ()
53 in
54 let commit =
55 Git.Commit.v ~tree:tree_hash ~author ~committer:author
56 (Some "Test commit\n")
57 in
58 let value = Git.Value.commit commit in
59 (* Write to buffer *)
60 let buf = Buffer.create 256 in
61 let writer = Bytesrw.Bytes.Writer.of_buffer buf in
62 Git.Value.write writer value;
63 let written = Buffer.contents buf in
64 (* Read back *)
65 let reader = Bytesrw.Bytes.Reader.of_string written in
66 match Git.Value.read reader with
67 | Ok value' -> (
68 match value' with
69 | Git.Value.Commit c ->
70 Alcotest.(check hash) "tree" tree_hash (Git.Commit.tree c);
71 Alcotest.(check (option string))
72 "message" (Some "Test commit\n") (Git.Commit.message c)
73 | _ -> Alcotest.fail "expected commit")
74 | Error (`Msg m) -> Alcotest.fail m
75
76let tests =
77 [
78 Alcotest.test_case "roundtrip" `Quick test_roundtrip;
79 Alcotest.test_case "bytesrw_roundtrip" `Quick test_bytesrw_roundtrip;
80 Alcotest.test_case "bytesrw_commit" `Quick test_bytesrw_commit;
81 ]
82
83let suite = ("value", tests)