Skip to content

Commit

Permalink
feat: add default value for var (#549)
Browse files Browse the repository at this point in the history
* feat: add default value for var

Signed-off-by: guowei shieh <guowei.shieh@outreach.io>
  • Loading branch information
guoweis-work committed Jun 15, 2022
1 parent d9088e5 commit 67e4628
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ Available helpers and some examples:
To be able to reuse a property from a teststep in a following testcase or step, you have to extract the variable, as the following example.

After the first step execution, `venom` extracts a value using a regular expression `foo with a ([a-z]+) here` from the content of the `result.systemout` property returned by the `executor`.
Then this variable can be reused in another test, with the name `testA.myvariable` with `testA` corresponding to the name of the testcase.
Then this variable can be reused in another test, with the name `testA.myvariable` with `testA` corresponding to the name of the testcase. A default value could also be supplied if the variable can't be extracted from the output, which can commonly happen when parsing json output.

```yaml
name: MyTestSuite
Expand All @@ -499,6 +499,7 @@ testcases:
myvariable:
from: result.systemout
regex: foo with a ([a-z]+) here
default: "somevalue"

- name: testB
steps:
Expand Down
9 changes: 6 additions & 3 deletions process_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,12 @@ func processVariableAssigments(ctx context.Context, tcName string, tcVars H, raw
if !has {
varValue, has = tcVars[tcName+"."+assigment.From]
if !has {
err := fmt.Errorf("%s reference not found in %s", assigment.From, strings.Join(tcVarsKeys, "\n"))
Info(ctx, "%v", err)
return nil, true, err
if assigment.Default == nil {
err := fmt.Errorf("%s reference not found in %s", assigment.From, strings.Join(tcVarsKeys, "\n"))
Info(ctx, "%v", err)
return nil, true, err
}
varValue = assigment.Default
}
}
if assigment.Regex == "" {
Expand Down
14 changes: 13 additions & 1 deletion tests/exec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ testcases:
- script: cat exec/testa.json
info: "the value of result.systemoutjson is {{.result.systemoutjson}}"
assertions:
- result.systemoutjson.foo ShouldContainSubstring bar
- result.systemoutjson.foo ShouldContainSubstring bar
vars:
foo:
from: result.systemoutjson.foo
bar:
from: result.systemoutjson.notexisting
default: "test"

- name: verify default
steps:
- script: echo "{{.cat-json.foo}} {{.cat-json.bar}}"
assertions:
- result.systemout ShouldEqual "bar test"
5 changes: 3 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ type AssignStep struct {
}

type Assignment struct {
From string `json:"from" yaml:"from"`
Regex string `json:"regex" yaml:"regex"`
From string `json:"from" yaml:"from"`
Regex string `json:"regex" yaml:"regex"`
Default interface{} `json:"default" yaml:"default"`
}

// RemoveNotPrintableChar removes not printable chararacter from a string
Expand Down

0 comments on commit 67e4628

Please sign in to comment.