core: Add test and fix for element with empty list
The incldued test previously caused a panic, it now returns an error message explaining the issue.
This commit is contained in:
parent
ef890386b6
commit
8403a465bc
|
@ -667,6 +667,9 @@ func interpolationFuncElement() ast.Function {
|
|||
ReturnType: ast.TypeString,
|
||||
Callback: func(args []interface{}) (interface{}, error) {
|
||||
list := args[0].([]ast.Variable)
|
||||
if len(list) == 0 {
|
||||
return nil, fmt.Errorf("element() may not be used with an empty list")
|
||||
}
|
||||
|
||||
index, err := strconv.Atoi(args[1].(string))
|
||||
if err != nil || index < 0 {
|
||||
|
|
|
@ -952,6 +952,7 @@ func TestInterpolateFuncElement(t *testing.T) {
|
|||
Vars: map[string]ast.Variable{
|
||||
"var.a_list": interfaceToVariableSwallowError([]string{"foo", "baz"}),
|
||||
"var.a_short_list": interfaceToVariableSwallowError([]string{"foo"}),
|
||||
"var.empty_list": interfaceToVariableSwallowError([]interface{}{}),
|
||||
},
|
||||
Cases: []testFunctionCase{
|
||||
{
|
||||
|
@ -980,6 +981,13 @@ func TestInterpolateFuncElement(t *testing.T) {
|
|||
true,
|
||||
},
|
||||
|
||||
// Empty list should fail
|
||||
{
|
||||
`${element(var.empty_list, 0)}`,
|
||||
nil,
|
||||
true,
|
||||
},
|
||||
|
||||
// Too many args
|
||||
{
|
||||
`${element(var.a_list, "0", "2")}`,
|
||||
|
|
Loading…
Reference in New Issue