-
Notifications
You must be signed in to change notification settings - Fork 315
/
tree_test.go
137 lines (112 loc) · 2.9 KB
/
tree_test.go
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
package git
import (
"errors"
"testing"
)
func TestTreeEntryById(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, treeID := seedTestRepo(t, repo)
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
id, err := NewOid("257cc5642cb1a054f08cc83f2d943e56fd3ebe99")
checkFatal(t, err)
entry := tree.EntryById(id)
if entry == nil {
t.Fatalf("entry id %v was not found", id)
}
}
func TestTreeBuilderInsert(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
subTree, err := repo.TreeBuilder()
if err != nil {
t.Fatalf("TreeBuilder: %v", err)
}
defer subTree.Free()
odb, err := repo.Odb()
if err != nil {
t.Fatalf("repo.Odb: %v", err)
}
blobId, err := odb.Write([]byte("hello"), ObjectBlob)
if err != nil {
t.Fatalf("odb.Write: %v", err)
}
if err = subTree.Insert("subfile", blobId, FilemodeBlobExecutable); err != nil {
t.Fatalf("TreeBuilder.Insert: %v", err)
}
treeID, err := subTree.Write()
if err != nil {
t.Fatalf("TreeBuilder.Write: %v", err)
}
tree, err := repo.LookupTree(treeID)
if err != nil {
t.Fatalf("LookupTree: %v", err)
}
entry, err := tree.EntryByPath("subfile")
if err != nil {
t.Fatalf("tree.EntryByPath(%q): %v", "subfile", err)
}
if !entry.Id.Equal(blobId) {
t.Fatalf("got oid %v, want %v", entry.Id, blobId)
}
}
func TestTreeWalk(t *testing.T) {
t.Parallel()
repo, err := OpenRepository("testdata/TestGitRepository.git")
checkFatal(t, err)
treeID, err := NewOid("6020a3b8d5d636e549ccbd0c53e2764684bb3125")
checkFatal(t, err)
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
var callCount int
err = tree.Walk(func(name string, entry *TreeEntry) error {
callCount++
return nil
})
checkFatal(t, err)
if callCount != 11 {
t.Fatalf("got called %v times, want %v", callCount, 11)
}
}
func TestTreeWalkSkip(t *testing.T) {
t.Parallel()
repo, err := OpenRepository("testdata/TestGitRepository.git")
checkFatal(t, err)
treeID, err := NewOid("6020a3b8d5d636e549ccbd0c53e2764684bb3125")
checkFatal(t, err)
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
var callCount int
err = tree.Walk(func(name string, entry *TreeEntry) error {
callCount++
return TreeWalkSkip
})
checkFatal(t, err)
if callCount != 4 {
t.Fatalf("got called %v times, want %v", callCount, 4)
}
}
func TestTreeWalkStop(t *testing.T) {
t.Parallel()
repo, err := OpenRepository("testdata/TestGitRepository.git")
checkFatal(t, err)
treeID, err := NewOid("6020a3b8d5d636e549ccbd0c53e2764684bb3125")
checkFatal(t, err)
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
var callCount int
stopError := errors.New("stop")
err = tree.Walk(func(name string, entry *TreeEntry) error {
callCount++
return stopError
})
if err != stopError {
t.Fatalf("got error %v, want %v", err, stopError)
}
if callCount != 1 {
t.Fatalf("got called %v times, want %v", callCount, 1)
}
}