Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed May 30, 2024
1 parent 2e50a99 commit b9a2066
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 29 deletions.
7 changes: 3 additions & 4 deletions cli/cage/commands/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func (c *CageCommands) Upgrade(
currVersion string,
upgrader upgrade.Upgrader,
) *cli.Command {
var preRelease bool
return &cli.Command{
Expand All @@ -20,9 +20,8 @@ func (c *CageCommands) Upgrade(
},
},
Action: func(ctx *cli.Context) error {
return upgrade.Upgrade(&upgrade.Input{
CurrentVersion: currVersion,
PreRelease: preRelease,
return upgrader.Upgrade(&upgrade.Input{
PreRelease: preRelease,
})
},
}
Expand Down
43 changes: 43 additions & 0 deletions cli/cage/commands/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package commands_test

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/loilo-inc/canarycage/cli/cage/commands"
"github.com/loilo-inc/canarycage/cli/cage/upgrade"
"github.com/loilo-inc/canarycage/mocks/mock_upgrade"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
)

func TestUpgrade(t *testing.T) {
t.Run("Upgrade", func(t *testing.T) {
app := cli.NewApp()
ctrl := gomock.NewController(t)
u := mock_upgrade.NewMockUpgrader(ctrl)
u.EXPECT().Upgrade(
gomock.Eq(&upgrade.Input{}),
).Return(nil)
cmds := commands.NewCageCommands(nil, nil)
app.Commands = []*cli.Command{
cmds.Upgrade(u),
}
err := app.Run([]string{"cage", "upgrade"})
assert.NoError(t, err)
})
t.Run("Upgrade with pre-release", func(t *testing.T) {
app := cli.NewApp()
ctrl := gomock.NewController(t)
u := mock_upgrade.NewMockUpgrader(ctrl)
u.EXPECT().Upgrade(
gomock.Eq(&upgrade.Input{PreRelease: true}),
).Return(nil)
cmds := commands.NewCageCommands(nil, nil)
app.Commands = []*cli.Command{
cmds.Upgrade(u),
}
err := app.Run([]string{"cage", "upgrade", "--pre-release"})
assert.NoError(t, err)
})
}
3 changes: 2 additions & 1 deletion cli/cage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

cage "github.com/loilo-inc/canarycage"
"github.com/loilo-inc/canarycage/cli/cage/commands"
"github.com/loilo-inc/canarycage/cli/cage/upgrade"
"github.com/urfave/cli/v2"
)

