-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit_remote_test.go
More file actions
385 lines (342 loc) · 8.8 KB
/
Copy pathgit_remote_test.go
File metadata and controls
385 lines (342 loc) · 8.8 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
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
382
383
384
385
package main
import (
"errors"
"log"
"os"
"path/filepath"
"testing"
vcsurl "github.com/gitsight/go-vcsurl"
)
func mkTempDir() string {
tmpDir := os.TempDir()
testDir, err := os.MkdirTemp(tmpDir, "tests-*")
if err != nil {
log.Fatal(err)
}
return testDir
}
func TestRemoteUrl(t *testing.T) {
cases := map[string]struct {
path string
branch string
line1 int
line2 int
want string
}{
"root-dir": {
path: "./",
branch: "",
line1: 0,
line2: 0,
want: "https://github.com/inouet/gh-open",
},
"root-dir-master": {
path: "./",
branch: "master",
line1: 0,
line2: 0,
want: "https://github.com/inouet/gh-open/tree/master/",
},
"readme.md": {
path: "./README.md",
branch: "master",
line1: 10,
line2: 0,
want: "https://github.com/inouet/gh-open/tree/master/README.md#L10",
},
}
for name, c := range cases {
gr, err := newGitRemote(c.path)
if err != nil {
t.Fatal(err)
}
got, _ := gr.remoteURL(c.branch, c.line1, c.line2)
if got != c.want {
t.Errorf("%s want '%s', got '%s'\n", name, c.want, got)
}
}
}
func TestRemoteUrlFunctional(t *testing.T) {
t.Parallel()
cloneDir := mkTempDir()
defer os.RemoveAll(cloneDir)
cases := map[string]struct {
repo string
repoDir string
path string
branch string
line1 int
line2 int
want string
}{
"github-cheat-sheet": {
repo: "https://github.com/githubtraining/github-cheat-sheet.git",
repoDir: "github-cheat-sheet",
path: "LICENSE",
branch: "master",
line1: 3,
line2: 4,
want: "https://github.com/githubtraining/github-cheat-sheet/tree/master/LICENSE#L3-L4",
},
"bitbucket-test": {
repo: "https://bitbucket.org/atn13/bitbucketstationlocations.git",
repoDir: "bitbucketstationlocations",
path: "README.txt",
branch: "master",
line1: 2,
line2: 4,
want: "https://bitbucket.org/atn13/bitbucketstationlocations/src/master/README.txt#lines-2:4",
},
"gitlab-gitlab-examples-docker": {
repo: "https://gitlab.com/gitlab-examples/docker.git",
repoDir: "docker",
path: "Dockerfile",
branch: "master",
line1: 1,
line2: 2,
want: "https://gitlab.com/gitlab-examples/docker/-/blob/master/Dockerfile#L1-2",
},
}
git, _ := newGit(cloneDir)
for name, c := range cases {
c := c
git.clone(c.repo)
gitDir := filepath.Join(cloneDir, c.repoDir)
path := filepath.Join(gitDir, c.path)
gr, err := newGitRemote(path)
if err != nil {
t.Fatal(err)
}
got, err := gr.remoteURL(c.branch, c.line1, c.line2)
if err != nil {
t.Fatal(err)
}
if got != c.want {
t.Errorf("%s want '%s', got '%s'\n", name, c.want, got)
}
}
}
func TestConfig(t *testing.T) {
testDir := mkTempDir()
defer os.RemoveAll(testDir)
git, _ := newGit(testDir)
git.clone("https://github.com/githubtraining/github-cheat-sheet.git")
path := filepath.Join(testDir, "/github-cheat-sheet")
git, _ = newGit(path)
// set config
git.exec("config", gitConfigURLTypeName, "bitbucket.org")
git.exec("config", gitConfigProtocolName, "http")
gr, err := newGitRemote(filepath.Join(path, "LICENSE"))
if err != nil {
t.Fatal(err)
}
got, _ := gr.remoteURL("master", 3, 4)
// Expect bitbucket style url and http protocol
want := "http://github.com/githubtraining/github-cheat-sheet/src/master/LICENSE#lines-3:4"
if got != want {
t.Errorf("want '%s', got '%s'\n", want, got)
}
}
func TestNewGitRemote(t *testing.T) {
emptyDir := mkTempDir()
defer os.RemoveAll(emptyDir)
cases := []struct {
path string
want string
wantErr error
}{
{
path: emptyDir,
wantErr: errors.New("not a git repository (or any of the parent directories)"),
},
}
for _, c := range cases {
_, err := newGitRemote(c.path)
if c.wantErr != nil {
if err == nil {
t.Errorf("want '%s', got '%s'\n", c.wantErr.Error(), err)
continue
}
if c.wantErr.Error() != err.Error() {
t.Errorf("want '%s', got '%s'\n", c.wantErr.Error(), err.Error())
}
}
}
}
func TestRelativePath(t *testing.T) {
testDir := mkTempDir()
defer os.RemoveAll(testDir)
subDir := "foo/bar/baz"
os.MkdirAll(testDir+"/"+subDir, 0777)
cases := []struct {
path1 string
path2 string
want string
}{
{
path1: testDir,
path2: testDir + "/" + subDir,
want: subDir,
},
{
path1: testDir,
path2: testDir,
want: "", // same directory return ""
},
}
for _, c := range cases {
got, _ := relativePath(c.path1, c.path2)
if got != c.want {
t.Errorf("want '%s', got '%s'\n", c.want, got)
}
}
}
func TestGetLineOption(t *testing.T) {
cases := []struct {
input string
wantLine1 int
wantLine2 int
wantErr bool
}{
{input: "", wantLine1: 0, wantLine2: 0, wantErr: false},
{input: "3", wantLine1: 3, wantLine2: 0, wantErr: false},
{input: "3-10", wantLine1: 3, wantLine2: 10, wantErr: false},
{input: "3-", wantLine1: 0, wantLine2: 0, wantErr: true},
}
for _, c := range cases {
line1, line2, err := getLineOption(c.input)
if c.wantErr && err == nil {
t.Errorf("'%s' wantErr %v, got %v\n", c.input, c.wantErr, err)
}
if line1 != c.wantLine1 || line2 != c.wantLine2 {
t.Errorf("'%s' wantErr %v, got %v\n", c.input, c.wantErr, err)
}
}
}
// TestParseSSHRemoteURL tests if SSH format remote URLs can be parsed correctly
func TestParseSSHRemoteURL(t *testing.T) {
// Standard SSH URLs
standardCases := []struct {
name string
remoteURL string
wantHost string
wantUser string
wantRepo string
shouldFail bool
}{
{
name: "standard ssh format",
remoteURL: "[email protected]:inouet/gh-open.git",
wantHost: "github.com",
wantUser: "inouet",
wantRepo: "gh-open",
},
{
name: "standard ssh format without .git",
remoteURL: "[email protected]:inouet/gh-open",
wantHost: "github.com",
wantUser: "inouet",
wantRepo: "gh-open",
},
{
name: "gitlab ssh format",
remoteURL: "[email protected]:username/repository.git",
wantHost: "gitlab.com",
wantUser: "username",
wantRepo: "repository",
},
{
name: "bitbucket ssh format",
remoteURL: "[email protected]:username/repository.git",
wantHost: "bitbucket.org",
wantUser: "username",
wantRepo: "repository",
},
}
// Test standard SSH URLs with vcsurl.Parse
for _, c := range standardCases {
t.Run(c.name, func(t *testing.T) {
info, err := vcsurl.Parse(c.remoteURL)
if c.shouldFail {
if err == nil {
t.Errorf("expected error but got none for %s", c.remoteURL)
}
return
}
if err != nil {
t.Fatalf("failed to parse %s: %v", c.remoteURL, err)
}
if string(info.Host) != c.wantHost {
t.Errorf("host: want %s, got %s", c.wantHost, info.Host)
}
if info.Username != c.wantUser {
t.Errorf("username: want %s, got %s", c.wantUser, info.Username)
}
if info.Name != c.wantRepo {
t.Errorf("repo name: want %s, got %s", c.wantRepo, info.Name)
}
// Check if a correct web URL can be generated from the successfully parsed URL
expectedWebURL := "https://" + c.wantHost + "/" + c.wantUser + "/" + c.wantRepo
host := string(info.Host)
fullName := info.FullName
webURL := "https://" + host + "/" + fullName
if webURL != expectedWebURL {
t.Errorf("web URL: want %s, got %s", expectedWebURL, webURL)
}
})
}
// Organization format SSH URLs
orgCases := []struct {
name string
remoteURL string
wantHost string
wantUser string
wantRepo string
shouldFail bool
}{
{
name: "organization format with ID",
remoteURL: "[email protected]:complex/repo-name.git",
wantHost: "github.com",
wantUser: "complex",
wantRepo: "repo-name",
},
{
name: "complex organization name",
remoteURL: "[email protected]:complex/repo-name.git",
wantHost: "github.com",
wantUser: "complex",
wantRepo: "repo-name",
},
}
// Test organization format SSH URLs with custom function
for _, c := range orgCases {
t.Run(c.name, func(t *testing.T) {
host, username, repo, err := parseSSHRemoteURL(c.remoteURL)
if c.shouldFail {
if err == nil {
t.Errorf("expected error but got none for %s", c.remoteURL)
}
return
}
if err != nil {
t.Fatalf("failed to parse %s: %v", c.remoteURL, err)
}
if host != c.wantHost {
t.Errorf("host: want %s, got %s", c.wantHost, host)
}
if username != c.wantUser {
t.Errorf("username: want %s, got %s", c.wantUser, username)
}
if repo != c.wantRepo {
t.Errorf("repo name: want %s, got %s", c.wantRepo, repo)
}
// Check if a correct web URL can be generated from the successfully parsed URL
expectedWebURL := "https://" + c.wantHost + "/" + c.wantUser + "/" + c.wantRepo
webURL := "https://" + host + "/" + username + "/" + repo
if webURL != expectedWebURL {
t.Errorf("web URL: want %s, got %s", expectedWebURL, webURL)
}
})
}
}