From 0afb59c65e1d635338464b00f2aaad9414e2f2d9 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 1 Feb 2022 14:58:53 +1100 Subject: [PATCH] Tests can have multiple env variables --- .../doc/operators/env-variable-operators.md | 9 +- pkg/yqlib/doc/operators/eval.md | 4 +- pkg/yqlib/operator_env_test.go | 82 +++++++++---------- pkg/yqlib/operator_eval_test.go | 12 +-- pkg/yqlib/operators_test.go | 16 ++-- 5 files changed, 65 insertions(+), 58 deletions(-) diff --git a/pkg/yqlib/doc/operators/env-variable-operators.md b/pkg/yqlib/doc/operators/env-variable-operators.md index ea99429a..8f0d98db 100644 --- a/pkg/yqlib/doc/operators/env-variable-operators.md +++ b/pkg/yqlib/doc/operators/env-variable-operators.md @@ -77,7 +77,7 @@ will output a: "12" ``` -## Dynamically evaluate a path from an environment variable +## Dynamically update a path from an environment variable The env variable can be any valid yq expression. Given a sample.yml file of: @@ -89,11 +89,14 @@ a: ``` then ```bash -myenv=".a.b[0].name" yq 'eval(strenv(myenv))' sample.yml +valueEnv="moo" pathEnv=".a.b[0].name" yq 'eval(strenv(pathEnv)) = strenv(valueEnv)' sample.yml ``` will output ```yaml -dog +a: + b: + - name: moo + - name: cat ``` ## Dynamic key lookup with environment variable diff --git a/pkg/yqlib/doc/operators/eval.md b/pkg/yqlib/doc/operators/eval.md index 6601419d..a1489dc9 100644 --- a/pkg/yqlib/doc/operators/eval.md +++ b/pkg/yqlib/doc/operators/eval.md @@ -36,13 +36,13 @@ a: ``` then ```bash -myenv=".a.b[0].name" yq 'eval(strenv(myenv)) = "cow"' sample.yml +valueEnv="moo" pathEnv=".a.b[0].name" yq 'eval(strenv(pathEnv)) = strenv(valueEnv)' sample.yml ``` will output ```yaml a: b: - - name: cow + - name: moo - name: cat ``` diff --git a/pkg/yqlib/operator_env_test.go b/pkg/yqlib/operator_env_test.go index a762e58a..000b6faf 100644 --- a/pkg/yqlib/operator_env_test.go +++ b/pkg/yqlib/operator_env_test.go @@ -6,101 +6,101 @@ import ( var envOperatorScenarios = []expressionScenario{ { - description: "Read string environment variable", - environmentVariable: "cat meow", - expression: `.a = env(myenv)`, + description: "Read string environment variable", + environmentVariables: map[string]string{"myenv": "cat meow"}, + expression: `.a = env(myenv)`, expected: []string{ "D0, P[], ()::a: cat meow\n", }, }, { - description: "Read boolean environment variable", - environmentVariable: "true", - expression: `.a = env(myenv)`, + description: "Read boolean environment variable", + environmentVariables: map[string]string{"myenv": "true"}, + expression: `.a = env(myenv)`, expected: []string{ "D0, P[], ()::a: true\n", }, }, { - description: "Read numeric environment variable", - environmentVariable: "12", - expression: `.a = env(myenv)`, + description: "Read numeric environment variable", + environmentVariables: map[string]string{"myenv": "12"}, + expression: `.a = env(myenv)`, expected: []string{ "D0, P[], ()::a: 12\n", }, }, { - description: "Read yaml environment variable", - environmentVariable: "{b: fish}", - expression: `.a = env(myenv)`, + description: "Read yaml environment variable", + environmentVariables: map[string]string{"myenv": "{b: fish}"}, + expression: `.a = env(myenv)`, expected: []string{ "D0, P[], ()::a: {b: fish}\n", }, }, { - description: "Read boolean environment variable as a string", - environmentVariable: "true", - expression: `.a = strenv(myenv)`, + description: "Read boolean environment variable as a string", + environmentVariables: map[string]string{"myenv": "true"}, + expression: `.a = strenv(myenv)`, expected: []string{ "D0, P[], ()::a: \"true\"\n", }, }, { - description: "Read numeric environment variable as a string", - environmentVariable: "12", - expression: `.a = strenv(myenv)`, + description: "Read numeric environment variable as a string", + environmentVariables: map[string]string{"myenv": "12"}, + expression: `.a = strenv(myenv)`, expected: []string{ "D0, P[], ()::a: \"12\"\n", }, }, { - description: "Dynamically evaluate a path from an environment variable", - subdescription: "The env variable can be any valid yq expression.", - document: `{a: {b: [{name: dog}, {name: cat}]}}`, - environmentVariable: ".a.b[0].name", - expression: `eval(strenv(myenv))`, + description: "Dynamically update a path from an environment variable", + subdescription: "The env variable can be any valid yq expression.", + document: `{a: {b: [{name: dog}, {name: cat}]}}`, + environmentVariables: map[string]string{"pathEnv": ".a.b[0].name", "valueEnv": "moo"}, + expression: `eval(strenv(pathEnv)) = strenv(valueEnv)`, expected: []string{ - "D0, P[a b 0 name], (!!str)::dog\n", + "D0, P[], (doc)::{a: {b: [{name: moo}, {name: cat}]}}\n", }, }, { - description: "Dynamic key lookup with environment variable", - environmentVariable: "cat", - document: `{cat: meow, dog: woof}`, - expression: `.[env(myenv)]`, + description: "Dynamic key lookup with environment variable", + environmentVariables: map[string]string{"myenv": "cat"}, + document: `{cat: meow, dog: woof}`, + expression: `.[env(myenv)]`, expected: []string{ "D0, P[cat], (!!str)::meow\n", }, }, { - description: "Replace strings with envsubst", - environmentVariable: "cat", - expression: `"the ${myenv} meows" | envsubst`, + description: "Replace strings with envsubst", + environmentVariables: map[string]string{"myenv": "cat"}, + expression: `"the ${myenv} meows" | envsubst`, expected: []string{ "D0, P[], (!!str)::the cat meows\n", }, }, { - description: "Replace strings with envsubst, missing variables", - environmentVariable: "cat", - expression: `"the ${myenvnonexisting} meows" | envsubst`, + description: "Replace strings with envsubst, missing variables", + environmentVariables: map[string]string{"myenv": "cat"}, + expression: `"the ${myenvnonexisting} meows" | envsubst`, expected: []string{ "D0, P[], (!!str)::the meows\n", }, }, { - description: "Replace strings with envsubst, missing variables with defaults", - environmentVariable: "cat", - expression: `"the ${myenvnonexisting-dog} meows" | envsubst`, + description: "Replace strings with envsubst, missing variables with defaults", + environmentVariables: map[string]string{"myenv": "cat"}, + expression: `"the ${myenvnonexisting-dog} meows" | envsubst`, expected: []string{ "D0, P[], (!!str)::the dog meows\n", }, }, { - description: "Replace string environment variable in document", - environmentVariable: "cat meow", - document: "{v: \"${myenv}\"}", - expression: `.v |= envsubst`, + description: "Replace string environment variable in document", + environmentVariables: map[string]string{"myenv": "cat meow"}, + document: "{v: \"${myenv}\"}", + expression: `.v |= envsubst`, expected: []string{ "D0, P[], (doc)::{v: \"cat meow\"}\n", }, diff --git a/pkg/yqlib/operator_eval_test.go b/pkg/yqlib/operator_eval_test.go index 052f1178..3bff5a12 100644 --- a/pkg/yqlib/operator_eval_test.go +++ b/pkg/yqlib/operator_eval_test.go @@ -15,13 +15,13 @@ var evalOperatorScenarios = []expressionScenario{ }, }, { - description: "Dynamically update a path from an environment variable", - subdescription: "The env variable can be any valid yq expression.", - document: `{a: {b: [{name: dog}, {name: cat}]}}`, - environmentVariable: ".a.b[0].name", - expression: `eval(strenv(myenv)) = "cow"`, + description: "Dynamically update a path from an environment variable", + subdescription: "The env variable can be any valid yq expression.", + document: `{a: {b: [{name: dog}, {name: cat}]}}`, + environmentVariables: map[string]string{"pathEnv": ".a.b[0].name", "valueEnv": "moo"}, + expression: `eval(strenv(pathEnv)) = strenv(valueEnv)`, expected: []string{ - "D0, P[], (doc)::{a: {b: [{name: cow}, {name: cat}]}}\n", + "D0, P[], (doc)::{a: {b: [{name: moo}, {name: cat}]}}\n", }, }, } diff --git a/pkg/yqlib/operators_test.go b/pkg/yqlib/operators_test.go index e34d5c15..acba0ab6 100644 --- a/pkg/yqlib/operators_test.go +++ b/pkg/yqlib/operators_test.go @@ -18,7 +18,7 @@ import ( type expressionScenario struct { description string subdescription string - environmentVariable string + environmentVariables map[string]string document string document2 string expression string @@ -87,8 +87,8 @@ func testScenario(t *testing.T, s *expressionScenario) { } - if s.environmentVariable != "" { - os.Setenv("myenv", s.environmentVariable) + for name, value := range s.environmentVariables { + os.Setenv(name, value) } context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, node) @@ -231,9 +231,13 @@ func documentInput(w *bufio.Writer, s expressionScenario) (string, string) { envCommand := "" - if s.environmentVariable != "" { - envCommand = fmt.Sprintf("myenv=\"%v\" ", s.environmentVariable) - os.Setenv("myenv", s.environmentVariable) + for name, value := range s.environmentVariables { + if envCommand == "" { + envCommand = fmt.Sprintf("%v=\"%v\" ", name, value) + } else { + envCommand = fmt.Sprintf("%v %v=\"%v\" ", envCommand, name, value) + } + os.Setenv(name, value) } if s.document != "" {