-
Notifications
You must be signed in to change notification settings - Fork 7
/
git_test.go
315 lines (284 loc) · 5.95 KB
/
git_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package git
import (
"errors"
"fmt"
"os"
"os/exec"
"testing"
)
var (
repo *Repo
revwalk *RevWalk
tree *Tree
path string
author string
ref = new(Reference)
)
func init() {
path = "./tmp"
author = "foot@bar.com"
}
// Repo
func TestInitBare(t *testing.T) {
repo = new(Repo)
if err := repo.Init(path, 1); err != nil {
t.Fatal("Error:", err)
}
}
func TestOpenBare(t *testing.T) {
defer os.RemoveAll(path)
defer repo.Free()
err := repo.Open(path)
if err != nil {
t.Fatal("Error:", err)
}
}
func TestInitNotBare(t *testing.T) {
repo = new(Repo)
if err := repo.Init(path, NOTBARE); err != nil {
t.Fatal("Error:", err)
}
}
func TestOpenNotBare(t *testing.T) {
err := repo.Open(path + "/.git")
checkFatal(t, err)
}
//FIXME: figure out how to seed an intial HEAD
func TestSeed(t *testing.T) {
tmpfile := "README"
f, err := os.Create(path + "/" + tmpfile)
_, err = f.WriteString("foo\n")
f.Close()
checkFatal(t, err)
err = run([]string{"git", "add", tmpfile})
checkFatal(t, err)
err = run([]string{"git", "commit", "-m", "test"})
checkFatal(t, err)
}
// Index
func TestIndexAdd(t *testing.T) {
index, err := NewIndex(repo)
if err != nil {
t.Error(err)
}
defer index.Free()
check(t, err)
tmpfile := "README"
f, err := os.OpenFile(path+"/"+tmpfile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
check(t, err)
_, err = f.WriteString("foo\n")
f.Close()
err = index.Add(tmpfile)
check(t, err)
err = index.Write()
check(t, err)
}
func TestIndexEntryCount(t *testing.T) {
expected := 1
index, err := NewIndex(repo)
if err != nil {
t.Error(err)
}
defer index.Free()
check(t, err)
err = index.Read()
check(t, err)
if index.EntryCount() != expected {
t.Fatalf("Expected 1 Entry count in the index, but there were %v", index.EntryCount())
}
}
func TestIndexGet(t *testing.T) {
path := "README"
flags := 6
index, err := NewIndex(repo)
if err != nil {
t.Error(err)
}
defer index.Free()
check(t, err)
err = index.Read()
check(t, err)
entry, err := index.Get(0)
if err != nil {
t.Fatal(err)
}
if entry.Path() != path {
t.Errorf("Expected Entry Path %v, got %v", path, index.EntryCount())
}
if entry.Flags() != flags {
t.Errorf("Expected Entry Flags %v, got %v", flags, index.EntryCount())
}
}
/*
// Commit
func TestCommit(t *testing.T) {
TestIndexAdd(t)
index, err := NewIndex(repo)
if err != nil {
t.Error(err)
}
defer index.Free()
check(t, err)
tree, err := TreeFromIndex(repo, &index)
check(t, err)
head, err := GetHead(repo)
check(t, err)
s := NewSignature("Foo Bar", "foo@bar.com")
defer s.Free()
err = CommitCreate(repo, tree, head, s, s, "some stuff here")
check(t, err)
}
func TestManyCommits(t *testing.T) {
for i := 0; i < 29; i++ {
TestCommit(t)
}
}
*/
// RevWalk
func TestNewRevWalk(t *testing.T) {
var err error
revwalk, err = NewRevWalk(repo)
if err != nil {
t.Fatal("Error:", err)
}
}
func TestRevWalkNext(t *testing.T) {
head, _ := GetHeadString(repo)
o, _ := NewOidString(head)
revwalk.Push(o)
if err := revwalk.Next(o); err != nil {
t.Fatal("Error:", err)
}
if o.String() != head {
t.Errorf("oid string should match %v is %v", head, o.String())
}
}
func TestRevWalk(t *testing.T) {
o := NewOid()
for {
if err := revwalk.Next(o); err != nil {
break
}
c := new(Commit)
c.Lookup(repo, o)
// Output example
fmt.Printf("%v %v %v %v\n", o.String(), c.Author(), c.Email(), c.Msg())
}
}
// Oid
func TestNewOid(t *testing.T) {
head, err := GetHeadString(repo)
check(t, err)
if _, err := NewOidString(head); err != nil {
t.Error(err)
}
}
// Singature
func TestSignature(t *testing.T) {
NewSignature("foo", "bar")
}
func TestTSignature(t *testing.T) {
}
// Tree
func TestTreeFromIndex(t *testing.T) {
index, err := NewIndex(repo)
if err != nil {
t.Error(err)
}
defer index.Free()
check(t, err)
_, err = TreeFromIndex(repo, index)
check(t, err)
}
func TestTreeLookup(t *testing.T) {
index, err := NewIndex(repo)
if err != nil {
t.Error(err)
}
defer index.Free()
check(t, err)
oid, err := TreeFromIndex(repo, index)
check(t, err)
tree, err := TreeLookup(repo, oid)
check(t, err)
tree.Free()
}
func TestTreeFromCommit(t *testing.T) {
head, err := GetHead(repo)
check(t, err)
commit := new(Commit)
err = commit.Lookup(repo, head)
check(t, err)
tree, err = TreeFromCommit(repo, commit)
check(t, err)
}
func TestTreeEntryByName(t *testing.T) {
expected := "README"
entry, err := tree.EntryByName(expected)
if err != nil {
t.Fatal("Expected to find a file, but was unable to.")
}
if entry.Filename() != expected {
t.Fatal("EntryByName did not return the proper file. Expected %v, got %v",
expected,
entry.Filename())
}
}
func TestInvalidTreeEntryByName(t *testing.T) {
expected := "README.does-not-exist"
_, err := tree.EntryByName(expected)
if err == nil {
t.Fatal("Was expecting a does not exist error, but did not recieve one.")
}
}
func TestTreeEntryByIndex(t *testing.T) {
expected := "README"
entry, err := tree.EntryByIndex(0)
if err != nil {
t.Fatal("Was unable to find the first entry via index.")
}
if entry.Filename() != expected {
t.Fatalf(
"EntryByIndex did not return the proper file. Expected %v, got %v",
expected,
entry.Filename())
}
}
func TestTreeEntryCount(t *testing.T) {
if tree.EntryCount() != 1 {
t.Fatalf("Expected 1 file in the tree, but there were %v", tree.EntryCount())
}
}
// Important: this must be called after all of the Test functions
func TestFinal(t *testing.T) {
if tree != nil {
tree.Free()
}
if revwalk != nil {
revwalk.Free()
}
if repo != nil {
repo.Free()
}
}
// private helper functions
func run(args []string) (err error) {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = path
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(err.Error() + " " + string(output))
}
return
}
func checkFatal(t *testing.T, err error) {
if err != nil {
fmt.Printf("Fatal: %T %v\n", t, err)
os.Exit(0)
}
}
func check(t *testing.T, err error) {
if err != nil {
t.Error(err)
}
}