aboutsummaryrefslogtreecommitdiff
path: root/object/store/loose/roundtrip_test.go
blob: e99da0f2466c0536963ff31b259a1feb6f7b3507 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package loose_test

import (
	"bytes"
	"io"
	"testing"

	"lindenii.org/go/furgit/internal/testgit"
	"lindenii.org/go/furgit/object/header"
	"lindenii.org/go/furgit/object/id"
	"lindenii.org/go/furgit/object/typ"
)

func TestRoundTrip(t *testing.T) {
	t.Parallel()

	cases := []struct {
		name    string
		ty      typ.Type
		content []byte
	}{
		{name: "blob", ty: typ.TypeBlob, content: []byte("roundtrip blob\n")},
		{name: "empty blob", ty: typ.TypeBlob, content: []byte{}},
		{name: "tree", ty: typ.TypeTree, content: []byte("roundtrip tree bytes")},
		{name: "commit", ty: typ.TypeCommit, content: []byte("roundtrip commit bytes")},
		{name: "tag", ty: typ.TypeTag, content: []byte("roundtrip tag bytes")},
	}

	for _, objectFormat := range id.SupportedObjectFormats() {
		t.Run(objectFormat.String(), func(t *testing.T) {
			t.Parallel()

			repo, err := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectFormat})
			if err != nil {
				t.Fatalf("NewRepo: %v", err)
			}

			looseStore := openLooseStore(t, repo)

			for _, tc := range cases {
				t.Run(tc.name, func(t *testing.T) {
					wantRaw := header.Append(nil, tc.ty, uint64(len(tc.content)))
					wantRaw = append(wantRaw, tc.content...)

					objectID, err := looseStore.WriteBytesContent(tc.ty, tc.content)
					if err != nil {
						t.Fatalf("WriteBytesContent: %v", err)
					}

					gotRaw, err := looseStore.ReadBytesFull(objectID)
					if err != nil {
						t.Fatalf("ReadBytesFull: %v", err)
					}

					if !bytes.Equal(gotRaw, wantRaw) {
						t.Fatalf("ReadBytesFull mismatch")
					}

					gotType, gotBody, err := looseStore.ReadBytesContent(objectID)
					if err != nil {
						t.Fatalf("ReadBytesContent: %v", err)
					}

					if gotType != tc.ty {
						t.Fatalf("ReadBytesContent type = %v, want %v", gotType, tc.ty)
					}

					if !bytes.Equal(gotBody, tc.content) {
						t.Fatalf("ReadBytesContent body mismatch")
					}

					headType, headSize, err := looseStore.ReadHeader(objectID)
					if err != nil {
						t.Fatalf("ReadHeader: %v", err)
					}

					if headType != tc.ty {
						t.Fatalf("ReadHeader type = %v, want %v", headType, tc.ty)
					}

					if headSize != uint64(len(tc.content)) {
						t.Fatalf("ReadHeader size = %d, want %d", headSize, len(tc.content))
					}

					fullReader, err := looseStore.ReadReaderFull(objectID)
					if err != nil {
						t.Fatalf("ReadReaderFull: %v", err)
					}

					gotFull, err := io.ReadAll(fullReader)
					if err != nil {
						_ = fullReader.Close()

						t.Fatalf("ReadReaderFull ReadAll: %v", err)
					}

					err = fullReader.Close()
					if err != nil {
						t.Fatalf("ReadReaderFull Close: %v", err)
					}

					if !bytes.Equal(gotFull, wantRaw) {
						t.Fatalf("ReadReaderFull mismatch")
					}

					contentType, contentSize, contentReader, err := looseStore.ReadReaderContent(objectID)
					if err != nil {
						t.Fatalf("ReadReaderContent: %v", err)
					}

					gotContent, err := io.ReadAll(contentReader)
					if err != nil {
						_ = contentReader.Close()

						t.Fatalf("ReadReaderContent ReadAll: %v", err)
					}

					err = contentReader.Close()
					if err != nil {
						t.Fatalf("ReadReaderContent Close: %v", err)
					}

					if contentType != tc.ty {
						t.Fatalf("ReadReaderContent type = %v, want %v", contentType, tc.ty)
					}

					if contentSize != uint64(len(tc.content)) {
						t.Fatalf("ReadReaderContent size = %d, want %d", contentSize, len(tc.content))
					}

					if !bytes.Equal(gotContent, tc.content) {
						t.Fatalf("ReadReaderContent mismatch")
					}
				})
			}
		})
	}
}