Skip to content

Commit

Permalink
tests: add unit tests for the vulcan package
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 11, 2024
1 parent 8fa3f7b commit f72415c
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/build/vulcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func vulcan(cmd *BuildCmd, conf *contracts.AzionApplicationOptions, vulcanParams
}

vul := vulcanPkg.NewVulcan()
err = vul.CheckVulcanMajor(vulcanVer, cmd.f)
err = vul.CheckVulcanMajor(vulcanVer, cmd.f, vul)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/init/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (cmd *initCmd) selectVulcanTemplates() error {
}

vul := vulcanPkg.NewVulcan()
err = vul.CheckVulcanMajor(vulcanVer, cmd.f)
err = vul.CheckVulcanMajor(vulcanVer, cmd.f, vul)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/link/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (cmd *LinkCmd) selectVulcanMode(info *LinkInfo) error {
}

vul := vulcanPkg.NewVulcan()
err = vul.CheckVulcanMajor(vulcanVer, cmd.F)
err = vul.CheckVulcanMajor(vulcanVer, cmd.F, vul)
if err != nil {
return err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/vulcan/vulcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ var versionVulcan = "@latest"

type VulcanPkg struct {
Command func(flags, params string, f *cmdutil.Factory) string
CheckVulcanMajor func(currentVersion string, f *cmdutil.Factory) error
CheckVulcanMajor func(currentVersion string, f *cmdutil.Factory, vulcan *VulcanPkg) error
ReadSettings func() (token.Settings, error)
}

func NewVulcan() *VulcanPkg {
return &VulcanPkg{
Command: command,
CheckVulcanMajor: checkVulcanMajor,
ReadSettings: token.ReadSettings,
}
}

Expand All @@ -41,8 +43,15 @@ func command(flags, params string, f *cmdutil.Factory) string {
return fmt.Sprintf(installEdgeFunctions, flags, versionVulcan, params)
}

func checkVulcanMajor(currentVersion string, f *cmdutil.Factory) error {
func checkVulcanMajor(currentVersion string, f *cmdutil.Factory, vulcan *VulcanPkg) error {
parts := strings.Split(currentVersion, ".")
// strings.Split will always return at least one element, so parts will always be len>1
// to avoid this, I am checking if version is empty. If so, I just use an empty slice
if currentVersion == "" {
parts = []string{}
}
fmt.Println(parts)
fmt.Println(len(parts))

// Extract the first part and convert it to a number
if len(parts) > 0 {
Expand All @@ -51,7 +60,7 @@ func checkVulcanMajor(currentVersion string, f *cmdutil.Factory) error {
return err
}

config, err := token.ReadSettings()
config, err := vulcan.ReadSettings()
if err != nil {
return err
}
Expand Down
138 changes: 128 additions & 10 deletions pkg/vulcan/vulcan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,83 @@ import (
"fmt"
"testing"

"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/testutils"
"github.com/aziontech/azion-cli/pkg/token"
"go.uber.org/zap/zapcore"
)

func TestCommand(t *testing.T) {
logger.New(zapcore.DebugLevel)
type args struct {
flags string
params string
}
tests := []struct {
name string
args args
want string
name string
args args
debug bool
want string
}{
{
name: "no flags",
name: "no flags - debug off",
args: args{
params: "presets ls",
},
want: fmt.Sprintf("npx --yes edge-functions%s presets ls", versionVulcan),
debug: false,
want: fmt.Sprintf("npx --yes edge-functions%s presets ls", versionVulcan),
},
{
name: "with flags",
name: "with flags - debug off",
args: args{
flags: "--loglevel=error --no-update-notifier",
params: "presets ls",
},
want: fmt.Sprintf("npx --yes --loglevel=error --no-update-notifier edge-functions%s presets ls", versionVulcan),
debug: false,
want: fmt.Sprintf("npx --yes --loglevel=error --no-update-notifier edge-functions%s presets ls", versionVulcan),
},
{
name: "no params",
name: "no params - debug off",
args: args{
flags: "--loglevel=error --no-update-notifier",
},
want: fmt.Sprintf("npx --yes --loglevel=error --no-update-notifier edge-functions%s ", versionVulcan),
debug: false,
want: fmt.Sprintf("npx --yes --loglevel=error --no-update-notifier edge-functions%s ", versionVulcan),
},
{
name: "no flags - debug on",
args: args{
params: "presets ls",
},
debug: true,
want: fmt.Sprintf("DEBUG=true npx --yes edge-functions%s presets ls", versionVulcan),
},
{
name: "with flags - debug on",
args: args{
flags: "--loglevel=error --no-update-notifier",
params: "presets ls",
},
debug: true,
want: fmt.Sprintf("DEBUG=true npx --yes --loglevel=error --no-update-notifier edge-functions%s presets ls", versionVulcan),
},
{
name: "no params - debug on",
args: args{
flags: "--loglevel=error --no-update-notifier",
},
debug: true,
want: fmt.Sprintf("DEBUG=true npx --yes --loglevel=error --no-update-notifier edge-functions%s ", versionVulcan),
},
}
f, _, _ := testutils.NewFactory(nil)
for _, tt := range tests {
logger.New(zapcore.DebugLevel)
mock := &httpmock.Registry{}
f, _, _ := testutils.NewFactory(mock)
if tt.debug {
f.Logger.Debug = true
}
vul := NewVulcan()
t.Run(tt.name, func(t *testing.T) {
if got := vul.Command(tt.args.flags, tt.args.params, f); got != tt.want {
Expand All @@ -50,3 +89,82 @@ func TestCommand(t *testing.T) {
})
}
}

func TestCheckVulcanMajor(t *testing.T) {
type args struct {
currentVersion string
}
tests := []struct {
name string
args args
lastVulcanVer string
expectedVersion string
wantErr bool
err string
}{
{
name: "new major version without last version",
args: args{
currentVersion: "3.0.0",
},
lastVulcanVer: "",
expectedVersion: firstTimeExecuting,
wantErr: false,
},
{
name: "new major version with last version",
args: args{
currentVersion: "3.0.0",
},
lastVulcanVer: "2.5.0",
expectedVersion: "@v2.5.0",
wantErr: false,
},
{
name: "same major version",
args: args{
currentVersion: "2.0.0",
},
lastVulcanVer: "2.5.0",
expectedVersion: "@v2.5.0",
wantErr: false,
},
{
name: "failed to parse version",
args: args{
currentVersion: "invalid",
},
expectedVersion: firstTimeExecuting,
wantErr: true,
err: "strconv.Atoi: parsing \"invalid\": invalid syntax",
},
{
name: "empty version string",
args: args{
currentVersion: "",
},
lastVulcanVer: "2.5.0",
expectedVersion: firstTimeExecuting,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, _, _ := testutils.NewFactory(nil)
vul := NewVulcan()
vul.ReadSettings = func() (token.Settings, error) {
return token.Settings{}, nil
}
err := vul.CheckVulcanMajor(tt.args.currentVersion, f, vul)
if (err != nil) != tt.wantErr {
t.Errorf("CheckVulcanMajor() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil && err.Error() != tt.err {
t.Errorf("CheckVulcanMajor() error = %v, wantErr %v", err, tt.wantErr)
}
if versionVulcan != tt.expectedVersion {
t.Errorf("versionVulcan = %v, expectedVersion %v", versionVulcan, tt.expectedVersion)
}
})
}
}

0 comments on commit f72415c

Please sign in to comment.