Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IZE-551 test ize init #487

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}