Skip to content

Commit

Permalink
Merge pull request #1 from interpeix/mock-resolver
Browse files Browse the repository at this point in the history
Mock resolver
  • Loading branch information
interpeix authored Mar 9, 2021
2 parents d543fff + 1478470 commit 619550c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
21 changes: 19 additions & 2 deletions pkg/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ type FilteredAsset struct {
score int
}

type platformResolver interface {
GetOS() []string
GetArch() []string
}

type runtimeResolver struct{}

func (runtimeResolver) GetOS() []string {
return config.GetOS()
}

func (runtimeResolver) GetArch() []string {
return config.GetArch()
}

var resolver platformResolver = runtimeResolver{}

func (g FilteredAsset) String() string { return g.Name }

// FilterAssets receives a slice of GL assets and tries to
Expand All @@ -48,7 +65,7 @@ func FilterAssets(repoName string, as []*Asset) (*FilteredAsset, error) {
matches := []*FilteredAsset{}
scores := map[string]int{}
scores[repoName] = 1
for _, os := range config.GetOS() {
for _, os := range resolver.GetOS() {
scores[os] = 10
}
for _, arch := range config.GetArch() {
Expand Down Expand Up @@ -128,7 +145,7 @@ func SanitizeName(name, version string) string {
// TODO maybe instead of doing this put everything in a map (set) and then
// generate the replacements? IDK.
firstPass := true
for _, osName := range config.GetOS() {
for _, osName := range resolver.GetOS() {
for _, archName := range config.GetArch() {
replacements = append(replacements, "_"+osName+archName, "")
replacements = append(replacements, "-"+osName+archName, "")
Expand Down
59 changes: 44 additions & 15 deletions pkg/assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@ import (
"testing"
)

type mockOSResolver struct {
OS []string
Arch []string
}

func (m *mockOSResolver) GetOS() []string {
return m.OS
}

func (m *mockOSResolver) GetArch() []string {
return m.Arch
}

func TestSanitizeName(t *testing.T) {
linuxAMDResolver := &mockOSResolver{OS: []string{"linux"}, Arch: []string{"amd64"}}
windowsAMDResolver := &mockOSResolver{OS: []string{"windows"}, Arch: []string{"amd64"}}
cases := []struct {
in string
v string
out string
resolver platformResolver
}{
{"bin_amd64_linux", "v0.0.1", "bin"},
{"bin_0.0.1_amd64_linux", "0.0.1", "bin"},
{"bin_0.0.1_amd64_linux", "v0.0.1", "bin"},
{"gitlab-runner-linux-amd64", "v13.2.1", "gitlab-runner"},
{"jq-linux64", "jq-1.5", "jq"},
{"bin_amd64_linux", "v0.0.1", "bin",linuxAMDResolver},
{"bin_0.0.1_amd64_linux", "0.0.1", "bin",linuxAMDResolver},
{"bin_0.0.1_amd64_linux", "v0.0.1", "bin",linuxAMDResolver},
{"gitlab-runner-linux-amd64", "v13.2.1", "gitlab-runner",linuxAMDResolver},
{"jq-linux64", "jq-1.5", "jq",linuxAMDResolver},
{"bin_0.0.1_Windows_x86_64.exe","0.0.1","bin.exe",windowsAMDResolver},
}

for _, c := range cases {
resolver = c.resolver
if n := SanitizeName(c.in, c.v); n != c.out {
t.Fatalf("Error replacing %s: %s does not match %s", c.in, n, c.out)
}
Expand All @@ -26,49 +44,60 @@ func TestSanitizeName(t *testing.T) {
}

func TestFilterAssets(t *testing.T) {
linuxAMDResolver := &mockOSResolver{OS: []string{"linux"}, Arch: []string{"amd64"}}
windowsAMDResolver := &mockOSResolver{OS: []string{"windows"}, Arch: []string{"amd64"}}
type args struct {
repoName string
as []*Asset
}
cases := []struct {
in args
out string
in args
out string
resolver platformResolver
}{
{args{"bin", []*Asset{
{Name: "bin_0.0.1_Linux_x86_64", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.0.1_Linux_x86_64"},
{Name: "bin_0.0.1_Linux_i386", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.0.1_Linux_i386"},
{Name: "bin_0.0.1_Darwin_x86_64", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.0.1_Darwin_x86_64"},
}}, "bin_0.0.1_linux_x86_64"},
}}, "bin_0.0.1_linux_x86_64", linuxAMDResolver},
{args{"bin", []*Asset{
{Name: "bin_0.1.0_Windows_i386.exe", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.1.0_Windows_i386.exe"},
{Name: "bin_0.1.0_Linux_x86_64", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.1.0_Linux_x86_64"},
{Name: "bin_0.1.0_Darwin_x86_64", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.1.0_Darwin_x86_64"},
}}, "bin_0.1.0_linux_x86_64"},
}}, "bin_0.1.0_linux_x86_64", linuxAMDResolver},
{args{"bin", []*Asset{
{Name: "bin_0.1.0_Windows_i386.exe", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.1.0_Windows_i386.exe"},
{Name: "bin_0.1.0_Linux_x86_64", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.1.0_Linux_x86_64"},
{Name: "bin_0.1.0_Darwin_x86_64", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.1.0_Darwin_x86_64"},
}}, "bin_0.1.0_linux_x86_64"},
}}, "bin_0.1.0_linux_x86_64", linuxAMDResolver},
{args{"gitlab-runner", []*Asset{
{Name: "Windows 64 bits", URL: "https://gitlab-runner-downloads.s3.amazonaws.com/v13.2.1/binaries/gitlab-runner-windows-amd64.zip"},
{Name: "linux amd64", URL: "https://gitlab-runner-downloads.s3.amazonaws.com/v13.2.1/binaries/gitlab-runner-linux-amd64"},
{Name: "macOS", URL: "https://gitlab-runner-downloads.s3.amazonaws.com/v13.2.1/binaries/gitlab-runner-darwin-amd64"},
}}, "gitlab-runner-linux-amd64"},
}}, "gitlab-runner-linux-amd64", linuxAMDResolver},
{args{"yq", []*Asset{
{Name: "yq_freebsd_amd64", URL: "https://github.com/mikefarah/yq/releases/download/3.3.2/yq_freebsd_amd64"},
{Name: "yq_linux_amd64", URL: "https://github.com/mikefarah/yq/releases/download/3.3.2/yq_linux_amd64"},
{Name: "yq_windows_amd64.exe", URL: "https://github.com/mikefarah/yq/releases/download/3.3.2/yq_windows_amd64.exe"},
}}, "yq_linux_amd64"},
}}, "yq_linux_amd64", linuxAMDResolver},
{args{"jq", []*Asset{
{Name: "jq-win64.exe", URL: "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-win64.exe"},
{Name: "jq-linux64", URL: "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64"},
{Name: "jq-osx-amd64", URL: "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64"},
}}, "jq-linux64"},
}}, "jq-linux64", linuxAMDResolver},
{args{"bin", []*Asset{
{Name: "bin_0.0.1_Windows_x86_64.exe", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.0.1_Windows_x86_64.exe"},
{Name: "bin_0.1.0_Linux_x86_64", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.1.0_Linux_x86_64"},
{Name: "bin_0.1.0_Darwin_x86_64", URL: "https://github.com/marcosnils/bin/releases/download/v0.0.1/bin_0.1.0_Darwin_x86_64"},
}}, "bin_0.0.1_windows_x86_64.exe", windowsAMDResolver},
}

for _, c := range cases {
if n, _ := FilterAssets(c.in.repoName, c.in.as); n.Name != c.out {
t.Fatalf("Error filtering %+v: %+v does not match %s", c.in, n, c.out)
resolver = c.resolver
if n, err := FilterAssets(c.in.repoName, c.in.as); err != nil {
t.Fatalf("Error filtering assets %v", err)
} else if n.Name != c.out {
t.Fatalf("Error filtering %+v: %+v does not match %s or error %v", c.in, n, c.out, err)
}
}

Expand Down

0 comments on commit 619550c

Please sign in to comment.