Skip to content

Commit c3a4e9b

Browse files
committed
fix(steps): provide functions args in function conditions values
Signed-off-by: Thomas Bétrancourt <[email protected]>
1 parent 8b89c27 commit c3a4e9b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

engine/engine_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,29 @@ func TestFunctionCustomState(t *testing.T) {
235235
assert.Equal(t, []string{"STATE_HELLO"}, customStates)
236236
}
237237

238+
func TestFunctionConditions(t *testing.T) {
239+
input := map[string]interface{}{}
240+
res, err := runTask("functionConditions.yaml", input, nil)
241+
242+
require.Nil(t, err)
243+
244+
assert.Equal(t, map[string]interface{}{
245+
"value": "Hello foobar !",
246+
}, res.Steps["asExpected"].Output)
247+
assert.Equal(t, "SAID_HELLO", res.Steps["asExpected"].State)
248+
assert.Equal(t, "Said hello to foobar", res.Steps["asExpected"].Error)
249+
250+
assert.Equal(t, map[string]interface{}{
251+
"value": "Hello foo !",
252+
}, res.Steps["notExpected"].Output)
253+
assert.Equal(t, "NOT_EXPECTED", res.Steps["notExpected"].State)
254+
assert.Equal(t, "Expected bar, got foo", res.Steps["notExpected"].Error)
255+
256+
assert.Nil(t, res.Steps["skipped"].Output)
257+
assert.Equal(t, "PRUNE", res.Steps["skipped"].State)
258+
assert.Equal(t, "No hello foo !", res.Steps["skipped"].Error)
259+
}
260+
238261
func TestFunctionPreHook(t *testing.T) {
239262
input := map[string]interface{}{}
240263
res, err := runTask("functionPreHook.yaml", input, nil)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: hello-with-conditions
2+
description: Say hello with conditions
3+
action:
4+
type: echo::hello::world
5+
configuration:
6+
name: '{{.function_args.name}}'
7+
custom_states:
8+
- SAID_HELLO
9+
- NOT_EXPECTED
10+
conditions:
11+
- type: skip
12+
if:
13+
- value: '{{.function_args.skip | default `false`}}'
14+
operator: EQ
15+
expected: 'true'
16+
then:
17+
this: PRUNE
18+
message: 'No hello {{.function_args.name}} !'
19+
- type: check
20+
if:
21+
- value: '{{.function_args.name}}'
22+
operator: NE
23+
expected: '{{.function_args.expected}}'
24+
then:
25+
this: NOT_EXPECTED
26+
message: 'Expected {{.function_args.expected}}, got {{.function_args.name}}'
27+
- type: check
28+
if:
29+
- value: '{{.function_args.name}}'
30+
operator: EQ
31+
expected: '{{.function_args.expected}}'
32+
then:
33+
this: SAID_HELLO
34+
message: 'Said hello to {{.function_args.name}}'

engine/step/step.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,23 @@ func PreRun(st *Step, values *values.Values, ss StateSetter, executedSteps map[s
477477
return
478478
}
479479

480+
// Check if we have a function as runner or not.
481+
runner, err := getRunner(st.Action.Type)
482+
if err != nil {
483+
ss(st.Name, StateServerError, err.Error())
484+
return
485+
}
486+
487+
_, isFunction := runner.(*functions.Function)
488+
if isFunction {
489+
var functionInput map[string]interface{}
490+
if err := utils.JSONnumberUnmarshal(bytes.NewBuffer(st.Action.Configuration), &functionInput); err != nil {
491+
ss(st.Name, StateServerError, err.Error())
492+
return
493+
}
494+
values.SetFunctionsArgs(functionInput)
495+
}
496+
480497
for _, sc := range conditions {
481498
if sc.Type != condition.SKIP {
482499
continue
@@ -521,6 +538,23 @@ func AfterRun(st *Step, values *values.Values, ss StateSetter) {
521538
return
522539
}
523540

541+
// Check if we have a function as runner or not.
542+
runner, err := getRunner(st.Action.Type)
543+
if err != nil {
544+
ss(st.Name, StateServerError, err.Error())
545+
return
546+
}
547+
548+
_, isFunction := runner.(*functions.Function)
549+
if isFunction {
550+
var functionInput map[string]interface{}
551+
if err := utils.JSONnumberUnmarshal(bytes.NewBuffer(st.Action.Configuration), &functionInput); err != nil {
552+
ss(st.Name, StateServerError, err.Error())
553+
return
554+
}
555+
values.SetFunctionsArgs(functionInput)
556+
}
557+
524558
for _, sc := range conditions {
525559
if sc.Type != condition.CHECK {
526560
continue
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: functionConditions
2+
description: Test function conditions
3+
title_format: "[test] Test function conditions"
4+
auto_runnable: true
5+
steps:
6+
asExpected:
7+
description: Say hello to foobar
8+
action:
9+
type: hello-with-conditions
10+
configuration:
11+
name: foobar
12+
expected: foobar
13+
14+
notExpected:
15+
description: Say hello to foo but expect bar
16+
action:
17+
type: hello-with-conditions
18+
configuration:
19+
name: foo
20+
expected: bar
21+
22+
skipped:
23+
description: Say hello to foo but expect bar
24+
action:
25+
type: hello-with-conditions
26+
configuration:
27+
name: foo
28+
expected: foor
29+
skip: true

0 commit comments

Comments
 (0)