forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_test.go
164 lines (119 loc) · 3.58 KB
/
object_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package git
import (
"testing"
)
func TestObjectPoymorphism(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, treeId := seedTestRepo(t, repo)
var obj Object
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
obj = commit
if obj.Type() != ObjectCommit {
t.Fatalf("Wrong object type, expected commit, have %v", obj.Type())
}
commitTree, err := commit.Tree()
checkFatal(t, err)
commitTree.EntryCount()
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
obj = tree
if obj.Type() != ObjectTree {
t.Fatalf("Wrong object type, expected tree, have %v", obj.Type())
}
tree2, ok := obj.(*Tree)
if !ok {
t.Fatalf("Converting back to *Tree is not ok")
}
entry := tree2.EntryByName("README")
if entry == nil {
t.Fatalf("Tree did not have expected \"README\" entry")
}
if entry.Filemode != FilemodeBlob {
t.Fatal("Wrong filemode for \"README\"")
}
_, ok = obj.(*Commit)
if ok {
t.Fatalf("*Tree is somehow the same as *Commit")
}
obj, err = repo.Lookup(tree.Id())
checkFatal(t, err)
_, ok = obj.(*Tree)
if !ok {
t.Fatalf("Lookup creates the wrong type")
}
if obj.Type() != ObjectTree {
t.Fatalf("Type() doesn't agree with dynamic type")
}
obj, err = repo.RevparseSingle("HEAD")
checkFatal(t, err)
if obj.Type() != ObjectCommit || obj.Id().String() != commit.Id().String() {
t.Fatalf("Failed to parse the right revision")
}
obj, err = repo.RevparseSingle("HEAD^{tree}")
checkFatal(t, err)
if obj.Type() != ObjectTree || obj.Id().String() != tree.Id().String() {
t.Fatalf("Failed to parse the right revision")
}
}
func checkOwner(t *testing.T, repo *Repository, obj Object) {
owner := obj.Owner()
if owner == nil {
t.Fatal("bad owner")
}
if owner.ptr != repo.ptr {
t.Fatalf("bad owner, got %v expected %v\n", owner.ptr, repo.ptr)
}
}
func TestObjectOwner(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, treeId := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
checkOwner(t, repo, commit)
checkOwner(t, repo, tree)
}
func TestObjectPeel(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitID, treeID := seedTestRepo(t, repo)
var obj Object
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
obj, err = commit.Peel(ObjectAny)
checkFatal(t, err)
if obj.Type() != ObjectTree {
t.Fatalf("Wrong object type when peeling a commit, expected tree, have %v", obj.Type())
}
obj, err = commit.Peel(ObjectTag)
if !IsErrorCode(err, ErrInvalidSpec) {
t.Fatalf("Wrong error when peeling a commit to a tag, expected ErrInvalidSpec, have %v", err)
}
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
obj, err = tree.Peel(ObjectAny)
if !IsErrorCode(err, ErrInvalidSpec) {
t.Fatalf("Wrong error when peeling a tree, expected ErrInvalidSpec, have %v", err)
}
entry := tree.EntryByName("README")
blob, err := repo.LookupBlob(entry.Id)
checkFatal(t, err)
obj, err = blob.Peel(ObjectAny)
if !IsErrorCode(err, ErrInvalidSpec) {
t.Fatalf("Wrong error when peeling a blob, expected ErrInvalidSpec, have %v", err)
}
tagID := createTestTag(t, repo, commit)
tag, err := repo.LookupTag(tagID)
checkFatal(t, err)
obj, err = tag.Peel(ObjectAny)
checkFatal(t, err)
if obj.Type() != ObjectCommit {
t.Fatalf("Wrong object type when peeling a tag, expected commit, have %v", obj.Type())
}
// TODO: Should test a tag that annotates a different object than a commit
// but it's impossible at the moment to tag such an object.
}