Skip to content

Commit

Permalink
feat: add pseudo expandEnv interpolate function (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored and bnjjj committed Nov 14, 2019
1 parent 2065661 commit afd163e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ Builtin variables:
* {{.venom.datetime}}
* {{.venom.timestamp}}

Venom templating

Beside venom variables, it is possible to use templating functions:

* expandEnv : {{expandEnv <filename>}}, rewrites the named file and replaces ${var} or $var in the string according to the values of the current environment variables. References to undefined variables are replaced by the empty string. You can use it a script step for instance: `script: cat {{expandEnv ./myFile}}`.

### Testsuite Versions

#### Version 2
Expand Down
4 changes: 4 additions & 0 deletions process_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func (v *Venom) parseTestCase(ts *TestSuite, tc *TestCase) ([]string, []string,

s := varRegEx.FindString(v)

if strings.HasPrefix(s, "{{expandEnv ") {
continue
}

for i := 0; i < len(extractedVars); i++ {
prefix := "{{." + extractedVars[i]
if strings.HasPrefix(s, prefix) {
Expand Down
26 changes: 24 additions & 2 deletions templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package venom
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -61,7 +64,7 @@ func (tmpl *Templater) ApplyOnStep(stepNumber int, step TestStep) (TestStep, err
return t, nil
}

//ApplyOnMap executes the template on a context
// ApplyOnMap executes the template on a context
// return true if there is an variable replaced
func (tmpl *Templater) ApplyOnMap(mapStringInterface map[string]interface{}) (bool, map[string]interface{}, error) {
var t map[string]interface{}
Expand All @@ -88,13 +91,32 @@ func (tmpl *Templater) ApplyOnMap(mapStringInterface map[string]interface{}) (bo
return applied, t, nil
}

var expandEnvRegEx = regexp.MustCompile("{{expandEnv (.*)}}")

func (tmpl *Templater) apply(in []byte) (bool, []byte) {
out := string(in)

if expandEnvRegEx.MatchString(out) {
capture := expandEnvRegEx.FindAllStringSubmatch(out, -1)[0]
if len(capture) > 0 {
filename := capture[1]
fileStat, _ := os.Stat(filename)
fileContent, _ := ioutil.ReadFile(filename)
if len(fileContent) != 0 {
newFileContent := os.ExpandEnv(string(fileContent))
err := ioutil.WriteFile(filename, []byte(newFileContent), fileStat.Mode().Perm())
if err == nil {
out = strings.Replace(out, capture[0], filename, -1)
}
}
}
}

tmpl.Add("", map[string]string{
"venom.datetime": time.Now().Format(time.RFC3339),
"venom.timestamp": fmt.Sprintf("%d", time.Now().Unix()),
})
var applied bool
out := string(in)
for k, v := range tmpl.Values {
applied = true
var buffer bytes.Buffer
Expand Down
10 changes: 10 additions & 0 deletions tests/MyTestSuiteWithExpand.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: MyTestSuite
version: "2"
testcases:
- name: testA
steps:
- type: exec
script: cat {{expandEnv TestExpand.txt}}
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldNotContainSubstring expandEnv
1 change: 1 addition & 0 deletions tests/TestExpand.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$HOME

0 comments on commit afd163e

Please sign in to comment.