forked from davidbanham/go-git-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
githttp_test.go
259 lines (247 loc) · 5.69 KB
/
githttp_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
package githttp
import (
"errors"
"gopkg.in/src-d/go-git.v4"
gogit "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
const (
gitRefs = "/info/refs?service="
uploadPack = "git-upload-pack"
testFile = "test.file"
)
func TestNewGitContext(t *testing.T) {
type args struct {
options GitOptions
uri string
check func() error
}
tests := []struct {
name string
args args
wantErr bool
wantRespErr bool
wantResp string
prepare func()
cleanup func()
}{
{
name: "Basic git access test",
args: args{
options: GitOptions{
ProjectRoot: "./testdata",
AutoCreate: true,
ReceivePack: true,
UploadPack: true,
},
uri: "/my/repo" + gitRefs + uploadPack,
check: func() error {
if _, err := os.Stat("./testdata/my/repo/HEAD"); os.IsNotExist(err) {
return errors.New("git HEAD does not exist")
}
return nil
},
},
cleanup: func() {
err := os.RemoveAll("./testdata/my/")
if err != nil {
panic(err)
}
},
wantResp: "service=git-upload-pack",
},
{
name: "Existing git access test",
args: args{
options: GitOptions{
ProjectRoot: "./testdata",
ReceivePack: true,
UploadPack: true,
},
uri: "/existing/repo" + gitRefs + uploadPack,
check: func() error {
if _, err := os.Stat("./testdata/existing/repo/HEAD"); os.IsNotExist(err) {
return errors.New("git HEAD does not exist")
}
return nil
},
},
prepare: func() {
_, err := initRepo("./testdata/existing/repo", true, false)
if err != nil {
panic(err)
}
},
cleanup: func() {
err := os.RemoveAll("./testdata/existing/")
if err != nil {
panic(err)
}
},
wantResp: "service=git-upload-pack",
},
{
name: "Nonexisting git access test",
args: args{
options: GitOptions{
ProjectRoot: "./testdata",
ReceivePack: true,
UploadPack: true,
},
uri: "/invalid/repo" + gitRefs + uploadPack,
check: func() error {
if _, err := os.Stat("./testdata/invalid/repo/HEAD"); os.IsNotExist(err) {
return nil
}
return errors.New("git HEAD unexpectedly exists")
},
},
wantRespErr: true,
},
{
name: "Existing nonbare git access test",
args: args{
options: GitOptions{
ProjectRoot: "./testdata",
AutoCreate: true,
NoBare: true,
ReceivePack: true,
UploadPack: true,
},
uri: "/nonbare/repo" + gitRefs + uploadPack,
check: func() error {
if _, err := os.Stat("./testdata/nonbare/repo/.git/HEAD"); os.IsNotExist(err) {
return errors.New("git HEAD does not exist")
}
return nil
},
},
prepare: func() {
_, err := initRepo("./testdata/nonbare/repo", false, true)
if err != nil {
panic(err)
}
},
cleanup: func() {
err := os.RemoveAll("./testdata/nonbare/")
if err != nil {
panic(err)
}
},
wantResp: "refs/heads/master",
},
{
name: "Nonbare git access test",
args: args{
options: GitOptions{
ProjectRoot: "./testdata",
AutoCreate: true,
NoBare: true,
ReceivePack: true,
UploadPack: true,
},
uri: "/nonbare/new/repo" + gitRefs + uploadPack,
check: func() error {
if _, err := os.Stat("./testdata/nonbare/new/repo/.git/HEAD"); os.IsNotExist(err) {
return errors.New("git HEAD does not exist")
}
return nil
},
},
cleanup: func() {
err := os.RemoveAll("./testdata/nonbare/")
if err != nil {
panic(err)
}
},
wantResp: "service=git-upload-pack",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.prepare != nil {
tt.prepare()
}
rr := httptest.NewRecorder()
gotContext, err := NewGitContext(tt.args.options)
if (err != nil) != tt.wantErr {
t.Errorf("NewGitContext() error = %v, wantErr %v", err, tt.wantErr)
return
}
gotContext.Init()
req, err := http.NewRequest("GET", tt.args.uri, nil)
if err != nil {
panic(err)
}
http.Handle(tt.name, gotContext)
gotContext.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; (status != http.StatusOK) != tt.wantRespErr {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
if !strings.Contains(rr.Body.String(), tt.wantResp) {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), tt.wantResp)
}
// Check the subtest case.
if tt.args.check != nil {
if err := tt.args.check(); err != nil {
t.Errorf("test case failed its check test: %v", err)
}
}
if tt.cleanup != nil {
tt.cleanup()
}
})
}
}
func initRepo(path string, bare bool, withTestFile bool) (*git.Repository, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
err = os.MkdirAll(absPath, os.ModePerm)
if err != nil {
return nil, err
}
repo, err := gogit.PlainInit(absPath, bare)
if err != nil {
return nil, err
}
if withTestFile {
w, err := repo.Worktree()
if err != nil {
return nil, err
}
testFilePath := filepath.Join(absPath, testFile)
err = ioutil.WriteFile(testFilePath, []byte("hello world!"), os.ModePerm)
if err != nil {
return nil, err
}
_, err = w.Add(testFile)
if err != nil {
return nil, err
}
_, err = w.Commit("example go-git commit", &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "[email protected]",
When: time.Now(),
},
})
if err != nil {
return nil, err
}
}
return repo, nil
}