-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
local_repository_test.go
381 lines (339 loc) · 10 KB
/
local_repository_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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"sync"
"testing"
"github.com/Songmu/gitconfig"
)
func samePathSlice(lhss, rhss []string) bool {
if len(lhss) != len(rhss) {
return false
}
lhssAbs := make([]string, len(lhss))
rhssAbs := make([]string, len(rhss))
for i, p := range lhss {
lhsAbs, _ := filepath.Abs(filepath.Clean(p))
lhssAbs[i] = strings.ToLower(lhsAbs)
rhsAbs, _ := filepath.Abs(filepath.Clean(rhss[i]))
rhssAbs[i] = strings.ToLower(rhsAbs)
}
sort.Strings(lhssAbs)
sort.Strings(rhssAbs)
for i := range lhssAbs {
if lhssAbs[i] != rhssAbs[i] {
return false
}
}
return true
}
func TestLocalRepositoryFromFullPath(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
tmproot := newTempDir(t)
_localRepositoryRoots = []string{tmproot}
testCases := []struct {
fpath string
expect string
subpaths []string
}{{
fpath: filepath.Join(tmproot, "github.com/motemen/ghq"),
expect: "motemen/ghq",
subpaths: []string{"ghq", "motemen/ghq", "github.com/motemen/ghq"},
}, {
fpath: filepath.Join(tmproot, "stash.com/scm/motemen/ghq"),
expect: "scm/motemen/ghq",
subpaths: []string{"ghq", "motemen/ghq", "scm/motemen/ghq", "stash.com/scm/motemen/ghq"},
}}
for _, tc := range testCases {
t.Run(tc.fpath, func(t *testing.T) {
r, err := LocalRepositoryFromFullPath(tc.fpath, nil)
if err != nil {
t.Errorf("error should be nil but: %s", err)
return
}
if r.NonHostPath() != tc.expect {
t.Errorf("NonHostPath: got: %s, expect: %s", r.NonHostPath(), tc.expect)
}
if !reflect.DeepEqual(r.Subpaths(), tc.subpaths) {
t.Errorf("Subpaths:\ngot: %+v\nexpect: %+v", r.Subpaths(), tc.subpaths)
}
})
}
}
func TestNewLocalRepository(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
tmproot := newTempDir(t)
_localRepositoryRoots = []string{tmproot}
testCases := []struct {
name, url, expect string
}{{
name: "GitHub",
url: "ssh://[email protected]/motemen/ghq.git",
expect: filepath.Join(tmproot, "github.com/motemen/ghq"),
}, {
name: "stash",
url: "ssh://[email protected]/scm/motemen/ghq.git",
expect: filepath.Join(tmproot, "stash.com/scm/motemen/ghq"),
}, {
name: "svn Sourceforge",
url: "http://svn.code.sf.net/p/ghq/code/trunk",
expect: filepath.Join(tmproot, "svn.code.sf.net/p/ghq/code/trunk"),
}, {
name: "git Sourceforge",
url: "http://git.code.sf.net/p/ghq/code",
expect: filepath.Join(tmproot, "git.code.sf.net/p/ghq/code"),
}, {
name: "svn Sourceforge JP",
url: "http://scm.sourceforge.jp/svnroot/ghq/",
expect: filepath.Join(tmproot, "scm.sourceforge.jp/svnroot/ghq"),
}, {
name: "git Sourceforge JP",
url: "http://scm.sourceforge.jp/gitroot/ghq/ghq.git",
expect: filepath.Join(tmproot, "scm.sourceforge.jp/gitroot/ghq/ghq"),
}, {
name: "svn Assembla",
url: "https://subversion.assembla.com/svn/ghq/",
expect: filepath.Join(tmproot, "subversion.assembla.com/svn/ghq"),
}, {
name: "git Assembla",
url: "https://git.assembla.com/ghq.git",
expect: filepath.Join(tmproot, "git.assembla.com/ghq"),
}, {
name: "bitbucket host with port",
url: "https://bitbucket.local:8888/motemen/ghq.git",
expect: filepath.Join(tmproot, "bitbucket.local/motemen/ghq"),
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
defer func(orig string) { _home = orig }(_home)
_home = ""
homeOnce = &sync.Once{}
r, err := LocalRepositoryFromURL(mustParseURL(tc.url), false)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
if r.FullPath != tc.expect {
t.Errorf("got: %s, expect: %s", r.FullPath, tc.expect)
}
})
}
}
func TestLocalRepositoryRoots(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
testCases := []struct {
root string
expect []string
}{{
root: "/path/to/ghqroot",
expect: []string{"/path/to/ghqroot"},
}, {
root: "/path/to/ghqroot1" + string(os.PathListSeparator) + "/path/to/ghqroot2",
expect: []string{"/path/to/ghqroot1", "/path/to/ghqroot2"},
}, {
root: "/path/to/ghqroot11" + string(os.PathListSeparator) + "vendor",
expect: []string{"/path/to/ghqroot11", filepath.Join(wd, "vendor")},
}}
for _, tc := range testCases {
t.Run(tc.root, func(t *testing.T) {
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
setEnv(t, envGhqRoot, tc.root)
got, err := localRepositoryRoots(true)
if err != nil {
t.Errorf("error should be nil, but: %s", err)
}
if !samePathSlice(got, tc.expect) {
t.Errorf("\ngot: %+v\nexpect: %+v", got, tc.expect)
}
})
}
}
// https://gist.github.com/kyanny/c231f48e5d08b98ff2c3
func TestList_Symlink(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}
root := newTempDir(t)
symDir := newTempDir(t)
origLocalRepositoryRoots := _localRepositoryRoots
_localRepositoryRoots = []string{root}
defer func() { _localRepositoryRoots = origLocalRepositoryRoots }()
if err := os.MkdirAll(filepath.Join(root, "github.com", "atom", "atom", ".git"), 0777); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(root, "github.com", "zabbix", "zabbix", ".git"), 0777); err != nil {
t.Fatal(err)
}
if err := os.Symlink(symDir, filepath.Join(root, "github.com", "ghq")); err != nil {
t.Fatal(err)
}
var paths []string
walkAllLocalRepositories(func(repo *LocalRepository) {
paths = append(paths, repo.RelPath)
})
if len(paths) != 2 {
t.Errorf("length of paths should be 2, but: %d", len(paths))
}
}
func TestList_Symlink_In_Same_Directory(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}
root := newTempDir(t)
symDir := newTempDir(t)
origLocalRepositoryRoots := _localRepositoryRoots
_localRepositoryRoots = []string{root}
defer func() { _localRepositoryRoots = origLocalRepositoryRoots }()
if err := os.MkdirAll(filepath.Join(root, "github.com", "root-user", "a-repository", ".git"), 0777); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(root, "github.com", "root-user", "z-repository", ".git"), 0777); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(symDir, "github.com", "sym-user", "h-repository", ".git"), 0777); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(symDir, "github.com", "sym-user", "h-repository"), filepath.Join(root, "github.com", "root-user", "h-repository")); err != nil {
t.Fatal(err)
}
var paths []string
walkAllLocalRepositories(func(repo *LocalRepository) {
paths = append(paths, repo.RelPath)
})
if len(paths) != 3 {
t.Errorf("length of paths should be 3, but: %d", len(paths))
}
}
func TestFindVCSBackend(t *testing.T) {
testCases := []struct {
name string
setup func(t *testing.T) (string, string)
expect *VCSBackend
}{{
name: "git-bare",
setup: func(t *testing.T) (string, string) {
dir := newTempDir(t)
dir = dir + ".git"
os.MkdirAll(dir, 0o755)
return dir, ""
},
expect: GitBackend,
}, {
name: "git",
setup: func(t *testing.T) (string, string) {
dir := newTempDir(t)
os.MkdirAll(filepath.Join(dir, ".git"), 0755)
return dir, ""
},
expect: GitBackend,
}, {
name: "git svn",
setup: func(t *testing.T) (string, string) {
dir := newTempDir(t)
os.MkdirAll(filepath.Join(dir, ".git", "svn"), 0755)
return dir, ""
},
expect: GitBackend,
}, {
name: "git with matched vcs",
setup: func(t *testing.T) (string, string) {
dir := newTempDir(t)
os.MkdirAll(filepath.Join(dir, ".git"), 0755)
return dir, "git"
},
expect: GitBackend,
}, {
name: "git with not matched vcs",
setup: func(t *testing.T) (string, string) {
dir := newTempDir(t)
os.MkdirAll(filepath.Join(dir, ".git"), 0755)
return dir, "mercurial"
},
expect: nil,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fpath, vcs := tc.setup(t)
backend := findVCSBackend(fpath, vcs)
if backend != tc.expect {
t.Errorf("got: %v, expect: %v", backend, tc.expect)
}
})
}
}
func TestLocalRepository_VCS(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
tmpdir := newTempDir(t)
setEnv(t, envGhqRoot, tmpdir)
pkg := filepath.Join(tmpdir, "github.com", "motemen", "ghq")
subpkg := filepath.Join(pkg, "logger")
os.MkdirAll(filepath.Join(pkg, ".git"), 0755)
os.MkdirAll(subpkg, 0755)
t.Run("reporoot", func(t *testing.T) {
repo, err := LocalRepositoryFromFullPath(pkg, nil)
if err != nil {
t.Errorf("error should be nil, but: %s (%s)", err, pkg)
return
}
vcs, repoPath := repo.VCS()
if vcs != GitBackend {
t.Errorf("repo.VCS() = %+v, expect: GitBackend", vcs)
return
}
if repoPath != pkg {
t.Errorf("got: %s, expect: %s", repoPath, pkg)
}
})
t.Run("subdir", func(t *testing.T) {
repo, err := LocalRepositoryFromFullPath(subpkg, nil)
if err != nil {
t.Errorf("error should be nil, but: %s", err)
return
}
vcs, repoPath := repo.VCS()
if vcs != GitBackend {
t.Errorf("repo.VCS() = %+v, expect: GitBackend", vcs)
}
if repoPath != pkg {
t.Errorf("got: %s, expect: %s", repoPath, pkg)
}
})
}
func TestLocalRepositoryRoots_URLMatchLocalRepositoryRoots(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}
setEnv(t, "HOME", "/home/tmp")
defer func(orig string) { _home = orig }(_home)
_home = ""
homeOnce = &sync.Once{}
t.Cleanup(gitconfig.WithConfig(t, `
[ghq]
root = /hoge
[ghq "https://github.com/hatena"]
root = ~/proj/hatena
root = /backups/hatena
[ghq "https://github.com/natureglobal"]
root = ~/proj/natureglobal
`))
want := []string{"/hoge", "/home/tmp/proj/hatena", "/backups/hatena", "/home/tmp/proj/natureglobal"}
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
got, err := localRepositoryRoots(true)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("localRepositoryRoots(true) = %+v, want: %+v", got, want)
}
}