Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: div_bhasin <[email protected]>
  • Loading branch information
divbhasin committed Jun 19, 2021
1 parent 46518fe commit 1b93de1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pkg/generator/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ func (opts *GenerateParametersOptions) genParameterSet(fn generator) (parameters
if parameters.IsInternal(name, opts.Bundle) {
continue
}
defaultVal, err := getDefaultParamValue(opts.Bundle, name)
if err != nil {
return pset, err
}
defaultVal, _ := getDefaultParamValue(opts.Bundle, name)

c, err := fn(name, surveyParameters, defaultVal)
if err != nil {
return pset, err
Expand All @@ -67,7 +65,6 @@ func (opts *GenerateParametersOptions) genParameterSet(fn generator) (parameters
}

func getDefaultParamValue(bun bundle.Bundle, name string) (string, error) {
// write logic to get default value of given param
for p, v := range bun.Parameters {
if p == name {
def, ok := bun.Definitions[v.Definition]
Expand All @@ -78,7 +75,9 @@ func getDefaultParamValue(bun bundle.Bundle, name string) (string, error) {
return "", fmt.Errorf("parameter definition for %s is empty", name)
}

return fmt.Sprintf("%s", def.Default), nil
if def.Default != nil {
return fmt.Sprintf("%s", def.Default), nil
}
}
}
return "", nil
Expand Down
61 changes: 61 additions & 0 deletions pkg/generator/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,64 @@ func TestSkipParameters(t *testing.T) {
assert.Equal(t, "skip-params", pset.Name, "Name was not set")
require.Empty(t, pset.Parameters, "parameter set should have empty parameters section")
}

func TestDefaultParameters(t *testing.T) {
firstParamName := "test"
secondParamName := "test2"
defaultVal := "example"

bun := bundle.Bundle{
Definitions: definition.Definitions{
firstParamName: &definition.Schema{
Default: defaultVal,
},
secondParamName: &definition.Schema{},
},
Parameters: map[string]bundle.Parameter{
firstParamName: {
Definition: firstParamName,
},
secondParamName: {
Definition: secondParamName,
},
},
}

val, err := getDefaultParamValue(bun, firstParamName)
assert.NoError(t, err, "valid default value for parameter should not give error")
assert.Equal(t, val, defaultVal)

val, err = getDefaultParamValue(bun, secondParamName)
assert.NoError(t, err, "valid default value for parameter should not give error")
assert.Equal(t, val, "")
}

func TestMalformedDefaultParameter(t *testing.T) {
firstParamName := "test"

bun := bundle.Bundle{
Definitions: definition.Definitions{},
Parameters: map[string]bundle.Parameter{
firstParamName: {
Definition: firstParamName,
},
},
}

_, err := getDefaultParamValue(bun, firstParamName)
assert.NotNil(t, err, "should give error when bundle has no parameter definitions")

bun2 := bundle.Bundle{
Definitions: definition.Definitions{
firstParamName: nil,
},
Parameters: map[string]bundle.Parameter{
firstParamName: {
Definition: firstParamName,
},
},
}

_, err = getDefaultParamValue(bun2, firstParamName)
assert.NotNil(t, err, "parameter with missing definition should give error when fetching it's default value")
}

0 comments on commit 1b93de1

Please sign in to comment.