Skip to content

Commit

Permalink
Return early if input is nil in TmplObjectArray
Browse files Browse the repository at this point in the history
In some cases the input variable can be nil and it would panic in function getInner.

Added a table test including existing test case for the function and importer package stretchr/testify as a dependency
  • Loading branch information
Crevil committed Oct 2, 2018
1 parent 2db0989 commit e485520
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 9 deletions.
61 changes: 59 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
[[constraint]]
name = "github.com/go-cmd/cmd"
version = "1.0.3"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.*"
3 changes: 3 additions & 0 deletions pkg/templates/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func TmplArray(path string, input interface{}) []interface{} {

// TODO: Add description
func TmplObjectArray(path string, input interface{}) []KeyValuePair {
if input == nil {
return nil
}
value := TmplGet(path, input)
switch value.(type) {
case map[interface{}]interface{}:
Expand Down
71 changes: 64 additions & 7 deletions pkg/templates/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -57,13 +58,69 @@ func TestTmplInt(t *testing.T) {
}

func TestTmpObjArray(t *testing.T) {
output := TmplObjectArray("b", input)
type input struct {
path string
data interface{}
}
tt := []struct {
name string
input input
output []KeyValuePair
}{
{
name: "nil input",
input: input{
path: "b",
data: nil,
},
output: []KeyValuePair{},
},
{
name: "empty input",
input: input{
path: "b",
data: fromYaml(""),
},
output: []KeyValuePair{},
},
{
name: "nested values",
input: input{
path: "b",
data: fromYaml(`
b:
c: 2
h: 'ewff'
`),
},
output: []KeyValuePair{
{Key: "c", Value: 2},
{Key: "h", Value: "ewff"},
},
},
{
name: "non object value in path",
input: input{
path: "a",
data: fromYaml(`a: b`),
},
output: []KeyValuePair{},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
output := TmplObjectArray(tc.input.path, tc.input.data)

if len(output) == 2 {
if !(output[0].Key == "c" && output[0].Value == 2 && output[1].Key == "h" && output[1].Value == "ewff") {
t.Errorf("ObjArray didn't match expected values, got: [{%s, %v}, {%s, %s}], want: [{c, 2}, {h, ewff}]", output[0].Key, output[0].Value, output[1].Key, output[1].Value)
}
} else {
t.Errorf("ObjArray didn't match length, got: %s, want: %v", output, 2)
assert.ElementsMatch(t, tc.output, output, "output does not match the expected")
})
}
}

func fromYaml(data string) interface{} {
m := make(map[string]interface{})
err := yaml.Unmarshal([]byte(data), m)
if err != nil {
log.Fatalf("faild to read yaml: %v", err)
}
return m
}

0 comments on commit e485520

Please sign in to comment.