From 3953c4368fbb1748058c55681fd30278fbabe2a3 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 07:39:07 +0000 Subject: [PATCH 1/8] chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.3 Bumps [github.com/pelletier/go-toml/v2](https://github.com/pelletier/go-toml) from 2.2.2 to 2.2.3. - [Release notes](https://github.com/pelletier/go-toml/releases) - [Changelog](https://github.com/pelletier/go-toml/blob/v2/.goreleaser.yaml) - [Commits](https://github.com/pelletier/go-toml/compare/v2.2.2...v2.2.3) --- updated-dependencies: - dependency-name: github.com/pelletier/go-toml/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index ab2c06482..cd38bce4a 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/machinebox/graphql v0.2.2 github.com/manifoldco/promptui v0.9.0 github.com/pelletier/go-toml v1.9.5 - github.com/pelletier/go-toml/v2 v2.2.2 + github.com/pelletier/go-toml/v2 v2.2.3 github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 github.com/schollz/progressbar/v3 v3.14.6 diff --git a/go.sum b/go.sum index c8f1e8686..a18e96ca2 100644 --- a/go.sum +++ b/go.sum @@ -212,8 +212,8 @@ github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= -github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= -github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -263,16 +263,10 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= From 37eeb6c79a0b7ce0b8d3a2d7d0f9cebbbfed5097 Mon Sep 17 00:00:00 2001 From: maxwelbm Date: Wed, 4 Sep 2024 16:58:08 -0300 Subject: [PATCH 2/8] test: test init success file vulcan.go --- pkg/cmd/build/build.go | 4 +- pkg/cmd/build/run.go | 18 ++--- pkg/cmd/build/utils.go | 16 ++--- pkg/cmd/build/vulcan.go | 21 +++--- pkg/cmd/build/vulcan_test.go | 125 +++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 31 deletions(-) create mode 100644 pkg/cmd/build/vulcan_test.go diff --git a/pkg/cmd/build/build.go b/pkg/cmd/build/build.go index 717faa48e..4fc9aca86 100644 --- a/pkg/cmd/build/build.go +++ b/pkg/cmd/build/build.go @@ -84,7 +84,7 @@ func NewBuildCmd(f *cmdutil.Factory) *BuildCmd { } } -func (cmd *BuildCmd) ExternalRun(fields *contracts.BuildInfo, confPath string, msgs *[]string) error { +func (b *BuildCmd) ExternalRun(fields *contracts.BuildInfo, confPath string, msgs *[]string) error { fields.ProjectPath = confPath - return cmd.run(fields, msgs) + return b.run(fields, msgs) } diff --git a/pkg/cmd/build/run.go b/pkg/cmd/build/run.go index cc40807c9..ca701ff26 100644 --- a/pkg/cmd/build/run.go +++ b/pkg/cmd/build/run.go @@ -7,24 +7,16 @@ import ( msg "github.com/aziontech/azion-cli/messages/build" "github.com/aziontech/azion-cli/pkg/contracts" "github.com/aziontech/azion-cli/pkg/logger" + vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan" "go.uber.org/zap" ) -func (cmd *BuildCmd) run(fields *contracts.BuildInfo, msgs *[]string) error { +func (b *BuildCmd) run(fields *contracts.BuildInfo, msgs *[]string) error { logger.Debug("Running build command") - err := RunBuildCmdLine(cmd, fields, msgs) - if err != nil { - return err - } - - return nil -} - -func RunBuildCmdLine(cmd *BuildCmd, fields *contracts.BuildInfo, msgs *[]string) error { var err error - conf, err := cmd.GetAzionJsonContent(fields.ProjectPath) + conf, err := b.GetAzionJsonContent(fields.ProjectPath) if err != nil { logger.Debug("Error while building your project", zap.Error(err)) return msg.ErrorBuilding @@ -66,6 +58,6 @@ func RunBuildCmdLine(cmd *BuildCmd, fields *contracts.BuildInfo, msgs *[]string) vulcanParams += " --firewall " } - return vulcan(cmd, conf, vulcanParams, fields, msgs) - + vul := vulcanPkg.NewVulcan() + return b.vulcan(vul, conf, vulcanParams, fields, msgs) } diff --git a/pkg/cmd/build/utils.go b/pkg/cmd/build/utils.go index 68a5e66a2..cdcd7c655 100644 --- a/pkg/cmd/build/utils.go +++ b/pkg/cmd/build/utils.go @@ -9,27 +9,27 @@ import ( "go.uber.org/zap" ) -func runCommand(cmd *BuildCmd, command string, msgs *[]string) error { +func (b *BuildCmd) runCommand(command string, msgs *[]string) error { var hasDeployMessage bool if len(*msgs) > 0 { hasDeployMessage = true } - logger.FInfoFlags(cmd.Io.Out, msg.BuildStart, cmd.f.Format, cmd.f.Out) + logger.FInfoFlags(b.Io.Out, msg.BuildStart, b.f.Format, b.f.Out) *msgs = append(*msgs, msg.BuildStart) - logger.FInfoFlags(cmd.Io.Out, msg.BuildRunningCmd, cmd.f.Format, cmd.f.Out) + logger.FInfoFlags(b.Io.Out, msg.BuildRunningCmd, b.f.Format, b.f.Out) *msgs = append(*msgs, msg.BuildRunningCmd) - logger.FInfoFlags(cmd.Io.Out, fmt.Sprintf("$ %s\n", command), cmd.f.Format, cmd.f.Out) + logger.FInfoFlags(b.Io.Out, fmt.Sprintf("$ %s\n", command), b.f.Format, b.f.Out) *msgs = append(*msgs, fmt.Sprintf("$ %s\n", command)) - err := cmd.CommandRunInteractive(cmd.f, command) + err := b.CommandRunInteractive(b.f, command) if err != nil { logger.Debug("Error while running command with simultaneous output", zap.Error(err)) return msg.ErrFailedToRunBuildCommand } - logger.FInfoFlags(cmd.Io.Out, msg.BuildSuccessful, cmd.f.Format, cmd.f.Out) + logger.FInfoFlags(b.Io.Out, msg.BuildSuccessful, b.f.Format, b.f.Out) *msgs = append(*msgs, msg.BuildSuccessful) if hasDeployMessage { @@ -39,8 +39,8 @@ func runCommand(cmd *BuildCmd, command string, msgs *[]string) error { outSlice := output.SliceOutput{ Messages: *msgs, GeneralOutput: output.GeneralOutput{ - Out: cmd.f.IOStreams.Out, - Flags: cmd.f.Flags, + Out: b.f.IOStreams.Out, + Flags: b.f.Flags, }, } diff --git a/pkg/cmd/build/vulcan.go b/pkg/cmd/build/vulcan.go index efe19cbf0..1b0c11f18 100644 --- a/pkg/cmd/build/vulcan.go +++ b/pkg/cmd/build/vulcan.go @@ -11,27 +11,32 @@ import ( "go.uber.org/zap" ) -func vulcan(cmd *BuildCmd, conf *contracts.AzionApplicationOptions, vulcanParams string, fields *contracts.BuildInfo, msgs *[]string) error { +func (b *BuildCmd) vulcan( + vul *vulcanPkg.VulcanPkg, + conf *contracts.AzionApplicationOptions, + vulcanParams string, + fields *contracts.BuildInfo, + msgs *[]string, +) error { // checking if vulcan major is correct - vulcanVer, err := cmd.CommandRunner(cmd.f, "npm show edge-functions version", []string{}) + vulcanVer, err := b.CommandRunner(b.f, "npm show edge-functions version", []string{}) if err != nil { return err } - vul := vulcanPkg.NewVulcan() - err = vul.CheckVulcanMajor(vulcanVer, cmd.f, vul) + err = vul.CheckVulcanMajor(vulcanVer, b.f, vul) if err != nil { return err } - command := vul.Command("", "build%s", cmd.f) - - err = runCommand(cmd, fmt.Sprintf(command, vulcanParams), msgs) + command := vul.Command("", "build%s", b.f) + vulcanCommand := fmt.Sprintf(command, vulcanParams) + err = b.runCommand(vulcanCommand, msgs) if err != nil { return fmt.Errorf(msg.ErrorVulcanExecute.Error(), err.Error()) } - err = cmd.WriteAzionJsonContent(conf, fields.ProjectPath) + err = b.WriteAzionJsonContent(conf, fields.ProjectPath) if err != nil { logger.Debug("Error while writing azion.json file", zap.Error(err)) return utils.ErrorWritingAzionJsonFile diff --git a/pkg/cmd/build/vulcan_test.go b/pkg/cmd/build/vulcan_test.go new file mode 100644 index 000000000..20cd8964b --- /dev/null +++ b/pkg/cmd/build/vulcan_test.go @@ -0,0 +1,125 @@ +package build + +import ( + "fmt" + "io" + "io/fs" + "testing" + + "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/contracts" + "github.com/aziontech/azion-cli/pkg/iostreams" + "github.com/aziontech/azion-cli/pkg/logger" + vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan" + "go.uber.org/zap/zapcore" +) + +func TestBuildCmd_vulcan(t *testing.T) { + logger.New(zapcore.DebugLevel) + + type fields struct { + Io *iostreams.IOStreams + WriteFile func(filename string, data []byte, perm fs.FileMode) error + CommandRunnerStream func(out io.Writer, cmd string, envvars []string) error + CommandRunInteractive func(f *cmdutil.Factory, comm string) error + CommandRunner func(f *cmdutil.Factory, comm string, envVars []string) (string, error) + FileReader func(path string) ([]byte, error) + GetAzionJsonContent func(pathConf string) (*contracts.AzionApplicationOptions, error) + WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions, confPath string) error + EnvLoader func(path string) ([]string, error) + Stat func(path string) (fs.FileInfo, error) + GetWorkDir func() (string, error) + f *cmdutil.Factory + } + + type args struct { + vul *vulcanPkg.VulcanPkg + conf *contracts.AzionApplicationOptions + vulcanParams string + fields *contracts.BuildInfo + msgs *[]string + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "flow completed with success", + fields: fields{ + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + }, + args: args{ + vul: &vulcanPkg.VulcanPkg{ + CheckVulcanMajor: func( + currentVersion string, + f *cmdutil.Factory, + vulcan *vulcanPkg.VulcanPkg, + ) error { + return nil + }, + Command: func( + flags, params string, + f *cmdutil.Factory, + ) string { + installEdgeFunctions := "npx --yes %s edge-functions%s %s" + versionVulcan := "@3.2.1" + return fmt.Sprintf( + installEdgeFunctions, + flags, + versionVulcan, + params, + ) + }, + }, + conf: &contracts.AzionApplicationOptions{}, + vulcanParams: "", + fields: &contracts.BuildInfo{}, + msgs: &[]string{}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &BuildCmd{ + Io: tt.fields.Io, + WriteFile: tt.fields.WriteFile, + CommandRunnerStream: tt.fields.CommandRunnerStream, + CommandRunInteractive: tt.fields.CommandRunInteractive, + CommandRunner: tt.fields.CommandRunner, + FileReader: tt.fields.FileReader, + GetAzionJsonContent: tt.fields.GetAzionJsonContent, + WriteAzionJsonContent: tt.fields.WriteAzionJsonContent, + EnvLoader: tt.fields.EnvLoader, + Stat: tt.fields.Stat, + GetWorkDir: tt.fields.GetWorkDir, + f: tt.fields.f, + } + if err := b.vulcan(tt.args.vul, + tt.args.conf, + tt.args.vulcanParams, + tt.args.fields, + tt.args.msgs, + ); (err != nil) != tt.wantErr { + t.Errorf("BuildCmd.vulcan() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 0e71621d9931af6b7dc818b7133ff7080c598d8c Mon Sep 17 00:00:00 2001 From: maxwelbm Date: Wed, 4 Sep 2024 18:46:03 -0300 Subject: [PATCH 3/8] test: build vulcan test completed --- pkg/cmd/build/utils.go | 10 +- pkg/cmd/build/vulcan.go | 3 +- pkg/cmd/build/vulcan_test.go | 251 +++++++++++++++++++++++++++++++++++ 3 files changed, 261 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/build/utils.go b/pkg/cmd/build/utils.go index cdcd7c655..81ce48cd5 100644 --- a/pkg/cmd/build/utils.go +++ b/pkg/cmd/build/utils.go @@ -15,7 +15,15 @@ func (b *BuildCmd) runCommand(command string, msgs *[]string) error { hasDeployMessage = true } - logger.FInfoFlags(b.Io.Out, msg.BuildStart, b.f.Format, b.f.Out) + out := b.Io.Out + format := b.f.Format + + fmt.Println() + fmt.Println() + + outS := b.f.Out + + logger.FInfoFlags(out, msg.BuildStart, format, outS) *msgs = append(*msgs, msg.BuildStart) logger.FInfoFlags(b.Io.Out, msg.BuildRunningCmd, b.f.Format, b.f.Out) diff --git a/pkg/cmd/build/vulcan.go b/pkg/cmd/build/vulcan.go index 1b0c11f18..5e156a3f0 100644 --- a/pkg/cmd/build/vulcan.go +++ b/pkg/cmd/build/vulcan.go @@ -30,8 +30,7 @@ func (b *BuildCmd) vulcan( } command := vul.Command("", "build%s", b.f) - vulcanCommand := fmt.Sprintf(command, vulcanParams) - err = b.runCommand(vulcanCommand, msgs) + err = b.runCommand(fmt.Sprintf(command, vulcanParams), msgs) if err != nil { return fmt.Errorf(msg.ErrorVulcanExecute.Error(), err.Error()) } diff --git a/pkg/cmd/build/vulcan_test.go b/pkg/cmd/build/vulcan_test.go index 20cd8964b..b09169ceb 100644 --- a/pkg/cmd/build/vulcan_test.go +++ b/pkg/cmd/build/vulcan_test.go @@ -1,6 +1,7 @@ package build import ( + "errors" "fmt" "io" "io/fs" @@ -39,6 +40,7 @@ func TestBuildCmd_vulcan(t *testing.T) { fields *contracts.BuildInfo msgs *[]string } + tests := []struct { name string fields fields @@ -65,6 +67,254 @@ func TestBuildCmd_vulcan(t *testing.T) { ) error { return nil }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + vul: &vulcanPkg.VulcanPkg{ + CheckVulcanMajor: func( + currentVersion string, + f *cmdutil.Factory, + vulcan *vulcanPkg.VulcanPkg, + ) error { + return nil + }, + Command: func( + flags, params string, + f *cmdutil.Factory, + ) string { + installEdgeFunctions := "npx --yes %s edge-functions%s %s" + versionVulcan := "@3.2.1" + return fmt.Sprintf( + installEdgeFunctions, + flags, + versionVulcan, + params, + ) + }, + }, + conf: &contracts.AzionApplicationOptions{}, + vulcanParams: "", + fields: &contracts.BuildInfo{}, + msgs: &[]string{}, + }, + }, + { + name: "error get version on edge-functions", + fields: fields{ + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", errors.New("Error Get version edge-functions") + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + vul: &vulcanPkg.VulcanPkg{ + CheckVulcanMajor: func( + currentVersion string, + f *cmdutil.Factory, + vulcan *vulcanPkg.VulcanPkg, + ) error { + return nil + }, + Command: func( + flags, params string, + f *cmdutil.Factory, + ) string { + installEdgeFunctions := "npx --yes %s edge-functions%s %s" + versionVulcan := "@3.2.1" + return fmt.Sprintf( + installEdgeFunctions, + flags, + versionVulcan, + params, + ) + }, + }, + conf: &contracts.AzionApplicationOptions{}, + vulcanParams: "", + fields: &contracts.BuildInfo{}, + msgs: &[]string{}, + }, + wantErr: true, + }, + { + name: "error check vulcan major", + fields: fields{ + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + vul: &vulcanPkg.VulcanPkg{ + CheckVulcanMajor: func( + currentVersion string, + f *cmdutil.Factory, + vulcan *vulcanPkg.VulcanPkg, + ) error { + return errors.New("Error check vulcan major") + }, + Command: func( + flags, params string, + f *cmdutil.Factory, + ) string { + installEdgeFunctions := "npx --yes %s edge-functions%s %s" + versionVulcan := "@3.2.1" + return fmt.Sprintf( + installEdgeFunctions, + flags, + versionVulcan, + params, + ) + }, + }, + conf: &contracts.AzionApplicationOptions{}, + vulcanParams: "", + fields: &contracts.BuildInfo{}, + msgs: &[]string{}, + }, + wantErr: true, + }, + { + name: "error run command build edge-funcionts bundler", + fields: fields{ + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return errors.New("error run command interactive command build bundler") + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + vul: &vulcanPkg.VulcanPkg{ + CheckVulcanMajor: func( + currentVersion string, + f *cmdutil.Factory, + vulcan *vulcanPkg.VulcanPkg, + ) error { + return nil + }, + Command: func( + flags, params string, + f *cmdutil.Factory, + ) string { + installEdgeFunctions := "npx --yes %s edge-functions%s %s" + versionVulcan := "@3.2.1" + return fmt.Sprintf( + installEdgeFunctions, + flags, + versionVulcan, + params, + ) + }, + }, + conf: &contracts.AzionApplicationOptions{}, + vulcanParams: "", + fields: &contracts.BuildInfo{}, + msgs: &[]string{}, + }, + wantErr: true, + }, + { + name: "error write azion json content", + fields: fields{ + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return errors.New("Error while writing azion.json file") + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, }, args: args{ vul: &vulcanPkg.VulcanPkg{ @@ -94,6 +344,7 @@ func TestBuildCmd_vulcan(t *testing.T) { fields: &contracts.BuildInfo{}, msgs: &[]string{}, }, + wantErr: true, }, } for _, tt := range tests { From 63ef25e950af73962e0a7b893c27af8df774dad0 Mon Sep 17 00:00:00 2001 From: maxwelbm Date: Thu, 5 Sep 2024 08:39:28 -0300 Subject: [PATCH 4/8] test: test unit utils from package build completed --- pkg/cmd/build/utils_test.go | 171 ++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 pkg/cmd/build/utils_test.go diff --git a/pkg/cmd/build/utils_test.go b/pkg/cmd/build/utils_test.go new file mode 100644 index 000000000..f2fcc9c6b --- /dev/null +++ b/pkg/cmd/build/utils_test.go @@ -0,0 +1,171 @@ +package build + +import ( + "io" + "io/fs" + "testing" + + msg "github.com/aziontech/azion-cli/messages/build" + "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/contracts" + "github.com/aziontech/azion-cli/pkg/iostreams" + "github.com/aziontech/azion-cli/pkg/logger" + "go.uber.org/zap/zapcore" +) + +func TestBuildCmd_runCommand(t *testing.T) { + logger.New(zapcore.DebugLevel) + + type fields struct { + Io *iostreams.IOStreams + WriteFile func(filename string, data []byte, perm fs.FileMode) error + CommandRunnerStream func(out io.Writer, cmd string, envvars []string) error + CommandRunInteractive func(f *cmdutil.Factory, comm string) error + CommandRunner func(f *cmdutil.Factory, comm string, envVars []string) (string, error) + FileReader func(path string) ([]byte, error) + GetAzionJsonContent func(pathConf string) (*contracts.AzionApplicationOptions, error) + WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions, confPath string) error + EnvLoader func(path string) ([]string, error) + Stat func(path string) (fs.FileInfo, error) + GetWorkDir func() (string, error) + f *cmdutil.Factory + } + type args struct { + command string + msgs *[]string + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "flow completed with success", + fields: fields{ + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + command: "", + msgs: &[]string{""}, + }, + }, + { + name: "flow completed with success, no message", + fields: fields{ + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + command: "", + msgs: &[]string{}, + }, + }, + { + name: "Error CommandRunInteractive failed", + fields: fields{ + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return msg.ErrFailedToRunBuildCommand + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + command: "", + msgs: &[]string{""}, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &BuildCmd{ + Io: tt.fields.Io, + WriteFile: tt.fields.WriteFile, + CommandRunnerStream: tt.fields.CommandRunnerStream, + CommandRunInteractive: tt.fields.CommandRunInteractive, + CommandRunner: tt.fields.CommandRunner, + FileReader: tt.fields.FileReader, + GetAzionJsonContent: tt.fields.GetAzionJsonContent, + WriteAzionJsonContent: tt.fields.WriteAzionJsonContent, + EnvLoader: tt.fields.EnvLoader, + Stat: tt.fields.Stat, + GetWorkDir: tt.fields.GetWorkDir, + f: tt.fields.f, + } + if err := b.runCommand(tt.args.command, tt.args.msgs); (err != nil) != tt.wantErr { + t.Errorf("BuildCmd.runCommand() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 90cc94eb3b440f780c7fbccaa6838d6e01b1586b Mon Sep 17 00:00:00 2001 From: maxwelbm Date: Thu, 5 Sep 2024 09:34:52 -0300 Subject: [PATCH 5/8] test: test unit build from package build completed --- pkg/cmd/build/build_test.go | 132 ++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 pkg/cmd/build/build_test.go diff --git a/pkg/cmd/build/build_test.go b/pkg/cmd/build/build_test.go new file mode 100644 index 000000000..7611eb481 --- /dev/null +++ b/pkg/cmd/build/build_test.go @@ -0,0 +1,132 @@ +package build + +import ( + "io" + "io/fs" + "testing" + + "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/contracts" + "github.com/aziontech/azion-cli/pkg/iostreams" + "github.com/aziontech/azion-cli/pkg/logger" + "go.uber.org/zap/zapcore" +) + +func TestNewCmd(t *testing.T) { + f := &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + } + NewCmd(f) +} + +func TestBuildCmd_ExternalRun(t *testing.T) { + logger.New(zapcore.DebugLevel) + + type fields struct { + Io *iostreams.IOStreams + WriteFile func(filename string, data []byte, perm fs.FileMode) error + CommandRunnerStream func(out io.Writer, cmd string, envvars []string) error + CommandRunInteractive func(f *cmdutil.Factory, comm string) error + CommandRunner func(f *cmdutil.Factory, comm string, envVars []string) (string, error) + FileReader func(path string) ([]byte, error) + GetAzionJsonContent func(pathConf string) (*contracts.AzionApplicationOptions, error) + WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions, confPath string) error + EnvLoader func(path string) ([]string, error) + Stat func(path string) (fs.FileInfo, error) + GetWorkDir func() (string, error) + f *cmdutil.Factory + } + type args struct { + fields *contracts.BuildInfo + confPath string + msgs *[]string + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "flow completed with success", + fields: fields{ + GetAzionJsonContent: func(pathConf string) (*contracts.AzionApplicationOptions, error) { + return &contracts.AzionApplicationOptions{}, nil + }, + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + fields: &contracts.BuildInfo{ + ProjectPath: "", + }, + confPath: "", + msgs: &[]string{}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &BuildCmd{ + Io: tt.fields.Io, + WriteFile: tt.fields.WriteFile, + CommandRunnerStream: tt.fields.CommandRunnerStream, + CommandRunInteractive: tt.fields.CommandRunInteractive, + CommandRunner: tt.fields.CommandRunner, + FileReader: tt.fields.FileReader, + GetAzionJsonContent: tt.fields.GetAzionJsonContent, + WriteAzionJsonContent: tt.fields.WriteAzionJsonContent, + EnvLoader: tt.fields.EnvLoader, + Stat: tt.fields.Stat, + GetWorkDir: tt.fields.GetWorkDir, + f: tt.fields.f, + } + if err := b.ExternalRun(tt.args.fields, tt.args.confPath, tt.args.msgs); (err != nil) != tt.wantErr { + t.Errorf("BuildCmd.ExternalRun() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestNewBuildCmd(t *testing.T) { + f := &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + } + NewBuildCmd(f) +} From b20bae608f5df25b022bf4b48505c0fbc25e7e28 Mon Sep 17 00:00:00 2001 From: maxwelbm Date: Thu, 5 Sep 2024 09:35:07 -0300 Subject: [PATCH 6/8] test: test unit run from package build completed --- pkg/cmd/build/run_test.go | 292 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 pkg/cmd/build/run_test.go diff --git a/pkg/cmd/build/run_test.go b/pkg/cmd/build/run_test.go new file mode 100644 index 000000000..24d7c0e61 --- /dev/null +++ b/pkg/cmd/build/run_test.go @@ -0,0 +1,292 @@ +package build + +import ( + "io" + "io/fs" + "testing" + + msg "github.com/aziontech/azion-cli/messages/build" + "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/contracts" + "github.com/aziontech/azion-cli/pkg/iostreams" + "github.com/aziontech/azion-cli/pkg/logger" + "go.uber.org/zap/zapcore" +) + +func TestBuildCmd_run(t *testing.T) { + logger.New(zapcore.DebugLevel) + + type fields struct { + Io *iostreams.IOStreams + WriteFile func(filename string, data []byte, perm fs.FileMode) error + CommandRunnerStream func(out io.Writer, cmd string, envvars []string) error + CommandRunInteractive func(f *cmdutil.Factory, comm string) error + CommandRunner func(f *cmdutil.Factory, comm string, envVars []string) (string, error) + FileReader func(path string) ([]byte, error) + GetAzionJsonContent func(pathConf string) (*contracts.AzionApplicationOptions, error) + WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions, confPath string) error + EnvLoader func(path string) ([]string, error) + Stat func(path string) (fs.FileInfo, error) + GetWorkDir func() (string, error) + f *cmdutil.Factory + } + type args struct { + fields *contracts.BuildInfo + msgs *[]string + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "flow completed with success", + fields: fields{ + GetAzionJsonContent: func(pathConf string) (*contracts.AzionApplicationOptions, error) { + return &contracts.AzionApplicationOptions{}, nil + }, + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + fields: &contracts.BuildInfo{ + ProjectPath: "", + }, + msgs: &[]string{}, + }, + }, + { + name: "flow completed with success, fields full values", + fields: fields{ + GetAzionJsonContent: func(pathConf string) (*contracts.AzionApplicationOptions, error) { + return &contracts.AzionApplicationOptions{}, nil + }, + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + fields: &contracts.BuildInfo{ + ProjectPath: "", + Preset: "vanilla", + Mode: "compute", + Entry: "no", + NodePolyfills: "true", + OwnWorker: "true", + IsFirewall: true, + }, + msgs: &[]string{}, + }, + }, + { + name: "Error Get Azion json", + fields: fields{ + GetAzionJsonContent: func(pathConf string) (*contracts.AzionApplicationOptions, error) { + return &contracts.AzionApplicationOptions{}, msg.ErrorBuilding + }, + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + fields: &contracts.BuildInfo{ + ProjectPath: "", + Preset: "vanilla", + Mode: "compute", + Entry: "no", + NodePolyfills: "true", + OwnWorker: "true", + IsFirewall: true, + }, + msgs: &[]string{}, + }, + wantErr: true, + }, + { + name: "Error parse NodePolyfills", + fields: fields{ + GetAzionJsonContent: func(pathConf string) (*contracts.AzionApplicationOptions, error) { + return &contracts.AzionApplicationOptions{}, nil + }, + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + fields: &contracts.BuildInfo{ + ProjectPath: "", + Preset: "vanilla", + Mode: "compute", + Entry: "no", + NodePolyfills: "adf", + OwnWorker: "true", + IsFirewall: true, + }, + msgs: &[]string{}, + }, + wantErr: true, + }, + { + name: "Error parse OwnWorker", + fields: fields{ + GetAzionJsonContent: func(pathConf string) (*contracts.AzionApplicationOptions, error) { + return &contracts.AzionApplicationOptions{}, nil + }, + Io: iostreams.System(), + CommandRunner: func( + f *cmdutil.Factory, + comm string, + envVars []string, + ) (string, error) { + return "", nil + }, + CommandRunInteractive: func(f *cmdutil.Factory, comm string) error { + return nil + }, + WriteAzionJsonContent: func( + conf *contracts.AzionApplicationOptions, + confPath string, + ) error { + return nil + }, + f: &cmdutil.Factory{ + Flags: cmdutil.Flags{ + GlobalFlagAll: false, + Format: "", + Out: "", + NoColor: false, + }, + IOStreams: iostreams.System(), + }, + }, + args: args{ + fields: &contracts.BuildInfo{ + ProjectPath: "", + Preset: "vanilla", + Mode: "compute", + Entry: "no", + NodePolyfills: "true", + OwnWorker: "adf", + IsFirewall: true, + }, + msgs: &[]string{}, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &BuildCmd{ + Io: tt.fields.Io, + WriteFile: tt.fields.WriteFile, + CommandRunnerStream: tt.fields.CommandRunnerStream, + CommandRunInteractive: tt.fields.CommandRunInteractive, + CommandRunner: tt.fields.CommandRunner, + FileReader: tt.fields.FileReader, + GetAzionJsonContent: tt.fields.GetAzionJsonContent, + WriteAzionJsonContent: tt.fields.WriteAzionJsonContent, + EnvLoader: tt.fields.EnvLoader, + Stat: tt.fields.Stat, + GetWorkDir: tt.fields.GetWorkDir, + f: tt.fields.f, + } + if err := b.run(tt.args.fields, tt.args.msgs); (err != nil) != tt.wantErr { + t.Errorf("BuildCmd.run() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 436c20fb4847d554497a194f12258cefbeafe7ab Mon Sep 17 00:00:00 2001 From: PatrickMenoti <82882574+PatrickMenoti@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:33:58 -0300 Subject: [PATCH 7/8] fix: link created .vulcan with lower case characters --- pkg/cmd/link/link.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/link/link.go b/pkg/cmd/link/link.go index ee8261642..d5fd261fa 100644 --- a/pkg/cmd/link/link.go +++ b/pkg/cmd/link/link.go @@ -7,6 +7,7 @@ import ( "os/exec" "path/filepath" "regexp" + "strings" "github.com/MakeNowJust/heredoc" msg "github.com/aziontech/azion-cli/messages/link" @@ -213,6 +214,17 @@ func (cmd *LinkCmd) run(c *cobra.Command, info *LinkInfo) error { msgs = append(msgs, msg.WrittenGitignore) } + cmdVulcanInit := "init" + cmdVulcanInit = fmt.Sprintf("%s --preset '%s' --mode '%s' --scope '%s'", cmdVulcanInit, strings.ToLower(info.Preset), strings.ToLower(info.Mode), info.PathWorkingDir) + + vul := vulcanPkg.NewVulcan() + command := vul.Command("", cmdVulcanInit, cmd.F) + + _, err = cmd.CommandRunner(cmd.F, command, []string{}) + if err != nil { + return err + } + if !info.Auto { if cmd.ShouldDevDeploy(info, msg.AskLocalDev, false) { if err := deps(c, cmd, info, msg.AskInstallDepsDev, &msgs); err != nil { @@ -253,17 +265,6 @@ func (cmd *LinkCmd) run(c *cobra.Command, info *LinkInfo) error { } - cmdVulcanInit := "init" - cmdVulcanInit = fmt.Sprintf("%s --preset '%s' --mode '%s' --scope '%s'", cmdVulcanInit, info.Preset, info.Mode, info.PathWorkingDir) - - vul := vulcanPkg.NewVulcan() - command := vul.Command("", cmdVulcanInit, cmd.F) - - _, err = cmd.CommandRunner(cmd.F, command, []string{}) - if err != nil { - return err - } - initOut := output.SliceOutput{ GeneralOutput: output.GeneralOutput{ Out: cmd.F.IOStreams.Out, From b5b4d9c712b39044c929a0112837c2a57266d0c2 Mon Sep 17 00:00:00 2001 From: PatrickMenoti <82882574+PatrickMenoti@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:34:27 -0300 Subject: [PATCH 8/8] fix: link created .vulcan with lower case characters #patch --- pkg/cmd/link/link.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cmd/link/link.go b/pkg/cmd/link/link.go index d5fd261fa..a0eccbf92 100644 --- a/pkg/cmd/link/link.go +++ b/pkg/cmd/link/link.go @@ -214,6 +214,7 @@ func (cmd *LinkCmd) run(c *cobra.Command, info *LinkInfo) error { msgs = append(msgs, msg.WrittenGitignore) } + //run init before calling build cmdVulcanInit := "init" cmdVulcanInit = fmt.Sprintf("%s --preset '%s' --mode '%s' --scope '%s'", cmdVulcanInit, strings.ToLower(info.Preset), strings.ToLower(info.Mode), info.PathWorkingDir)