Skip to content

Commit

Permalink
add unit test for downloader methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Kanchwala committed Oct 2, 2020
1 parent 58d4745 commit e72bf36
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions pkg/downloader/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package downloader

import (
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -131,3 +132,115 @@ func TestGetURLSubDir(t *testing.T) {
}
}
}

func TestDownload(t *testing.T) {

table := []struct {
name string
URL string
dest string
wantDest string
wantErr error
}{
{
name: "empty URL",
URL: "",
dest: "some-destination",
wantDest: "",
wantErr: ErrEmptyURLDest,
},
{
name: "empty destination",
URL: "some-url",
dest: "",
wantDest: "",
wantErr: ErrEmptyURLDest,
},
{
name: "invalid url",
URL: "github.com/some-repo",
dest: "some-dest",
wantDest: "",
wantErr: fmt.Errorf("GitHub URLs should be github.com/username/repo"),
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
g := NewGoGetter()
gotDest, gotErr := g.Download(tt.URL, tt.dest)
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("error got: '%v', want: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(gotDest, tt.wantDest) {
t.Errorf("dest got: '%v', want: '%v'", gotDest, tt.wantDest)
}
})
}
}

func TestDownloadWithType(t *testing.T) {

table := []struct {
name string
Type string
URL string
dest string
wantDest string
wantErr error
}{
{
name: "empty URL and Type",
Type: "",
URL: "",
dest: "some-destination",
wantDest: "",
wantErr: ErrEmptyURLType,
},
{
name: "empty URL",
Type: "some-type",
URL: "",
dest: "some-destination",
wantDest: "",
wantErr: ErrEmptyURLDest,
},
{
name: "empty Type",
Type: "",
URL: "some-url",
dest: "some-destination",
wantDest: "",
wantErr: ErrEmptyURLDest,
},
{
name: "empty dest",
Type: "some-type",
URL: "some-url",
dest: "",
wantDest: "",
wantErr: ErrEmptyURLDest,
},
{
name: "invalid url",
Type: "some-type",
URL: "github.com/some-url",
dest: "some-dest",
wantDest: "",
wantErr: fmt.Errorf("download not supported for scheme 'some-type'"),
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
g := NewGoGetter()
gotDest, gotErr := g.DownloadWithType(tt.Type, tt.URL, tt.dest)
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("error got: '%v', want: '%v'", gotErr, tt.wantErr)
}
if !reflect.DeepEqual(gotDest, tt.wantDest) {
t.Errorf("dest got: '%v', want: '%v'", gotDest, tt.wantDest)
}
})
}
}

0 comments on commit e72bf36

Please sign in to comment.