Skip to content

Commit

Permalink
- tests for goal init
Browse files Browse the repository at this point in the history
  • Loading branch information
aaabramov committed Nov 9, 2021
1 parent 2966fbc commit 2486011
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
73 changes: 37 additions & 36 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var initCmd = &cobra.Command{
Short: "Create new goal.yaml file in current file",
Long: "Create new goal.yaml file in current file",
Run: func(cmd *cobra.Command, args []string) {
initGoals()
initGoals("goal.yaml")
},
}

Expand All @@ -22,47 +22,48 @@ func init() {
// TODO: support templates
}

func initGoals() {
lib.Info("⌛ Generating default goal.yaml file")
goals := map[string]lib.YamlGoal{
"workspace": {
Cmd: "terraform",
Args: []string{"apply", "-var-file", "vars/dev.tfvars"},
Assert: nil,
},
"tf-apply": {
Envs: &map[string]lib.YamlEnvGoal{
"dev": {
Desc: "Terraform apply on dev",
Cmd: "terraform",
Args: []string{"apply", "-var-file", "vars/dev.tfvars"},
Assert: &lib.Assert{
Desc: "Check if on dev workspace",
Ref: "workspace",
Expect: "dev",
Fix: "terraform workspace select dev",
},
var defaultGoals = map[string]lib.YamlGoal{
"workspace": {
Cmd: "terraform",
Args: []string{"apply", "-var-file", "vars/dev.tfvars"},
Assert: nil,
},
"tf-apply": {
Envs: &map[string]lib.YamlEnvGoal{
"dev": {
Desc: "Terraform apply on dev",
Cmd: "terraform",
Args: []string{"apply", "-var-file", "vars/dev.tfvars"},
Assert: &lib.Assert{
Desc: "Check if on dev workspace",
Ref: "workspace",
Expect: "dev",
Fix: "terraform workspace select dev",
},
"stage": {
Desc: "Terraform apply on stage",
Cmd: "terraform",
Args: []string{"apply", "-var-file", "vars/stage.tfvars"},
Assert: &lib.Assert{
Desc: "Check if on stage workspace",
Ref: "workspace",
Expect: "stage",
Fix: "terraform workspace select stage",
},
},
"stage": {
Desc: "Terraform apply on stage",
Cmd: "terraform",
Args: []string{"apply", "-var-file", "vars/stage.tfvars"},
Assert: &lib.Assert{
Desc: "Check if on stage workspace",
Ref: "workspace",
Expect: "stage",
Fix: "terraform workspace select stage",
},
},
},
}
bytes, err := yaml.Marshal(goals)
},
}

func initGoals(filename string) {
lib.Info("⌛ Generating default %s file", filename)
bytes, err := yaml.Marshal(defaultGoals)
if err != nil {
lib.Fatal("Failed to generate default YAML for goals")
}
if err = ioutil.WriteFile("goal.yaml", bytes, 0644); err != nil {
lib.Fatal("Failed to create goal.yaml")
if err = ioutil.WriteFile(filename, bytes, 0644); err != nil {
lib.Fatal("Failed to create %s", filename)
}
lib.Info("✅ Generated default goal.yaml file. Try running `goal` to see available goals.")
lib.Info("✅ Generated default %s file. Try running `goal` to see available goals.", filename)
}
31 changes: 31 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/aaabramov/goal/lib"
"io/ioutil"
"os"
"testing"
)

func Test_initGoals(t *testing.T) {
type args struct {
filename string
}
tests := []struct {
name string
args args
}{
{name: "generate file", args: args{filename: "goal.test.yaml"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
initGoals(tt.args.filename)
bytes, _ := ioutil.ReadFile(tt.args.filename)
parseCommands, _ := lib.ParseCommands(bytes)
if len(parseCommands.Commands) != 3 {
t.Errorf("expected %d commands to be generated", 3)
}
_ = os.Remove(tt.args.filename)
})
}
}

0 comments on commit 2486011

Please sign in to comment.