@@ -30,11 +30,11 @@ var (
30
30
bitbucketToken = "fake-bitbucket-token"
31
31
)
32
32
33
- func Test_ParseGitUrl (t * testing.T ) {
33
+ func Test_NewGitUrl (t * testing.T ) {
34
34
tests := []struct {
35
35
name string
36
36
url string
37
- wantUrl GitUrl
37
+ wantUrl * GitUrl
38
38
wantErr string
39
39
}{
40
40
{
@@ -51,7 +51,7 @@ func Test_ParseGitUrl(t *testing.T) {
51
51
{
52
52
name : "should parse public GitHub repo with root path" ,
53
53
url : "https://github.com/devfile/library" ,
54
- wantUrl : GitUrl {
54
+ wantUrl : & GitUrl {
55
55
Protocol : "https" ,
56
56
Host : "github.com" ,
57
57
Owner : "devfile" ,
@@ -70,7 +70,7 @@ func Test_ParseGitUrl(t *testing.T) {
70
70
{
71
71
name : "should parse public GitHub repo with file path" ,
72
72
url : "https://github.com/devfile/library/blob/main/devfile.yaml" ,
73
- wantUrl : GitUrl {
73
+ wantUrl : & GitUrl {
74
74
Protocol : "https" ,
75
75
Host : "github.com" ,
76
76
Owner : "devfile" ,
@@ -84,7 +84,7 @@ func Test_ParseGitUrl(t *testing.T) {
84
84
{
85
85
name : "should parse public GitHub repo with raw file path" ,
86
86
url : "https://raw.githubusercontent.com/devfile/library/main/devfile.yaml" ,
87
- wantUrl : GitUrl {
87
+ wantUrl : & GitUrl {
88
88
Protocol : "https" ,
89
89
Host : "raw.githubusercontent.com" ,
90
90
Owner : "devfile" ,
@@ -108,7 +108,7 @@ func Test_ParseGitUrl(t *testing.T) {
108
108
{
109
109
name : "should parse private GitHub repo with token" ,
110
110
url : "https://github.com/fake-owner/fake-private-repo" ,
111
- wantUrl : GitUrl {
111
+ wantUrl : & GitUrl {
112
112
Protocol : "https" ,
113
113
Host : "github.com" ,
114
114
Owner : "fake-owner" ,
@@ -122,7 +122,7 @@ func Test_ParseGitUrl(t *testing.T) {
122
122
{
123
123
name : "should parse private raw GitHub file path with token" ,
124
124
url : "https://raw.githubusercontent.com/fake-owner/fake-private-repo/main/README.md" ,
125
- wantUrl : GitUrl {
125
+ wantUrl : & GitUrl {
126
126
Protocol : "https" ,
127
127
Host : "raw.githubusercontent.com" ,
128
128
Owner : "fake-owner" ,
@@ -137,7 +137,7 @@ func Test_ParseGitUrl(t *testing.T) {
137
137
{
138
138
name : "should parse public GitLab repo with root path" ,
139
139
url : "https://gitlab.com/gitlab-org/gitlab-foss" ,
140
- wantUrl : GitUrl {
140
+ wantUrl : & GitUrl {
141
141
Protocol : "https" ,
142
142
Host : "gitlab.com" ,
143
143
Owner : "gitlab-org" ,
@@ -156,7 +156,7 @@ func Test_ParseGitUrl(t *testing.T) {
156
156
{
157
157
name : "should parse public GitLab repo with file path" ,
158
158
url : "https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/README.md" ,
159
- wantUrl : GitUrl {
159
+ wantUrl : & GitUrl {
160
160
Protocol : "https" ,
161
161
Host : "gitlab.com" ,
162
162
Owner : "gitlab-org" ,
@@ -180,7 +180,7 @@ func Test_ParseGitUrl(t *testing.T) {
180
180
{
181
181
name : "should parse private GitLab repo with token" ,
182
182
url : "https://gitlab.com/fake-owner/fake-private-repo" ,
183
- wantUrl : GitUrl {
183
+ wantUrl : & GitUrl {
184
184
Protocol : "https" ,
185
185
Host : "gitlab.com" ,
186
186
Owner : "fake-owner" ,
@@ -194,7 +194,7 @@ func Test_ParseGitUrl(t *testing.T) {
194
194
{
195
195
name : "should parse private raw GitLab file path with token" ,
196
196
url : "https://gitlab.com/fake-owner/fake-private-repo/-/raw/main/README.md" ,
197
- wantUrl : GitUrl {
197
+ wantUrl : & GitUrl {
198
198
Protocol : "https" ,
199
199
Host : "gitlab.com" ,
200
200
Owner : "fake-owner" ,
@@ -209,7 +209,7 @@ func Test_ParseGitUrl(t *testing.T) {
209
209
{
210
210
name : "should parse public Bitbucket repo with root path" ,
211
211
url : "https://bitbucket.org/fake-owner/fake-public-repo" ,
212
- wantUrl : GitUrl {
212
+ wantUrl : & GitUrl {
213
213
Protocol : "https" ,
214
214
Host : "bitbucket.org" ,
215
215
Owner : "fake-owner" ,
@@ -228,7 +228,7 @@ func Test_ParseGitUrl(t *testing.T) {
228
228
{
229
229
name : "should parse public Bitbucket repo with file path" ,
230
230
url : "https://bitbucket.org/fake-owner/fake-public-repo/src/main/README.md" ,
231
- wantUrl : GitUrl {
231
+ wantUrl : & GitUrl {
232
232
Protocol : "https" ,
233
233
Host : "bitbucket.org" ,
234
234
Owner : "fake-owner" ,
@@ -242,7 +242,7 @@ func Test_ParseGitUrl(t *testing.T) {
242
242
{
243
243
name : "should parse public Bitbucket file path with nested path" ,
244
244
url : "https://bitbucket.org/fake-owner/fake-public-repo/src/main/directory/test.txt" ,
245
- wantUrl : GitUrl {
245
+ wantUrl : & GitUrl {
246
246
Protocol : "https" ,
247
247
Host : "bitbucket.org" ,
248
248
Owner : "fake-owner" ,
@@ -256,7 +256,7 @@ func Test_ParseGitUrl(t *testing.T) {
256
256
{
257
257
name : "should parse public Bitbucket repo with raw file path" ,
258
258
url : "https://bitbucket.org/fake-owner/fake-public-repo/raw/main/README.md" ,
259
- wantUrl : GitUrl {
259
+ wantUrl : & GitUrl {
260
260
Protocol : "https" ,
261
261
Host : "bitbucket.org" ,
262
262
Owner : "fake-owner" ,
@@ -285,7 +285,7 @@ func Test_ParseGitUrl(t *testing.T) {
285
285
{
286
286
name : "should parse private Bitbucket repo with token" ,
287
287
url : "https://bitbucket.org/fake-owner/fake-private-repo" ,
288
- wantUrl : GitUrl {
288
+ wantUrl : & GitUrl {
289
289
Protocol : "https" ,
290
290
Host : "bitbucket.org" ,
291
291
Owner : "fake-owner" ,
@@ -299,7 +299,7 @@ func Test_ParseGitUrl(t *testing.T) {
299
299
{
300
300
name : "should parse private raw Bitbucket file path with token" ,
301
301
url : "https://bitbucket.org/fake-owner/fake-private-repo/raw/main/README.md" ,
302
- wantUrl : GitUrl {
302
+ wantUrl : & GitUrl {
303
303
Protocol : "https" ,
304
304
Host : "bitbucket.org" ,
305
305
Owner : "fake-owner" ,
@@ -314,7 +314,7 @@ func Test_ParseGitUrl(t *testing.T) {
314
314
315
315
for _ , tt := range tests {
316
316
t .Run (tt .name , func (t * testing.T ) {
317
- got , err := ParseGitUrl (tt .url )
317
+ got , err := NewGitUrl (tt .url )
318
318
if (err != nil ) != (tt .wantErr != "" ) {
319
319
t .Errorf ("Unxpected error: %t, want: %v" , err , tt .wantUrl )
320
320
} else if err == nil && ! reflect .DeepEqual (got , tt .wantUrl ) {
@@ -326,23 +326,63 @@ func Test_ParseGitUrl(t *testing.T) {
326
326
}
327
327
}
328
328
329
- // todo: try mocking
330
- func Test_SetToken (t * testing.T ) {
331
- g := GitUrl {
332
- Protocol : "https" ,
333
- Host : "github.com" ,
334
- Owner : "devfile" ,
335
- Repo : "library" ,
336
- Branch : "main" ,
337
- token : "" ,
329
+ func Test_GetGitRawFileAPI (t * testing.T ) {
330
+ tests := []struct {
331
+ name string
332
+ g GitUrl
333
+ want string
334
+ }{
335
+ {
336
+ name : "Github url" ,
337
+ g : GitUrl {
338
+ Protocol : "https" ,
339
+ Host : "github.com" ,
340
+ Owner : "devfile" ,
341
+ Repo : "library" ,
342
+ Branch : "main" ,
343
+ Path : "tests/README.md" ,
344
+ },
345
+ want : "https://raw.githubusercontent.com/devfile/library/main/tests/README.md" ,
346
+ },
347
+ {
348
+ name : "GitLab url" ,
349
+ g : GitUrl {
350
+ Protocol : "https" ,
351
+ Host : "gitlab.com" ,
352
+ Owner : "gitlab-org" ,
353
+ Repo : "gitlab" ,
354
+ Branch : "master" ,
355
+ Path : "README.md" ,
356
+ },
357
+ want : "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/files/README.md/raw" ,
358
+ },
359
+ {
360
+ name : "Bitbucket url" ,
361
+ g : GitUrl {
362
+ Protocol : "https" ,
363
+ Host : "bitbucket.org" ,
364
+ Owner : "owner" ,
365
+ Repo : "repo-name" ,
366
+ Branch : "main" ,
367
+ Path : "path/to/file.md" ,
368
+ },
369
+ want : "https://api.bitbucket.org/2.0/repositories/owner/repo-name/src/main/path/to/file.md" ,
370
+ },
371
+ {
372
+ name : "Empty GitUrl" ,
373
+ g : GitUrl {},
374
+ want : "" ,
375
+ },
338
376
}
339
377
340
- httpTimeout := 0
341
- token := "fake-git-token"
342
-
343
- err := g .SetToken (token , & httpTimeout )
344
- assert .NoError (t , err )
345
- assert .Equal (t , token , g .token )
378
+ for _ , tt := range tests {
379
+ t .Run (tt .name , func (t * testing.T ) {
380
+ result := tt .g .GetGitRawFileAPI ()
381
+ if ! reflect .DeepEqual (result , tt .want ) {
382
+ t .Errorf ("Got: %v, want: %v" , result , tt .want )
383
+ }
384
+ })
385
+ }
346
386
}
347
387
348
388
func Test_IsPublic (t * testing.T ) {
0 commit comments