Skip to content

Commit

Permalink
refactor: add missing error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
luisdavim committed Jan 11, 2022
1 parent 07088b5 commit b4c1a9c
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 23 deletions.
4 changes: 3 additions & 1 deletion internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ func (c *cli) readState(s *state) error {
if len(c.spec) > 0 {

sp := new(StateFiles)
sp.specFromYAML(c.spec)
if err := sp.specFromYAML(c.spec); err != nil {
return fmt.Errorf("error parsing spec file: %w", err)
}

for _, val := range sp.StateFiles {
fo := fileOption{}
Expand Down
4 changes: 3 additions & 1 deletion internal/app/decision_maker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func Test_inspectUpgradeScenario(t *testing.T) {

// Act
c, _ := getChartInfo(tt.args.r.Chart, tt.args.r.Version)
cs.inspectUpgradeScenario(tt.args.r, &outcome, c)
if err := cs.inspectUpgradeScenario(tt.args.r, &outcome, c); err != nil {
t.Errorf("inspectUpgradeScenario() error = %v", err)
}
got := outcome.Decisions[0].Type
t.Log(outcome.Decisions[0].Description)

Expand Down
2 changes: 1 addition & 1 deletion internal/app/helm_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_getChartInfo(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := getChartInfo(tt.args.r.Chart, tt.args.r.Version)
if err != nil && tt.want != nil {
t.Errorf("getChartInfo() = Unexpected error: %w", err)
t.Errorf("getChartInfo() = Unexpected error: %v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getChartInfo() = %v, want %v", got, tt.want)
Expand Down
4 changes: 2 additions & 2 deletions internal/app/kube_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func labelNamespace(ns string, labels map[string]string, authoritative bool) {
// ignore default k8s namespace label from being removed
delete(nsLabels, "kubernetes.io/metadata.name")
// ignore every label defined in DSF for the namespace from being removed
for definedLabelKey, _ := range labels {
for definedLabelKey := range labels {
delete(nsLabels, definedLabelKey)
}
for label, _ := range nsLabels {
for label := range nsLabels {
args = append(args, label+"-")
}
}
Expand Down
10 changes: 8 additions & 2 deletions internal/app/spec_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func Test_specFromYAML(t *testing.T) {
},
}

teardownTestCase := setupStateFileTestCase(t)
teardownTestCase, err := setupStateFileTestCase(t)
if err != nil {
t.Fatal(err)
}
defer teardownTestCase(t)
for _, tt := range tests {
// os.Args = append(os.Args, "-f ../../examples/example.yaml")
Expand Down Expand Up @@ -72,7 +75,10 @@ func Test_specFileSort(t *testing.T) {
},
}

teardownTestCase := setupStateFileTestCase(t)
teardownTestCase, err := setupStateFileTestCase(t)
if err != nil {
t.Fatal(err)
}
defer teardownTestCase(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
39 changes: 29 additions & 10 deletions internal/app/state_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"testing"
)

func setupStateFileTestCase(t *testing.T) func(t *testing.T) {
func setupStateFileTestCase(t *testing.T) (func(t *testing.T), error) {
t.Log("setup test case")
os.MkdirAll(tempFilesDir, 0o755)
if err := os.MkdirAll(tempFilesDir, 0o755); err != nil {
t.Errorf("setupStateFileTestCase(), failed to create temp files dir: %v", err)
return nil, err
}

return func(t *testing.T) {
t.Log("teardown test case")
os.RemoveAll(tempFilesDir)
}
}, nil
}

func Test_fromTOML(t *testing.T) {
Expand Down Expand Up @@ -45,7 +48,10 @@ func Test_fromTOML(t *testing.T) {
os.Setenv("ORG_PATH", "sample")
os.Setenv("VALUE", "sample")

teardownTestCase := setupStateFileTestCase(t)
teardownTestCase, err := setupStateFileTestCase(t)
if err != nil {
t.Errorf("setupStateFileTestCase(), got: %v", err)
}
defer teardownTestCase(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -97,7 +103,10 @@ func Test_fromTOML_Expand(t *testing.T) {
os.Setenv("ORG_PATH", "sample")
os.Setenv("VALUE", "sample")

teardownTestCase := setupStateFileTestCase(t)
teardownTestCase, err := setupStateFileTestCase(t)
if err != nil {
t.Errorf("setupStateFileTestCase(), got: %v", err)
}
defer teardownTestCase(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -179,7 +188,10 @@ func Test_fromYAML(t *testing.T) {
os.Setenv("VALUE", "sample")
os.Setenv("ORG_PATH", "sample")

teardownTestCase := setupStateFileTestCase(t)
teardownTestCase, err := setupStateFileTestCase(t)
if err != nil {
t.Errorf("setupStateFileTestCase(), got: %v", err)
}
defer teardownTestCase(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -225,13 +237,17 @@ func Test_fromYAML_UnsetVars(t *testing.T) {
},
}

teardownTestCase := setupStateFileTestCase(t)
teardownTestCase, err := setupStateFileTestCase(t)
if err != nil {
t.Errorf("setupStateFileTestCase(), got: %v", err)
}
defer teardownTestCase(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.targetVar == "ORG_PATH" {
switch tt.targetVar {
case "ORG_PATH":
os.Setenv("VALUE", "sample")
} else if tt.targetVar == "VALUE" {
case "VALUE":
os.Setenv("ORG_PATH", "sample")
}
err := tt.args.s.fromYAML(tt.args.file)
Expand Down Expand Up @@ -282,7 +298,10 @@ func Test_fromYAML_Expand(t *testing.T) {
os.Setenv("ORG_PATH", "sample")
os.Setenv("VALUE", "sample")

teardownTestCase := setupStateFileTestCase(t)
teardownTestCase, err := setupStateFileTestCase(t)
if err != nil {
t.Errorf("setupStateFileTestCase(), got: %v", err)
}
defer teardownTestCase(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
22 changes: 16 additions & 6 deletions internal/app/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import (
"testing"
)

func setupTestCase(t *testing.T) func(t *testing.T) {
func setupTestCase(t *testing.T) (func(t *testing.T), error) {
t.Log("setup test case")
os.MkdirAll(tempFilesDir, 0o755)
os.MkdirAll(os.TempDir()+"/helmsman-tests/myapp", os.ModePerm)
os.MkdirAll(os.TempDir()+"/helmsman-tests/dir-with space/myapp", os.ModePerm)
if err := os.MkdirAll(tempFilesDir, 0o755); err != nil {
return nil, err
}
if err := os.MkdirAll(os.TempDir()+"/helmsman-tests/myapp", os.ModePerm); err != nil {
return nil, err
}
if err := os.MkdirAll(os.TempDir()+"/helmsman-tests/dir-with space/myapp", os.ModePerm); err != nil {
return nil, err
}
cmd := helmCmd([]string{"create", os.TempDir() + "/helmsman-tests/dir-with space/myapp"}, "creating an empty local chart directory")
if _, err := cmd.Exec(); err != nil {
log.Fatalf("Command failed: %v", err)
Expand All @@ -19,7 +25,7 @@ func setupTestCase(t *testing.T) func(t *testing.T) {
t.Log("teardown test case")
os.RemoveAll(tempFilesDir)
os.RemoveAll(os.TempDir() + "/helmsman-tests/")
}
}, nil
}

func Test_state_validate(t *testing.T) {
Expand Down Expand Up @@ -494,7 +500,11 @@ func Test_state_getReleaseChartsInfo(t *testing.T) {
},
}

teardownTestCase := setupTestCase(t)
teardownTestCase, err := setupTestCase(t)
if err != nil {
t.Errorf("setupTestCase() = %v", err)
return
}
defer teardownTestCase(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit b4c1a9c

Please sign in to comment.