Skip to content

Commit

Permalink
fix: apply templater on vars (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored and fsamin committed Aug 16, 2018
1 parent 9ec0e1b commit 3229a22
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
8 changes: 4 additions & 4 deletions executors/exec/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ func (Executor) Run(testCaseContext venom.TestCaseContext, l venom.Logger, step
// Create a tmp file
tmpscript, errt := ioutil.TempFile(os.TempDir(), "venom-")
if errt != nil {
return nil, fmt.Errorf("Cannot create tmp file: %s\n", errt)
return nil, fmt.Errorf("cannot create tmp file: %s\n", errt)
}

// Put script in file
l.Debugf("work with tmp file %s", tmpscript)
l.Debugf("work with tmp file %s", tmpscript.Name())
n, errw := tmpscript.Write([]byte(scriptContent))
if errw != nil || n != len(scriptContent) {
if errw != nil {
return nil, fmt.Errorf("Cannot write script: %s\n", errw)
return nil, fmt.Errorf("cannot write script: %s\n", errw)
}
return nil, fmt.Errorf("cannot write all script: %d/%d\n", n, len(scriptContent))
}
Expand All @@ -113,7 +113,7 @@ func (Executor) Run(testCaseContext venom.TestCaseContext, l venom.Logger, step
//and add .PS1 extension
newPath = newPath + ".PS1"
if err := os.Rename(oldPath, newPath); err != nil {
return nil, fmt.Errorf("cannot rename script to add powershell Extension, aborting\n")
return nil, fmt.Errorf("cannot rename script to add powershell extension, aborting\n")
}
//This aims to stop a the very first error and return the right exit code
psCommand := fmt.Sprintf("& { $ErrorActionPreference='Stop'; & %s ;exit $LastExitCode}", newPath)
Expand Down
3 changes: 1 addition & 2 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func (v *Venom) Parse(path []string, exclude []string) error {
extractedVars := []string{}
for i := range v.testsuites {
ts := &v.testsuites[i]
log.Info("Parsing testsuite", ts.Package)

log.Info("Parsing testsuite ", ts.Package)
tvars, textractedVars, err := v.parseTestSuite(ts)
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions process_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"runtime/pprof"
"strings"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -42,6 +43,21 @@ func (v *Venom) runTestSuite(ts *TestSuite) {
ts.Templater.Add("", map[string]string{"venom.testsuite": ts.ShortName})
ts.Templater.Add("", map[string]string{"venom.testsuite.filename": ts.Filename})

// we apply templater on current vars only
for index := 0; index < 10; index++ {
var toApply bool
for k, v := range ts.Templater.Values {
if strings.Contains(v, "{{") {
toApply = true
_, s := ts.Templater.apply([]byte(v))
ts.Templater.Values[k] = string(s)
}
}
if !toApply {
break
}
}

totalSteps := 0
for _, tc := range ts.TestCases {
totalSteps += len(tc.TestSteps)
Expand Down
6 changes: 5 additions & 1 deletion tests/MyTestSuiteWithVenomBuiltinVar.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
name: MyTestSuite
vars:
short: "a short"
long: "{{.short}} and a long"
testcases:
- name: testA
steps:
- type: exec
script: echo '{{.venom.testsuite}} {{.venom.testsuite.filename}} {{.venom.testcase}} {{.venom.teststep.number}} {{.venom.datetime}} {{.venom.timestamp}}'
script: echo '{{.venom.testsuite}} {{.venom.testsuite.filename}} {{.venom.testcase}} {{.venom.teststep.number}} {{.venom.datetime}} {{.venom.timestamp}} {{.short}} {{.long}}'
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring MyTestSuite
- result.systemout ShouldContainSubstring testA
- result.systemout ShouldContainSubstring "a short a short and a long"
- result.systemout ShouldContainSubstring 0

0 comments on commit 3229a22

Please sign in to comment.