forked from x-motemen/ghq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_repository_test.go
253 lines (226 loc) · 6.9 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
package main
import (
"os"
"path/filepath"
"reflect"
"testing"
)
func TestLocalRepositoryFromFullPath(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
tmproot := newTempDir(t)
defer os.RemoveAll(tmproot)
_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)
}
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)
defer os.RemoveAll(tmproot)
_localRepositoryRoots = []string{tmproot}
testCases := []struct {
name, url, expect string
}{{
name: "GitHub",
url: "ssh://git@github.com/motemen/ghq.git",
expect: filepath.Join(tmproot, "github.com/motemen/ghq"),
}, {
name: "stash",
url: "ssh://git@stash.com/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"),
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r, err := LocalRepositoryFromURL(mustParseURL(tc.url))
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)
defer func(orig string) { os.Setenv("GHQ_ROOT", orig) }(os.Getenv("GHQ_ROOT"))
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
os.Setenv("GHQ_ROOT", tc.root)
got, err := localRepositoryRoots()
if err != nil {
t.Errorf("error should be nil, but: %s", err)
}
if !reflect.DeepEqual(got, tc.expect) {
t.Errorf("\ngot: %+v\nexpect: %+v", got, tc.expect)
}
})
}
}
// https://gist.github.com/kyanny/c231f48e5d08b98ff2c3
func TestList_Symlink(t *testing.T) {
root := newTempDir(t)
defer os.RemoveAll(root)
symDir := newTempDir(t)
defer os.RemoveAll(symDir)
origLocalRepositryRoots := _localRepositoryRoots
_localRepositoryRoots = []string{root}
defer func() { _localRepositoryRoots = origLocalRepositryRoots }()
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)
}
paths := []string{}
walkLocalRepositories(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 TestFindVCSBackend(t *testing.T) {
testCases := []struct {
name string
setup func(t *testing.T) (string, func())
expect *VCSBackend
}{{
name: "git",
setup: func(t *testing.T) (string, func()) {
dir := newTempDir(t)
os.MkdirAll(filepath.Join(dir, ".git"), 0755)
return dir, func() {
os.RemoveAll(dir)
}
},
expect: GitBackend,
}, {
name: "git svn",
setup: func(t *testing.T) (string, func()) {
dir := newTempDir(t)
os.MkdirAll(filepath.Join(dir, ".git", "svn"), 0755)
return dir, func() {
os.RemoveAll(dir)
}
},
expect: GitsvnBackend,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fpath, teardown := tc.setup(t)
defer teardown()
backend := findVCSBackend(fpath)
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)
defer func(orig string) { os.Setenv("GHQ_ROOT", orig) }(os.Getenv("GHQ_ROOT"))
_localRepositoryRoots = nil
tmpdir := newTempDir(t)
os.Setenv("GHQ_ROOT", 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", err)
}
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)
}
})
t.Run("subdir", func(t *testing.T) {
repo, err := LocalRepositoryFromFullPath(subpkg, nil)
if err != nil {
t.Errorf("error should be nil, but: %s", err)
}
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)
}
})
}