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

feat: add default value for var #549

Merged
merged 4 commits into from
Jun 15, 2022
Merged
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
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