@@ -30,11 +30,11 @@ var (
3030 bitbucketToken = "fake-bitbucket-token"
3131)
3232
33- func Test_ParseGitUrl (t * testing.T ) {
33+ func Test_NewGitUrl (t * testing.T ) {
3434 tests := []struct {
3535 name string
3636 url string
37- wantUrl GitUrl
37+ wantUrl * GitUrl
3838 wantErr string
3939 }{
4040 {
@@ -51,7 +51,7 @@ func Test_ParseGitUrl(t *testing.T) {
5151 {
5252 name : "should parse public GitHub repo with root path" ,
5353 url : "https://github.com/devfile/library" ,
54- wantUrl : GitUrl {
54+ wantUrl : & GitUrl {
5555 Protocol : "https" ,
5656 Host : "github.com" ,
5757 Owner : "devfile" ,
@@ -70,7 +70,7 @@ func Test_ParseGitUrl(t *testing.T) {
7070 {
7171 name : "should parse public GitHub repo with file path" ,
7272 url : "https://github.com/devfile/library/blob/main/devfile.yaml" ,
73- wantUrl : GitUrl {
73+ wantUrl : & GitUrl {
7474 Protocol : "https" ,
7575 Host : "github.com" ,
7676 Owner : "devfile" ,
@@ -84,7 +84,7 @@ func Test_ParseGitUrl(t *testing.T) {
8484 {
8585 name : "should parse public GitHub repo with raw file path" ,
8686 url : "https://raw.githubusercontent.com/devfile/library/main/devfile.yaml" ,
87- wantUrl : GitUrl {
87+ wantUrl : & GitUrl {
8888 Protocol : "https" ,
8989 Host : "raw.githubusercontent.com" ,
9090 Owner : "devfile" ,
@@ -108,7 +108,7 @@ func Test_ParseGitUrl(t *testing.T) {
108108 {
109109 name : "should parse private GitHub repo with token" ,
110110 url : "https://github.com/fake-owner/fake-private-repo" ,
111- wantUrl : GitUrl {
111+ wantUrl : & GitUrl {
112112 Protocol : "https" ,
113113 Host : "github.com" ,
114114 Owner : "fake-owner" ,
@@ -122,7 +122,7 @@ func Test_ParseGitUrl(t *testing.T) {
122122 {
123123 name : "should parse private raw GitHub file path with token" ,
124124 url : "https://raw.githubusercontent.com/fake-owner/fake-private-repo/main/README.md" ,
125- wantUrl : GitUrl {
125+ wantUrl : & GitUrl {
126126 Protocol : "https" ,
127127 Host : "raw.githubusercontent.com" ,
128128 Owner : "fake-owner" ,
@@ -137,7 +137,7 @@ func Test_ParseGitUrl(t *testing.T) {
137137 {
138138 name : "should parse public GitLab repo with root path" ,
139139 url : "https://gitlab.com/gitlab-org/gitlab-foss" ,
140- wantUrl : GitUrl {
140+ wantUrl : & GitUrl {
141141 Protocol : "https" ,
142142 Host : "gitlab.com" ,
143143 Owner : "gitlab-org" ,
@@ -156,7 +156,7 @@ func Test_ParseGitUrl(t *testing.T) {
156156 {
157157 name : "should parse public GitLab repo with file path" ,
158158 url : "https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/README.md" ,
159- wantUrl : GitUrl {
159+ wantUrl : & GitUrl {
160160 Protocol : "https" ,
161161 Host : "gitlab.com" ,
162162 Owner : "gitlab-org" ,
@@ -180,7 +180,7 @@ func Test_ParseGitUrl(t *testing.T) {
180180 {
181181 name : "should parse private GitLab repo with token" ,
182182 url : "https://gitlab.com/fake-owner/fake-private-repo" ,
183- wantUrl : GitUrl {
183+ wantUrl : & GitUrl {
184184 Protocol : "https" ,
185185 Host : "gitlab.com" ,
186186 Owner : "fake-owner" ,
@@ -194,7 +194,7 @@ func Test_ParseGitUrl(t *testing.T) {
194194 {
195195 name : "should parse private raw GitLab file path with token" ,
196196 url : "https://gitlab.com/fake-owner/fake-private-repo/-/raw/main/README.md" ,
197- wantUrl : GitUrl {
197+ wantUrl : & GitUrl {
198198 Protocol : "https" ,
199199 Host : "gitlab.com" ,
200200 Owner : "fake-owner" ,
@@ -209,7 +209,7 @@ func Test_ParseGitUrl(t *testing.T) {
209209 {
210210 name : "should parse public Bitbucket repo with root path" ,
211211 url : "https://bitbucket.org/fake-owner/fake-public-repo" ,
212- wantUrl : GitUrl {
212+ wantUrl : & GitUrl {
213213 Protocol : "https" ,
214214 Host : "bitbucket.org" ,
215215 Owner : "fake-owner" ,
@@ -228,7 +228,7 @@ func Test_ParseGitUrl(t *testing.T) {
228228 {
229229 name : "should parse public Bitbucket repo with file path" ,
230230 url : "https://bitbucket.org/fake-owner/fake-public-repo/src/main/README.md" ,
231- wantUrl : GitUrl {
231+ wantUrl : & GitUrl {
232232 Protocol : "https" ,
233233 Host : "bitbucket.org" ,
234234 Owner : "fake-owner" ,
@@ -242,7 +242,7 @@ func Test_ParseGitUrl(t *testing.T) {
242242 {
243243 name : "should parse public Bitbucket file path with nested path" ,
244244 url : "https://bitbucket.org/fake-owner/fake-public-repo/src/main/directory/test.txt" ,
245- wantUrl : GitUrl {
245+ wantUrl : & GitUrl {
246246 Protocol : "https" ,
247247 Host : "bitbucket.org" ,
248248 Owner : "fake-owner" ,
@@ -256,7 +256,7 @@ func Test_ParseGitUrl(t *testing.T) {
256256 {
257257 name : "should parse public Bitbucket repo with raw file path" ,
258258 url : "https://bitbucket.org/fake-owner/fake-public-repo/raw/main/README.md" ,
259- wantUrl : GitUrl {
259+ wantUrl : & GitUrl {
260260 Protocol : "https" ,
261261 Host : "bitbucket.org" ,
262262 Owner : "fake-owner" ,
@@ -285,7 +285,7 @@ func Test_ParseGitUrl(t *testing.T) {
285285 {
286286 name : "should parse private Bitbucket repo with token" ,
287287 url : "https://bitbucket.org/fake-owner/fake-private-repo" ,
288- wantUrl : GitUrl {
288+ wantUrl : & GitUrl {
289289 Protocol : "https" ,
290290 Host : "bitbucket.org" ,
291291 Owner : "fake-owner" ,
@@ -299,7 +299,7 @@ func Test_ParseGitUrl(t *testing.T) {
299299 {
300300 name : "should parse private raw Bitbucket file path with token" ,
301301 url : "https://bitbucket.org/fake-owner/fake-private-repo/raw/main/README.md" ,
302- wantUrl : GitUrl {
302+ wantUrl : & GitUrl {
303303 Protocol : "https" ,
304304 Host : "bitbucket.org" ,
305305 Owner : "fake-owner" ,
@@ -314,7 +314,7 @@ func Test_ParseGitUrl(t *testing.T) {
314314
315315 for _ , tt := range tests {
316316 t .Run (tt .name , func (t * testing.T ) {
317- got , err := ParseGitUrl (tt .url )
317+ got , err := NewGitUrl (tt .url )
318318 if (err != nil ) != (tt .wantErr != "" ) {
319319 t .Errorf ("Unxpected error: %t, want: %v" , err , tt .wantUrl )
320320 } else if err == nil && ! reflect .DeepEqual (got , tt .wantUrl ) {
@@ -326,23 +326,63 @@ func Test_ParseGitUrl(t *testing.T) {
326326 }
327327}
328328
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+ },
338376 }
339377
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+ }
346386}
347387
348388func Test_IsPublic (t * testing.T ) {
0 commit comments