Skip to content

Commit

Permalink
Add test of cmd.getLatestVersion used in bin update
Browse files Browse the repository at this point in the history
Signed-off-by: Sune Keller <absukl@almbrand.dk>
  • Loading branch information
sirlatrom committed Mar 17, 2021
1 parent c113fd3 commit 86118f8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 20 deletions.
30 changes: 10 additions & 20 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,24 @@ func newUpdateCmd() *updateCmd {
cfg := config.Get()

// Update single binary
binsToProcess := cfg.Bins
if bin != "" {
bin, err := getBinPath(bin)
if err != nil {
return err
}

b := cfg.Bins[bin]

if ui, err := getLatestVersion(b); err != nil {
binsToProcess = map[string]*config.Binary{bin: cfg.Bins[bin]}
}
for _, b := range binsToProcess {
p, err := providers.New(b.URL, b.Provider)
if err != nil {
return err
}
if ui, err := getLatestVersion(b, p); err != nil {
return err
} else if ui != nil {
toUpdate[ui] = b
}

} else {
for _, b := range cfg.Bins {
if ui, err := getLatestVersion(b); err != nil {
return err
} else if ui != nil {
toUpdate[ui] = b
}
}
}

if len(toUpdate) == 0 {
Expand Down Expand Up @@ -141,13 +137,7 @@ func newUpdateCmd() *updateCmd {
return root
}

func getLatestVersion(b *config.Binary) (*updateInfo, error) {
p, err := providers.New(b.URL, b.Provider)

if err != nil {
return nil, err
}

func getLatestVersion(b *config.Binary, p providers.Provider) (*updateInfo, error) {
log.Debugf("Checking updates for %s", b.Path)
v, u, err := p.GetLatestVersion()

Expand Down
73 changes: 73 additions & 0 deletions cmd/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"reflect"
"testing"

"github.com/marcosnils/bin/pkg/config"
"github.com/marcosnils/bin/pkg/providers"
)

type mockProvider struct {
providers.Provider
latestVersion string
latestVersionURL string
err error
}

func (m mockProvider) GetLatestVersion() (string, string, error) {
return m.latestVersion, m.latestVersionURL, m.err
}

func (m mockProvider) GetID() string {
return "github"
}

func TestGetLatestVersion(t *testing.T) {
type mockValues struct {
latestVersion string
latestVersionURL string
err error
}
cases := []struct {
in *config.Binary
m mockValues
out *updateInfo
}{
{
&config.Binary{
Path: "/home/user/bin/launchpad",
Version: "1.1.0",
URL: "https://github.com/Mirantis/launchpad/releases/download/1.1.0/launchpad-linux-x64",
RemoteName: "launchpad-linux-x64",
Provider: "github",
},
mockValues{"1.1.1", "https://github.com/Mirantis/launchpad/releases/download/1.1.1/launchpad-linux-x64", nil},
&updateInfo{
version: "1.1.1",
url: "https://github.com/Mirantis/launchpad/releases/download/1.1.1/launchpad-linux-x64",
},
},
{
&config.Binary{
Path: "/home/user/bin/launchpad",
Version: "1.2.0-rc.1",
URL: "https://github.com/Mirantis/launchpad/releases/download/1.2.0-rc.1/launchpad-linux-x64",
RemoteName: "launchpad-linux-x64",
Provider: "github",
},
mockValues{"1.1.1", "https://github.com/Mirantis/launchpad/releases/download/1.1.1/launchpad-linux-x64", nil},
nil,
},
}

for _, c := range cases {
p := mockProvider{latestVersion: c.m.latestVersion, latestVersionURL: c.m.latestVersionURL, err: c.m.err}
if v, err := getLatestVersion(c.in, p); err != nil {
t.Fatalf("Error during getLatestVersion(%#v, %#v): %v", c.in, p, err)
} else if !reflect.DeepEqual(v, c.out) {
t.Fatalf("For case %#v: %#v does not match %#v", c.in, v, c.out)
}
}

}

0 comments on commit 86118f8

Please sign in to comment.