From f1c767f32f9a30ad1120108d25afd39028a1ee8b Mon Sep 17 00:00:00 2001 From: psihachina Date: Fri, 7 Oct 2022 15:14:01 +0300 Subject: [PATCH 1/2] added check TTY --- internal/commands/initialize.go | 77 +++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/internal/commands/initialize.go b/internal/commands/initialize.go index 7a228808..aea90677 100644 --- a/internal/commands/initialize.go +++ b/internal/commands/initialize.go @@ -2,14 +2,15 @@ package commands import ( "fmt" + "github.com/AlecAivazis/survey/v2" "github.com/hazelops/ize/internal/schema" "github.com/hazelops/ize/internal/version" + "golang.org/x/sys/unix" "os" "path/filepath" "sort" "strings" - "github.com/AlecAivazis/survey/v2" "github.com/hazelops/ize/examples" "github.com/hazelops/ize/internal/generate" "github.com/hazelops/ize/pkg/templates" @@ -102,6 +103,11 @@ func (o *InitOptions) Validate() error { } func (o *InitOptions) Run() error { + isTTY := true + _, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ) + if err != nil { + isTTY = false + } if len(o.Template) != 0 { dest, err := generate.GenerateFiles(o.Template, o.Output) if err != nil { @@ -129,57 +135,62 @@ func (o *InitOptions) Run() error { } } - dir, err := filepath.Abs(o.Output) + dir, err = filepath.Abs(o.Output) if err != nil { return fmt.Errorf("can't init: %w", err) } namespace = filepath.Base(dir) - err = survey.AskOne( - &survey.Input{ - Message: fmt.Sprintf("Namespace:"), - Default: namespace, - }, - &namespace, - survey.WithValidator(survey.Required), - ) - if err != nil { - return fmt.Errorf("can't init: %w", err) - } - err = survey.AskOne( - &survey.Input{ - Message: fmt.Sprintf("Environment:"), - Default: env, - }, - &env, - survey.WithValidator(survey.Required), - ) - if err != nil { - return fmt.Errorf("can't init: %w", err) - } - - envList = append(envList, env) - env = "" + if isTTY { + err = survey.AskOne( + &survey.Input{ + Message: fmt.Sprintf("Namespace:"), + Default: namespace, + }, + &namespace, + survey.WithValidator(survey.Required), + ) + if err != nil { + return fmt.Errorf("can't init: %w", err) + } - for { err = survey.AskOne( &survey.Input{ - Message: fmt.Sprintf("Another environment? [enter - skip]"), + Message: fmt.Sprintf("Environment:"), Default: env, }, &env, + survey.WithValidator(survey.Required), ) if err != nil { return fmt.Errorf("can't init: %w", err) } - if env == "" { - break - } - envList = append(envList, env) env = "" + + for { + err = survey.AskOne( + &survey.Input{ + Message: fmt.Sprintf("Another environment? [enter - skip]"), + Default: env, + }, + &env, + ) + if err != nil { + return fmt.Errorf("can't init: %w", err) + } + + if env == "" { + break + } + + envList = append(envList, env) + env = "" + } + } else { + envList = append(envList, env) } for _, v := range envList { From 3b6df54e2bd128d51f21a45087b5cf2dc6264a49 Mon Sep 17 00:00:00 2001 From: psihachina Date: Fri, 7 Oct 2022 15:14:37 +0300 Subject: [PATCH 2/2] added `ize init` unit tests --- internal/commands/initialize_test.go | 183 +++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) diff --git a/internal/commands/initialize_test.go b/internal/commands/initialize_test.go index cb22ddbe..8d49dc6a 100644 --- a/internal/commands/initialize_test.go +++ b/internal/commands/initialize_test.go @@ -1,6 +1,12 @@ package commands import ( + "github.com/hazelops/ize/internal/config" + "github.com/spf13/pflag" + "github.com/spf13/viper" + "os" + "path/filepath" + "strings" "testing" ) @@ -22,3 +28,180 @@ func Test_writeConfig(t *testing.T) { }) } } + +func TestInit(t *testing.T) { + tests := []struct { + name string + args []string + wantErr bool + env map[string]string + }{ + { + name: "success (only env)", + args: []string{"init"}, + env: map[string]string{"ENV": "test"}, + wantErr: false, + }, + { + name: "success (env and flag)", + args: []string{"init", "--skip-examples"}, + env: map[string]string{"ENV": "test"}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + viper.Reset() + // Set env + for k, v := range tt.env { + t.Setenv(k, v) + } + + temp, err := os.MkdirTemp("", "test") + if err != nil { + t.Error(err) + return + } + err = os.Chdir(temp) + if err != nil { + t.Error(err) + return + } + err = os.MkdirAll(filepath.Join(temp, ".ize", "env", "test"), 0777) + if err != nil { + t.Error(err) + return + } + + t.Setenv("HOME", temp) + + cfg := new(config.Project) + cmd := newRootCmd(cfg) + + cmd.SetArgs(tt.args) + cmd.PersistentFlags().ParseErrorsWhitelist.UnknownFlags = true + err = cmd.PersistentFlags().Parse(tt.args) + if err != nil { + t.Error(err) + return + } + + cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) { + if len(f.Value.String()) != 0 { + _ = viper.BindPFlag(strings.ReplaceAll(f.Name, "-", "_"), cmd.PersistentFlags().Lookup(f.Name)) + } + }) + + config.InitConfig() + + err = cmd.Execute() + if (err != nil) != tt.wantErr { + t.Errorf("ize gen tfenv error = %v, wantErr %v", err, tt.wantErr) + return + } + + file, err := os.ReadFile(filepath.Join(".ize", "env", os.Getenv("ENV"), "ize.toml")) + if err != nil { + t.Error(err) + } + + if !strings.Contains(string(file), filepath.Base(temp)) { + t.Errorf("ize.toml = %v, want contains %s", string(file), filepath.Base(temp)) + } + // Unset env + for k, _ := range tt.env { + os.Unsetenv(k) + } + }) + } +} + +func TestInitInternal(t *testing.T) { + tests := []struct { + name string + args []string + wantErr bool + env map[string]string + want string + }{ + { + name: "success list", + args: []string{"init", "--list"}, + wantErr: false, + }, + { + name: "success", + want: "\"examples.ize.sh\"", + args: []string{"init", "--template", "ecs-apps-monorepo"}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + viper.Reset() + // Set env + for k, v := range tt.env { + t.Setenv(k, v) + } + + temp, err := os.MkdirTemp("", "test") + if err != nil { + t.Error(err) + return + } + err = os.Chdir(temp) + if err != nil { + t.Error(err) + return + } + err = os.MkdirAll(filepath.Join(temp, ".ize", "env", "test"), 0777) + if err != nil { + t.Error(err) + return + } + + t.Setenv("HOME", temp) + + cfg := new(config.Project) + cmd := newRootCmd(cfg) + + cmd.SetArgs(tt.args) + cmd.PersistentFlags().ParseErrorsWhitelist.UnknownFlags = true + err = cmd.PersistentFlags().Parse(tt.args) + if err != nil { + t.Error(err) + return + } + + cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) { + if len(f.Value.String()) != 0 { + _ = viper.BindPFlag(strings.ReplaceAll(f.Name, "-", "_"), cmd.PersistentFlags().Lookup(f.Name)) + } + }) + + config.InitConfig() + + err = cmd.Execute() + if (err != nil) != tt.wantErr { + t.Errorf("ize gen tfenv error = %v, wantErr %v", err, tt.wantErr) + return + } + + if tt.want != "" { + file, err := os.ReadFile(filepath.Join(".ize", "env", "testnut", "ize.toml")) + if err != nil { + t.Error(err) + } + + if !strings.Contains(string(file), tt.want) { + t.Errorf("ize.toml = %v, want contains %s", string(file), tt.want) + } + } + + // Unset env + for k, _ := range tt.env { + os.Unsetenv(k) + } + }) + } +}