@@ -25,27 +25,41 @@ import (
25
25
)
26
26
27
27
func Test_ParseGitUrl (t * testing.T ) {
28
+ invalidUrlError := "URL is invalid"
29
+ invalidUrlPathError := "url path to directory or file should contain*"
30
+ missingUserAndRepoError := "url path should contain <user>/<repo>*"
31
+
32
+ invalidGitHostError := "url host should be a valid GitHub, GitLab, or Bitbucket host*"
33
+ invalidGitHubPathError := "url path should contain <owner>/<repo>/<tree or blob>/<branch>/<path/to/file/or/directory>*"
34
+ invalidGitHubRawPathError := "raw url path should contain <owner>/<repo>/<branch>/<path/to/file>*"
35
+
36
+ invalidGitLabPathError := "url path to directory or file should contain <blob or tree or raw>/<branch>/<path/to/file/or/directory>*"
37
+ missingGitLabKeywordError := "url path should contain 'blob' or 'tree' or 'raw'*"
38
+
39
+ invalidBitbucketPathError := "url path should contain path to directory or file*"
40
+ missingBitbucketKeywordError := "url path should contain 'raw' or 'src'*"
41
+
28
42
tests := []struct {
29
43
name string
30
44
url string
31
- wantUrl Url
45
+ wantUrl GitUrl
32
46
wantErr string
33
47
}{
34
48
{
35
49
name : "should fail with empty url" ,
36
50
url : "" ,
37
- wantErr : "URL is invalid" ,
51
+ wantErr : invalidUrlError ,
38
52
},
39
53
{
40
54
name : "should fail with invalid git host" ,
41
55
url : "https://google.ca/" ,
42
- wantErr : "url host should be a valid GitHub, GitLab, or Bitbucket host*" ,
56
+ wantErr : invalidGitHostError ,
43
57
},
44
58
// GitHub
45
59
{
46
60
name : "should parse GitHub repo with root path" ,
47
61
url : "https://github.com/devfile/library" ,
48
- wantUrl : Url {
62
+ wantUrl : GitUrl {
49
63
Protocol : "https" ,
50
64
Host : "github.com" ,
51
65
Owner : "devfile" ,
@@ -58,7 +72,7 @@ func Test_ParseGitUrl(t *testing.T) {
58
72
{
59
73
name : "should parse GitHub repo with root path and tag" ,
60
74
url : "https://github.com/devfile/library/tree/v2.2.0" ,
61
- wantUrl : Url {
75
+ wantUrl : GitUrl {
62
76
Protocol : "https" ,
63
77
Host : "github.com" ,
64
78
Owner : "devfile" ,
@@ -71,7 +85,7 @@ func Test_ParseGitUrl(t *testing.T) {
71
85
{
72
86
name : "should parse GitHub repo with root path and revision" ,
73
87
url : "https://github.com/devfile/library/tree/0ce592a416fb185564516353891a45016ac7f671" ,
74
- wantUrl : Url {
88
+ wantUrl : GitUrl {
75
89
Protocol : "https" ,
76
90
Host : "github.com" ,
77
91
Owner : "devfile" ,
@@ -84,12 +98,12 @@ func Test_ParseGitUrl(t *testing.T) {
84
98
{
85
99
name : "should fail with only GitHub host" ,
86
100
url : "https://github.com/" ,
87
- wantErr : "url path should contain <user>/<repo>*" ,
101
+ wantErr : missingUserAndRepoError ,
88
102
},
89
103
{
90
104
name : "should parse GitHub repo with file path" ,
91
105
url : "https://github.com/devfile/library/blob/main/devfile.yaml" ,
92
- wantUrl : Url {
106
+ wantUrl : GitUrl {
93
107
Protocol : "https" ,
94
108
Host : "github.com" ,
95
109
Owner : "devfile" ,
@@ -102,7 +116,7 @@ func Test_ParseGitUrl(t *testing.T) {
102
116
{
103
117
name : "should parse GitHub repo with raw file path" ,
104
118
url : "https://raw.githubusercontent.com/devfile/library/main/devfile.yaml" ,
105
- wantUrl : Url {
119
+ wantUrl : GitUrl {
106
120
Protocol : "https" ,
107
121
Host : "raw.githubusercontent.com" ,
108
122
Owner : "devfile" ,
@@ -115,38 +129,38 @@ func Test_ParseGitUrl(t *testing.T) {
115
129
{
116
130
name : "should fail with missing GitHub repo" ,
117
131
url : "https://github.com/devfile" ,
118
- wantErr : "url path should contain <user>/<repo>*" ,
132
+ wantErr : missingUserAndRepoError ,
119
133
},
120
134
{
121
135
name : "should fail with missing GitHub blob" ,
122
136
url : "https://github.com/devfile/library/main/devfile.yaml" ,
123
- wantErr : "url path to directory or file should contain*" ,
137
+ wantErr : invalidUrlPathError ,
124
138
},
125
139
{
126
140
name : "should fail with missing GitHub tree" ,
127
141
url : "https://github.com/devfile/library/main/tests/yamls" ,
128
- wantErr : "url path to directory or file should contain*" ,
142
+ wantErr : invalidUrlPathError ,
129
143
},
130
144
{
131
145
name : "should fail with just GitHub tree" ,
132
146
url : "https://github.com/devfile/library/tree" ,
133
- wantErr : "url path should contain <owner>/<repo>/<tree or blob>/<branch>/<path/to/file/or/directory>*" ,
147
+ wantErr : invalidGitHubPathError ,
134
148
},
135
149
{
136
150
name : "should fail with just GitHub blob" ,
137
151
url : "https://github.com/devfile/library/blob" ,
138
- wantErr : "url path should contain <owner>/<repo>/<tree or blob>/<branch>/<path/to/file/or/directory>*" ,
152
+ wantErr : invalidGitHubPathError ,
139
153
},
140
154
{
141
155
name : "should fail with invalid GitHub raw file path" ,
142
156
url : "https://raw.githubusercontent.com/devfile/library/devfile.yaml" ,
143
- wantErr : "raw url path should contain <owner>/<repo>/<branch>/<path/to/file>*" ,
157
+ wantErr : invalidGitHubRawPathError ,
144
158
},
145
159
// Gitlab
146
160
{
147
161
name : "should parse GitLab repo with root path" ,
148
162
url : "https://gitlab.com/gitlab-org/gitlab-foss" ,
149
- wantUrl : Url {
163
+ wantUrl : GitUrl {
150
164
Protocol : "https" ,
151
165
Host : "gitlab.com" ,
152
166
Owner : "gitlab-org" ,
@@ -159,12 +173,12 @@ func Test_ParseGitUrl(t *testing.T) {
159
173
{
160
174
name : "should fail with only GitLab host" ,
161
175
url : "https://gitlab.com/" ,
162
- wantErr : "url path should contain <user>/<repo>*" ,
176
+ wantErr : missingUserAndRepoError ,
163
177
},
164
178
{
165
179
name : "should parse GitLab repo with file path" ,
166
180
url : "https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/README.md" ,
167
- wantUrl : Url {
181
+ wantUrl : GitUrl {
168
182
Protocol : "https" ,
169
183
Host : "gitlab.com" ,
170
184
Owner : "gitlab-org" ,
@@ -177,23 +191,23 @@ func Test_ParseGitUrl(t *testing.T) {
177
191
{
178
192
name : "should fail with missing GitLab repo" ,
179
193
url : "https://gitlab.com/gitlab-org" ,
180
- wantErr : "url path should contain <user>/<repo>*" ,
194
+ wantErr : missingUserAndRepoError ,
181
195
},
182
196
{
183
197
name : "should fail with missing GitLab keywords" ,
184
198
url : "https://gitlab.com/gitlab-org/gitlab-foss/-/master/directory/README.md" ,
185
- wantErr : "url path should contain 'blob' or 'tree' or 'raw'*" ,
199
+ wantErr : missingGitLabKeywordError ,
186
200
},
187
201
{
188
202
name : "should fail with missing GitLab file or directory path" ,
189
203
url : "https://gitlab.com/gitlab-org/gitlab-foss/-/tree/master" ,
190
- wantErr : "url path to directory or file should contain <blob or tree or raw>/<branch>/<path/to/file/or/directory>*" ,
204
+ wantErr : invalidGitLabPathError ,
191
205
},
192
206
// Bitbucket
193
207
{
194
208
name : "should parse Bitbucket repo with root path" ,
195
209
url : "https://bitbucket.org/fake-owner/fake-public-repo" ,
196
- wantUrl : Url {
210
+ wantUrl : GitUrl {
197
211
Protocol : "https" ,
198
212
Host : "bitbucket.org" ,
199
213
Owner : "fake-owner" ,
@@ -206,12 +220,12 @@ func Test_ParseGitUrl(t *testing.T) {
206
220
{
207
221
name : "should fail with only Bitbucket host" ,
208
222
url : "https://bitbucket.org/" ,
209
- wantErr : "url path should contain <user>/<repo>*" ,
223
+ wantErr : missingUserAndRepoError ,
210
224
},
211
225
{
212
226
name : "should parse Bitbucket repo with file path" ,
213
227
url : "https://bitbucket.org/fake-owner/fake-public-repo/src/main/README.md" ,
214
- wantUrl : Url {
228
+ wantUrl : GitUrl {
215
229
Protocol : "https" ,
216
230
Host : "bitbucket.org" ,
217
231
Owner : "fake-owner" ,
@@ -224,7 +238,7 @@ func Test_ParseGitUrl(t *testing.T) {
224
238
{
225
239
name : "should parse Bitbucket file path with nested path" ,
226
240
url : "https://bitbucket.org/fake-owner/fake-public-repo/src/main/directory/test.txt" ,
227
- wantUrl : Url {
241
+ wantUrl : GitUrl {
228
242
Protocol : "https" ,
229
243
Host : "bitbucket.org" ,
230
244
Owner : "fake-owner" ,
@@ -237,7 +251,7 @@ func Test_ParseGitUrl(t *testing.T) {
237
251
{
238
252
name : "should parse Bitbucket repo with raw file path" ,
239
253
url : "https://bitbucket.org/fake-owner/fake-public-repo/raw/main/README.md" ,
240
- wantUrl : Url {
254
+ wantUrl : GitUrl {
241
255
Protocol : "https" ,
242
256
Host : "bitbucket.org" ,
243
257
Owner : "fake-owner" ,
@@ -250,17 +264,17 @@ func Test_ParseGitUrl(t *testing.T) {
250
264
{
251
265
name : "should fail with missing Bitbucket repo" ,
252
266
url : "https://bitbucket.org/fake-owner" ,
253
- wantErr : "url path should contain <user>/<repo>*" ,
267
+ wantErr : missingUserAndRepoError ,
254
268
},
255
269
{
256
270
name : "should fail with invalid Bitbucket directory or file path" ,
257
271
url : "https://bitbucket.org/fake-owner/fake-public-repo/main/README.md" ,
258
- wantErr : "url path should contain path to directory or file*" ,
272
+ wantErr : invalidBitbucketPathError ,
259
273
},
260
274
{
261
275
name : "should fail with missing Bitbucket keywords" ,
262
276
url : "https://bitbucket.org/fake-owner/fake-public-repo/main/test/README.md" ,
263
- wantErr : "url path should contain 'raw' or 'src'*" ,
277
+ wantErr : missingBitbucketKeywordError ,
264
278
},
265
279
}
266
280
@@ -281,12 +295,12 @@ func Test_ParseGitUrl(t *testing.T) {
281
295
func Test_GetGitRawFileAPI (t * testing.T ) {
282
296
tests := []struct {
283
297
name string
284
- g Url
298
+ g GitUrl
285
299
want string
286
300
}{
287
301
{
288
302
name : "Github url" ,
289
- g : Url {
303
+ g : GitUrl {
290
304
Protocol : "https" ,
291
305
Host : "github.com" ,
292
306
Owner : "devfile" ,
@@ -298,19 +312,19 @@ func Test_GetGitRawFileAPI(t *testing.T) {
298
312
},
299
313
{
300
314
name : "GitLab url" ,
301
- g : Url {
315
+ g : GitUrl {
302
316
Protocol : "https" ,
303
317
Host : "gitlab.com" ,
304
318
Owner : "gitlab-org" ,
305
319
Repo : "gitlab" ,
306
- Revision : "master " ,
320
+ Revision : "v15.11.0-ee " ,
307
321
Path : "README.md" ,
308
322
},
309
- want : "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/files/README.md/raw" ,
323
+ want : "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/files/README.md/raw?ref=v15.11.0-ee " ,
310
324
},
311
325
{
312
326
name : "Bitbucket url" ,
313
- g : Url {
327
+ g : GitUrl {
314
328
Protocol : "https" ,
315
329
Host : "bitbucket.org" ,
316
330
Owner : "owner" ,
@@ -322,7 +336,7 @@ func Test_GetGitRawFileAPI(t *testing.T) {
322
336
},
323
337
{
324
338
name : "Empty GitUrl" ,
325
- g : Url {},
339
+ g : GitUrl {},
326
340
want : "" ,
327
341
},
328
342
}
@@ -338,7 +352,7 @@ func Test_GetGitRawFileAPI(t *testing.T) {
338
352
}
339
353
340
354
func Test_IsPublic (t * testing.T ) {
341
- publicGitUrl := Url {
355
+ publicGitUrl := GitUrl {
342
356
Protocol : "https" ,
343
357
Host : "github.com" ,
344
358
Owner : "devfile" ,
@@ -347,7 +361,7 @@ func Test_IsPublic(t *testing.T) {
347
361
token : "fake-token" ,
348
362
}
349
363
350
- privateGitUrl := Url {
364
+ privateGitUrl := GitUrl {
351
365
Protocol : "https" ,
352
366
Host : "github.com" ,
353
367
Owner : "not" ,
@@ -360,7 +374,7 @@ func Test_IsPublic(t *testing.T) {
360
374
361
375
tests := []struct {
362
376
name string
363
- g Url
377
+ g GitUrl
364
378
want bool
365
379
}{
366
380
{
@@ -387,43 +401,16 @@ func Test_IsPublic(t *testing.T) {
387
401
388
402
func Test_CloneGitRepo (t * testing.T ) {
389
403
tempInvalidDir := t .TempDir ()
390
- tempDirGitHub := t .TempDir ()
391
- tempDirGitLab := t .TempDir ()
392
- tempDirBitbucket := t .TempDir ()
393
404
394
- invalidGitUrl := Url {
405
+ invalidGitUrl := GitUrl {
395
406
Protocol : "" ,
396
407
Host : "" ,
397
408
Owner : "nonexistent" ,
398
409
Repo : "nonexistent" ,
399
410
Revision : "nonexistent" ,
400
411
}
401
412
402
- validPublicGitHubUrl := Url {
403
- Protocol : "https" ,
404
- Host : "github.com" ,
405
- Owner : "devfile" ,
406
- Repo : "library" ,
407
- Revision : "main" ,
408
- }
409
-
410
- validPublicGitLabUrl := Url {
411
- Protocol : "https" ,
412
- Host : "gitlab.com" ,
413
- Owner : "mike-hoang" ,
414
- Repo : "public-testing-repo" ,
415
- Revision : "main" ,
416
- }
417
-
418
- validPublicBitbucketUrl := Url {
419
- Protocol : "https" ,
420
- Host : "bitbucket.org" ,
421
- Owner : "mike-hoang" ,
422
- Repo : "public-testing-repo" ,
423
- Revision : "master" ,
424
- }
425
-
426
- invalidPrivateGitHubRepo := Url {
413
+ invalidPrivateGitHubRepo := GitUrl {
427
414
Protocol : "https" ,
428
415
Host : "github.com" ,
429
416
Owner : "fake-owner" ,
@@ -438,7 +425,7 @@ func Test_CloneGitRepo(t *testing.T) {
438
425
439
426
tests := []struct {
440
427
name string
441
- gitUrl Url
428
+ gitUrl GitUrl
442
429
destDir string
443
430
wantErr string
444
431
}{
@@ -460,21 +447,6 @@ func Test_CloneGitRepo(t *testing.T) {
460
447
destDir : tempInvalidDir ,
461
448
wantErr : privateRepoBadTokenErr ,
462
449
},
463
- {
464
- name : "should be able to clone valid public github url" ,
465
- gitUrl : validPublicGitHubUrl ,
466
- destDir : tempDirGitHub ,
467
- },
468
- {
469
- name : "should be able to clone valid public gitlab url" ,
470
- gitUrl : validPublicGitLabUrl ,
471
- destDir : tempDirGitLab ,
472
- },
473
- {
474
- name : "should be able to clone valid public bitbucket url" ,
475
- gitUrl : validPublicBitbucketUrl ,
476
- destDir : tempDirBitbucket ,
477
- },
478
450
}
479
451
480
452
for _ , tt := range tests {
0 commit comments