-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathserve_test.go
More file actions
227 lines (191 loc) · 4.6 KB
/
serve_test.go
File metadata and controls
227 lines (191 loc) · 4.6 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
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
package servegit
import (
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
const testAddress = "test.local:3939"
var discardLogger = log.New(io.Discard, "", log.LstdFlags)
func TestReposHandler(t *testing.T) {
cases := []struct {
name string
repos []string
}{{
name: "empty",
}, {
name: "simple",
repos: []string{"project1", "project2"},
}, {
name: "nested",
repos: []string{"project1", "project2", "dir/project3", "dir/project4.bare"},
}}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
root := gitInitRepos(t, tc.repos...)
rootFS, err := os.OpenRoot(root)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { rootFS.Close() })
h := (&Serve{
Info: testLogger(t),
Debug: discardLogger,
Addr: testAddress,
Root: root,
RootFS: rootFS,
}).handler()
var want []Repo
for _, name := range tc.repos {
isBare := strings.HasSuffix(name, ".bare")
uri := path.Join("/repos", name)
clonePath := uri
if !isBare {
clonePath += "/.git"
}
want = append(want, Repo{Name: name, URI: uri, ClonePath: clonePath})
}
testReposHandler(t, h, want)
})
}
}
func testReposHandler(t *testing.T, h http.Handler, repos []Repo) {
ts := httptest.NewServer(h)
t.Cleanup(ts.Close)
get := func(path string) string {
res, err := http.Get(ts.URL + path)
if err != nil {
t.Fatal(err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if testing.Verbose() {
t.Logf("GET %s:\n%s", path, b)
}
return string(b)
}
// Check we have some known strings on the index page
index := get("/")
for _, sub := range []string{"http://" + testAddress, "/v1/list-repos", "/repos/"} {
if !strings.Contains(index, sub) {
t.Errorf("index page does not contain substring %q", sub)
}
}
// repos page will list the top-level dirs
list := get("/repos/")
for _, repo := range repos {
if path.Dir(repo.URI) != "/repos" {
continue
}
if !strings.Contains(repo.Name, "/") && !strings.Contains(list, repo.Name) {
t.Errorf("repos page does not contain substring %q", repo.Name)
}
}
// check our API response
type Response struct{ Items []Repo }
var want, got Response
want.Items = repos
if err := json.Unmarshal([]byte(get("/v1/list-repos")), &got); err != nil {
t.Fatal(err)
}
opts := []cmp.Option{
cmpopts.EquateEmpty(),
cmpopts.SortSlices(func(a, b Repo) bool { return a.Name < b.Name }),
}
if !cmp.Equal(want, got, opts...) {
t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(want, got, opts...))
}
}
func gitInitBare(t *testing.T, path string) {
if err := exec.Command("git", "init", "--bare", path).Run(); err != nil {
t.Fatal(err)
}
}
func gitInit(t *testing.T, path string) {
cmd := exec.Command("git", "init")
cmd.Dir = path
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
}
func gitInitRepos(t *testing.T, names ...string) string {
root := t.TempDir()
// We cannot os.OpenRoot on a non-existent dir so we return tmpdir
if len(names) == 0 {
return root
}
root = filepath.Join(root, "repos-root")
for _, name := range names {
p := filepath.Join(root, name)
if err := os.MkdirAll(p, 0755); err != nil {
t.Fatal(err)
}
if strings.HasSuffix(p, ".bare") {
gitInitBare(t, p)
} else {
gitInit(t, p)
}
}
return root
}
func TestIgnoreGitSubmodules(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "dir"), os.ModePerm); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "dir", ".git"), []byte("ignore me please"), os.ModePerm); err != nil {
t.Fatal(err)
}
rootFS, err := os.OpenRoot(root)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { rootFS.Close() })
repos, err := (&Serve{
Info: testLogger(t),
Debug: discardLogger,
Root: root,
RootFS: rootFS,
}).Repos()
if err != nil {
t.Fatal(err)
}
if len(repos) != 0 {
t.Fatalf("expected no repos, got %v", repos)
}
}
func TestIsBareRepo(t *testing.T) {
dir := t.TempDir()
gitInitBare(t, dir)
if !isBareRepo(dir) {
t.Errorf("Path %s it not a bare repository", dir)
}
}
func TestEmptyDirIsNotBareRepo(t *testing.T) {
dir := t.TempDir()
if isBareRepo(dir) {
t.Errorf("Path %s it falsey detected as a bare repository", dir)
}
}
func testLogger(t *testing.T) *log.Logger {
return log.New(testWriter{t}, "testLogger ", log.LstdFlags)
}
type testWriter struct {
*testing.T
}
func (tw testWriter) Write(p []byte) (n int, err error) {
tw.Log(string(p))
return len(p), nil
}