Skip to content

Commit

Permalink
Merge pull request #487 from hazelops/IZE-551-test-ize-init
Browse files Browse the repository at this point in the history
IZE-551 test ize init
  • Loading branch information
psihachina authored Oct 7, 2022
2 parents 406d367 + 3b6df54 commit 71734a8
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 33 deletions.
77 changes: 44 additions & 33 deletions internal/commands/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
183 changes: 183 additions & 0 deletions internal/commands/initialize_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
}
})
}
}

0 comments on commit 71734a8

Please sign in to comment.