-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackfile_test.go
More file actions
92 lines (78 loc) · 2.3 KB
/
packfile_test.go
File metadata and controls
92 lines (78 loc) · 2.3 KB
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
package githttp
import (
"io/ioutil"
"os"
"testing"
git "github.com/libgit2/git2go/v33"
)
const (
packFilename = "testdata/repo.git/objects/pack/pack-3915156951f90b8239a1d1933cbe85ae1bc7457f.pack"
indexFilename = "testdata/repo.git/objects/pack/pack-3915156951f90b8239a1d1933cbe85ae1bc7457f.idx"
)
func testParsedIndex(t *testing.T, idx *PackfileIndex) {
if 3 != len(idx.Entries) {
t.Errorf("Expected 3 entries, got %v", len(idx.Entries))
}
// The entries in the index are sorted.
entries := []struct {
hash string
size uint64
objectType git.ObjectType
}{
{"417c01c8795a35b8e835113a85a5c0c1c77f67fb", 33, git.ObjectTree},
{"88aa3454adb27c3c343ab57564d962a0a7f6a3c1", 170, git.ObjectCommit},
{"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", 0, git.ObjectBlob},
}
for i, entry := range entries {
if entry.hash != idx.Entries[i].Oid.String() {
t.Errorf("Entry %d hash mismatch: expected %v, got %v", i, entry.hash, idx.Entries[i].Oid)
}
if entry.size != idx.Entries[i].Size {
t.Errorf("Entry %d size mismatch: expected %v, got %v", i, entry.size, idx.Entries[i].Size)
}
if entry.objectType != idx.Entries[i].Type {
t.Errorf("Entry %d type mismatch: expected %v, got %v", i, entry.objectType, idx.Entries[i].Type)
}
}
}
func TestParseIndex(t *testing.T) {
odb, err := git.NewOdb()
if err != nil {
t.Fatalf("Failed to create odb: %v", err)
}
defer odb.Free()
backend, err := git.NewOdbBackendOnePack(packFilename)
if err != nil {
t.Fatalf("Failed to create backend: %v", err)
}
if err := odb.AddAlternate(backend, 1); err != nil {
t.Fatalf("Failed to add backend: %v", err)
}
idx, err := ParseIndex(indexFilename, odb)
if err != nil {
t.Errorf("Failed to parse the index: %v", err)
}
testParsedIndex(t, idx)
}
func TestUnpackPackfile(t *testing.T) {
dir, err := ioutil.TempDir("", "packfile_test")
if err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
defer os.RemoveAll(dir)
odb, err := git.NewOdb()
if err != nil {
t.Fatalf("Failed to create odb: %v", err)
}
defer odb.Free()
f, err := os.Open(packFilename)
if err != nil {
t.Fatalf("Failed to open the index file: %v", err)
}
defer f.Close()
idx, _, err := UnpackPackfile(odb, f, dir, nil)
if err != nil {
t.Fatalf("Failed to unpack packfile: %v", err)
}
testParsedIndex(t, idx)
}