Expand All @@ -30,7 +31,7 @@ func main() {
cmds.RollOut(&envars),
cmds.Run(&envars),
cmds.Recreate(&envars),
cmds.Upgrade(version),
cmds.Upgrade(upgrade.NewUpgrader(version)),
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Expand Down
24 changes: 16 additions & 8 deletions cli/cage/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ import (
"golang.org/x/xerrors"
)

type Upgrader interface {
Upgrade(p *Input) error
}
type upgrader struct {
currentVersion string
}

type Input struct {
CurrentVersion string
PreRelease bool
TargetPath string
Os string
Arch string
PreRelease bool
TargetPath string
}

func NewUpgrader(currentVersion string) Upgrader {
return &upgrader{currentVersion: currentVersion}
}

func Upgrade(p *Input) error {
func (u *upgrader) Upgrade(p *Input) error {
cont := context.Background()
client := github.NewClient(nil)
log.Infof("checking for updates...")
Expand All @@ -53,7 +61,7 @@ func Upgrade(p *Input) error {
return xerrors.Errorf("failed to find latest release")

Check warning on line 61 in cli/cage/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

cli/cage/upgrade/upgrade.go#L61

Added line #L61 was not covered by tests
}
log.Infof("latest release: %s", latestRelease.GetTagName())
currVer, currVerErr := semver.NewVersion(p.CurrentVersion)
currVer, currVerErr := semver.NewVersion(u.currentVersion)
latestVer := semver.MustParse(latestRelease.GetTagName())
if currVerErr == nil {
if currVer.Equal(latestVer) || currVer.GreaterThan(latestVer) {
Expand All @@ -62,7 +70,7 @@ func Upgrade(p *Input) error {
}
}
// ignore current version if it's not a valid semver
log.Infof("upgrading from %s to %s", p.CurrentVersion, latestRelease.GetTagName())
log.Infof("upgrading from %s to %s", u.currentVersion, latestRelease.GetTagName())
var version = latestRelease.GetTagName()
var checksumAsset *github.ReleaseAsset
var binaryAsset *github.ReleaseAsset
Expand Down
31 changes: 15 additions & 16 deletions cli/cage/upgrade/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ func TestUpgrade(t *testing.T) {
t.Run("basic", func(t *testing.T) {
registerResponses(t, "0.1.0", "0.2.0", "0.2.1-rc1")
tmpDir := setupCurrent(t, "0.1.0")

err := upgrade.Upgrade(&upgrade.Input{
CurrentVersion: "0.1.0",
TargetPath: tmpDir + "/cage"})
u := upgrade.NewUpgrader("0.1.0")
err := u.Upgrade(&upgrade.Input{
TargetPath: tmpDir + "/cage"})
if err != nil {
t.Fatal(err)
}
Expand All @@ -112,20 +111,20 @@ func TestUpgrade(t *testing.T) {
t.Run("no updates", func(t *testing.T) {
registerResponses(t, "0.1.0")
tmpDir := setupCurrent(t, "0.1.0")
err := upgrade.Upgrade(&upgrade.Input{
CurrentVersion: "0.1.0",
TargetPath: tmpDir + "/cage",
u := upgrade.NewUpgrader("0.1.0")
err := u.Upgrade(&upgrade.Input{
TargetPath: tmpDir + "/cage",
})
assert.NoError(t, err)
assertUpgraded(t, tmpDir, "0.1.0")
})
t.Run("pre-release", func(t *testing.T) {
registerResponses(t, "0.1.0", "0.2.0", "0.2.1-rc1")
tmpDir := setupCurrent(t, "0.1.0")
err := upgrade.Upgrade(&upgrade.Input{
CurrentVersion: "0.1.0",
PreRelease: true,
TargetPath: tmpDir + "/cage"})
u := upgrade.NewUpgrader("0.1.0")
err := u.Upgrade(&upgrade.Input{
PreRelease: true,
TargetPath: tmpDir + "/cage"})
if err != nil {
t.Fatal(err)
}
Expand All @@ -134,9 +133,9 @@ func TestUpgrade(t *testing.T) {
t.Run("should upgrade if current version is not a valid semver", func(t *testing.T) {
registerResponses(t, "0.1.0", "0.2.0")
tmpDir := setupCurrent(t, "dev")
err := upgrade.Upgrade(&upgrade.Input{
CurrentVersion: "dev",
TargetPath: tmpDir + "/cage"})
u := upgrade.NewUpgrader("dev")
err := u.Upgrade(&upgrade.Input{
TargetPath: tmpDir + "/cage"})
if err != nil {
t.Fatal(err)
}
Expand All @@ -150,8 +149,8 @@ func TestUpgrade(t *testing.T) {
httpmock.NewJsonResponderOrPanic(200, makeReleases("0.1.0", "0.2.0")))
httpmock.RegisterResponder("GET", "https://localhost/0.2.0/canarycage_0.2.0_checksums.txt",
httpmock.NewStringResponder(200, "invalid"))
err := upgrade.Upgrade(&upgrade.Input{
CurrentVersion: "0.1.0"})
u := upgrade.NewUpgrader("0.1.0")
err := u.Upgrade(&upgrade.Input{})
assert.EqualError(t, err, "invalid checksum line: invalid")
})
}
49 changes: 49 additions & 0 deletions mocks/mock_upgrade/cage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b9a2066

Please sign in to comment.