Skip to content

Commit

Permalink
add support for {{env(VAR)}} filling
Browse files Browse the repository at this point in the history
  • Loading branch information
jcdietrich committed Apr 11, 2024
1 parent bc4d9e2 commit 1f734e8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/match/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func (mm Request) matchKeyAndValues(reqMap mock.Values, mockMap mock.Values) boo
return false
}
} else {
log.Debugf("value %v doesn't appear in mock", key)
log.Debugf("value %v doesn't appear in mock", key)

return false
return false
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/vars/env_vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package vars

import (
"os"
"regexp"
"strings"
)

var paramRe = regexp.MustCompile(`(?m)\((.*)\)`)

type EnvVars struct {
valueMap map[string]string
ready bool
}

func (ev EnvVars) Fill(holders []string) map[string][]string {
vars := make(map[string][]string)
for _, tag := range holders {
if strings.HasPrefix(tag, "env(") {
var name = ev.getInputParam(tag)
log.Debugf("looking for envar %s", name)
vars[tag] = append(vars[tag], os.Getenv(name))
}
}
return vars
}

func (ev EnvVars) getInputParam(param string) string {
match := paramRe.FindStringSubmatch(param)
if len(match) > 1 {
return match[1]
}
return ""
}
58 changes: 58 additions & 0 deletions pkg/vars/env_vars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package vars

import (
"fmt"
"math/rand"
"os"
"strings"
"testing"
)

const Name = "___MMOCK__TEST__ENV__VAR"

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randSeq(n int) string {
randString := make([]rune, n)
for i := range randString {
randString[i] = letters[rand.Intn(len(letters))]
}
return string(randString)
}

func TestEnvVar(t *testing.T) {
var value = randSeq(18)
os.Setenv(Name, value)

var tag = fmt.Sprintf("env(%s)", Name)
holders := []string{tag}
env := EnvVars{}

result := env.Fill(holders)
foundValue, found := result[tag]

os.Unsetenv(Name)

if !found {
t.Errorf("tag %s was not found", tag)
}

if !strings.Contains(foundValue[0], value) {
t.Errorf("EnvVar %s Expected: %s, Actual: %s", Name, value, foundValue[0])
}

var varNotPresent = "__NOT_PRESENT___"

tag = fmt.Sprintf("env(%s)", varNotPresent)
holders = []string{tag}
env = EnvVars{}

result = env.Fill(holders)
foundValue, found = result[tag]

os.Unsetenv(varNotPresent)

if found && foundValue[0] != "" {
t.Errorf("tag %s was found with value %v", tag, foundValue[0])
}
}
2 changes: 2 additions & 0 deletions pkg/vars/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition) {
requestFiller := fp.FillerFactory.CreateRequestFiller(req, m)
fakeFiller := fp.FillerFactory.CreateFakeFiller()
streamFiller := fp.FillerFactory.CreateStreamFiller()
envFiller := fp.FillerFactory.CreateEnvVarFiller()

//replace stream holders for their content
fp.walkAndFill(m, streamFiller.Fill(fp.walkAndGet(m.Response)))
Expand All @@ -36,6 +37,7 @@ func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition) {
//fill holders
vars := requestFiller.Fill(holders)
fp.mergeVars(vars, fakeFiller.Fill(holders))
fp.mergeVars(vars, envFiller.Fill(holders))
fp.walkAndFill(m, vars)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/vars/filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type FillerFactory interface {
CreateRequestFiller(req *mock.Request, mock *mock.Definition) Filler
CreateFakeFiller() Filler
CreateStreamFiller() Filler
CreateEnvVarFiller() Filler
}

type MockFillerFactory struct {
Expand All @@ -35,3 +36,8 @@ func (mff MockFillerFactory) CreateFakeFiller() Filler {
func (mff MockFillerFactory) CreateStreamFiller() Filler {
return Stream{}
}

func (mff MockFillerFactory) CreateEnvVarFiller() Filler {

return EnvVars{}
}

0 comments on commit 1f734e8

Please sign in to comment.