Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return an error when the argument count is wrong #2510

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/exprparser/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestFunctionContains(t *testing.T) {
assert.Equal(t, tt.expected, output)
})
}

_, err := NewInterpeter(env, Config{}).Evaluate("contains('one')", DefaultStatusCheckNone)
assert.Error(t, err)
}

func TestFunctionStartsWith(t *testing.T) {
Expand Down Expand Up @@ -72,6 +75,9 @@ func TestFunctionStartsWith(t *testing.T) {
assert.Equal(t, tt.expected, output)
})
}

_, err := NewInterpeter(env, Config{}).Evaluate("startsWith('one')", DefaultStatusCheckNone)
assert.Error(t, err)
}

func TestFunctionEndsWith(t *testing.T) {
Expand Down Expand Up @@ -101,6 +107,9 @@ func TestFunctionEndsWith(t *testing.T) {
assert.Equal(t, tt.expected, output)
})
}

_, err := NewInterpeter(env, Config{}).Evaluate("endsWith('one')", DefaultStatusCheckNone)
assert.Error(t, err)
}

func TestFunctionJoin(t *testing.T) {
Expand Down Expand Up @@ -128,6 +137,9 @@ func TestFunctionJoin(t *testing.T) {
assert.Equal(t, tt.expected, output)
})
}

_, err := NewInterpeter(env, Config{}).Evaluate("join()", DefaultStatusCheckNone)
assert.Error(t, err)
}

func TestFunctionToJSON(t *testing.T) {
Expand All @@ -154,6 +166,9 @@ func TestFunctionToJSON(t *testing.T) {
assert.Equal(t, tt.expected, output)
})
}

_, err := NewInterpeter(env, Config{}).Evaluate("tojson()", DefaultStatusCheckNone)
assert.Error(t, err)
}

func TestFunctionFromJSON(t *testing.T) {
Expand All @@ -177,6 +192,9 @@ func TestFunctionFromJSON(t *testing.T) {
assert.Equal(t, tt.expected, output)
})
}

_, err := NewInterpeter(env, Config{}).Evaluate("fromjson()", DefaultStatusCheckNone)
assert.Error(t, err)
}

func TestFunctionHashFiles(t *testing.T) {
Expand Down Expand Up @@ -248,4 +266,7 @@ func TestFunctionFormat(t *testing.T) {
}
})
}

_, err := NewInterpeter(env, Config{}).Evaluate("format()", DefaultStatusCheckNone)
assert.Error(t, err)
}
35 changes: 35 additions & 0 deletions pkg/exprparser/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,23 +589,58 @@
args = append(args, reflect.ValueOf(value))
}

argCountCheck := func(argCount int) error {
if len(args) != argCount {
return fmt.Errorf("'%s' expected %d arguments but got %d instead", funcCallNode.Callee, argCount, len(args))
}
return nil
}

argAtLeastCheck := func(atLeast int) error {

Check failure on line 599 in pkg/exprparser/interpreter.go

View workflow job for this annotation

GitHub Actions / Check for spelling errors

atLeast ==> at least
if len(args) < atLeast {

Check failure on line 600 in pkg/exprparser/interpreter.go

View workflow job for this annotation

GitHub Actions / Check for spelling errors

atLeast ==> at least
return fmt.Errorf("'%s' expected at least %d arguments but got %d instead", funcCallNode.Callee, atLeast, len(args))

Check failure on line 601 in pkg/exprparser/interpreter.go

View workflow job for this annotation

GitHub Actions / Check for spelling errors

atLeast ==> at least
}
return nil
}

switch strings.ToLower(funcCallNode.Callee) {
case "contains":
if err := argCountCheck(2); err != nil {
return nil, err
}
return impl.contains(args[0], args[1])
case "startswith":
if err := argCountCheck(2); err != nil {
return nil, err
}
return impl.startsWith(args[0], args[1])
case "endswith":
if err := argCountCheck(2); err != nil {
return nil, err
}
return impl.endsWith(args[0], args[1])
case "format":
if err := argAtLeastCheck(1); err != nil {
return nil, err
}
return impl.format(args[0], args[1:]...)
case "join":
if err := argAtLeastCheck(1); err != nil {
return nil, err
}
if len(args) == 1 {
return impl.join(args[0], reflect.ValueOf(","))
}
return impl.join(args[0], args[1])
case "tojson":
if err := argCountCheck(1); err != nil {
return nil, err
}
return impl.toJSON(args[0])
case "fromjson":
if err := argCountCheck(1); err != nil {
return nil, err
}
return impl.fromJSON(args[0])
case "hashfiles":
if impl.env.HashFiles != nil {
Expand Down
Loading