mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Tests can have multiple env variables
This commit is contained in:
parent
535799462f
commit
0afb59c65e
@ -77,7 +77,7 @@ will output
|
|||||||
a: "12"
|
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.
|
The env variable can be any valid yq expression.
|
||||||
|
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
@ -89,11 +89,14 @@ a:
|
|||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```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
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
dog
|
a:
|
||||||
|
b:
|
||||||
|
- name: moo
|
||||||
|
- name: cat
|
||||||
```
|
```
|
||||||
|
|
||||||
## Dynamic key lookup with environment variable
|
## Dynamic key lookup with environment variable
|
||||||
|
@ -36,13 +36,13 @@ a:
|
|||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```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
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
a:
|
a:
|
||||||
b:
|
b:
|
||||||
- name: cow
|
- name: moo
|
||||||
- name: cat
|
- name: cat
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
var envOperatorScenarios = []expressionScenario{
|
var envOperatorScenarios = []expressionScenario{
|
||||||
{
|
{
|
||||||
description: "Read string environment variable",
|
description: "Read string environment variable",
|
||||||
environmentVariable: "cat meow",
|
environmentVariables: map[string]string{"myenv": "cat meow"},
|
||||||
expression: `.a = env(myenv)`,
|
expression: `.a = env(myenv)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], ()::a: cat meow\n",
|
"D0, P[], ()::a: cat meow\n",
|
||||||
@ -15,7 +15,7 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Read boolean environment variable",
|
description: "Read boolean environment variable",
|
||||||
environmentVariable: "true",
|
environmentVariables: map[string]string{"myenv": "true"},
|
||||||
expression: `.a = env(myenv)`,
|
expression: `.a = env(myenv)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], ()::a: true\n",
|
"D0, P[], ()::a: true\n",
|
||||||
@ -23,7 +23,7 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Read numeric environment variable",
|
description: "Read numeric environment variable",
|
||||||
environmentVariable: "12",
|
environmentVariables: map[string]string{"myenv": "12"},
|
||||||
expression: `.a = env(myenv)`,
|
expression: `.a = env(myenv)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], ()::a: 12\n",
|
"D0, P[], ()::a: 12\n",
|
||||||
@ -31,7 +31,7 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Read yaml environment variable",
|
description: "Read yaml environment variable",
|
||||||
environmentVariable: "{b: fish}",
|
environmentVariables: map[string]string{"myenv": "{b: fish}"},
|
||||||
expression: `.a = env(myenv)`,
|
expression: `.a = env(myenv)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], ()::a: {b: fish}\n",
|
"D0, P[], ()::a: {b: fish}\n",
|
||||||
@ -39,7 +39,7 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Read boolean environment variable as a string",
|
description: "Read boolean environment variable as a string",
|
||||||
environmentVariable: "true",
|
environmentVariables: map[string]string{"myenv": "true"},
|
||||||
expression: `.a = strenv(myenv)`,
|
expression: `.a = strenv(myenv)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], ()::a: \"true\"\n",
|
"D0, P[], ()::a: \"true\"\n",
|
||||||
@ -47,25 +47,25 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Read numeric environment variable as a string",
|
description: "Read numeric environment variable as a string",
|
||||||
environmentVariable: "12",
|
environmentVariables: map[string]string{"myenv": "12"},
|
||||||
expression: `.a = strenv(myenv)`,
|
expression: `.a = strenv(myenv)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], ()::a: \"12\"\n",
|
"D0, P[], ()::a: \"12\"\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Dynamically evaluate a path from an environment variable",
|
description: "Dynamically update a path from an environment variable",
|
||||||
subdescription: "The env variable can be any valid yq expression.",
|
subdescription: "The env variable can be any valid yq expression.",
|
||||||
document: `{a: {b: [{name: dog}, {name: cat}]}}`,
|
document: `{a: {b: [{name: dog}, {name: cat}]}}`,
|
||||||
environmentVariable: ".a.b[0].name",
|
environmentVariables: map[string]string{"pathEnv": ".a.b[0].name", "valueEnv": "moo"},
|
||||||
expression: `eval(strenv(myenv))`,
|
expression: `eval(strenv(pathEnv)) = strenv(valueEnv)`,
|
||||||
expected: []string{
|
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",
|
description: "Dynamic key lookup with environment variable",
|
||||||
environmentVariable: "cat",
|
environmentVariables: map[string]string{"myenv": "cat"},
|
||||||
document: `{cat: meow, dog: woof}`,
|
document: `{cat: meow, dog: woof}`,
|
||||||
expression: `.[env(myenv)]`,
|
expression: `.[env(myenv)]`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
@ -74,7 +74,7 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Replace strings with envsubst",
|
description: "Replace strings with envsubst",
|
||||||
environmentVariable: "cat",
|
environmentVariables: map[string]string{"myenv": "cat"},
|
||||||
expression: `"the ${myenv} meows" | envsubst`,
|
expression: `"the ${myenv} meows" | envsubst`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!str)::the cat meows\n",
|
"D0, P[], (!!str)::the cat meows\n",
|
||||||
@ -82,7 +82,7 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Replace strings with envsubst, missing variables",
|
description: "Replace strings with envsubst, missing variables",
|
||||||
environmentVariable: "cat",
|
environmentVariables: map[string]string{"myenv": "cat"},
|
||||||
expression: `"the ${myenvnonexisting} meows" | envsubst`,
|
expression: `"the ${myenvnonexisting} meows" | envsubst`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!str)::the meows\n",
|
"D0, P[], (!!str)::the meows\n",
|
||||||
@ -90,7 +90,7 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Replace strings with envsubst, missing variables with defaults",
|
description: "Replace strings with envsubst, missing variables with defaults",
|
||||||
environmentVariable: "cat",
|
environmentVariables: map[string]string{"myenv": "cat"},
|
||||||
expression: `"the ${myenvnonexisting-dog} meows" | envsubst`,
|
expression: `"the ${myenvnonexisting-dog} meows" | envsubst`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!str)::the dog meows\n",
|
"D0, P[], (!!str)::the dog meows\n",
|
||||||
@ -98,7 +98,7 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Replace string environment variable in document",
|
description: "Replace string environment variable in document",
|
||||||
environmentVariable: "cat meow",
|
environmentVariables: map[string]string{"myenv": "cat meow"},
|
||||||
document: "{v: \"${myenv}\"}",
|
document: "{v: \"${myenv}\"}",
|
||||||
expression: `.v |= envsubst`,
|
expression: `.v |= envsubst`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
|
@ -18,10 +18,10 @@ var evalOperatorScenarios = []expressionScenario{
|
|||||||
description: "Dynamically update a path from an environment variable",
|
description: "Dynamically update a path from an environment variable",
|
||||||
subdescription: "The env variable can be any valid yq expression.",
|
subdescription: "The env variable can be any valid yq expression.",
|
||||||
document: `{a: {b: [{name: dog}, {name: cat}]}}`,
|
document: `{a: {b: [{name: dog}, {name: cat}]}}`,
|
||||||
environmentVariable: ".a.b[0].name",
|
environmentVariables: map[string]string{"pathEnv": ".a.b[0].name", "valueEnv": "moo"},
|
||||||
expression: `eval(strenv(myenv)) = "cow"`,
|
expression: `eval(strenv(pathEnv)) = strenv(valueEnv)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (doc)::{a: {b: [{name: cow}, {name: cat}]}}\n",
|
"D0, P[], (doc)::{a: {b: [{name: moo}, {name: cat}]}}\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ import (
|
|||||||
type expressionScenario struct {
|
type expressionScenario struct {
|
||||||
description string
|
description string
|
||||||
subdescription string
|
subdescription string
|
||||||
environmentVariable string
|
environmentVariables map[string]string
|
||||||
document string
|
document string
|
||||||
document2 string
|
document2 string
|
||||||
expression string
|
expression string
|
||||||
@ -87,8 +87,8 @@ func testScenario(t *testing.T, s *expressionScenario) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.environmentVariable != "" {
|
for name, value := range s.environmentVariables {
|
||||||
os.Setenv("myenv", s.environmentVariable)
|
os.Setenv(name, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, node)
|
context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, node)
|
||||||
@ -231,9 +231,13 @@ func documentInput(w *bufio.Writer, s expressionScenario) (string, string) {
|
|||||||
|
|
||||||
envCommand := ""
|
envCommand := ""
|
||||||
|
|
||||||
if s.environmentVariable != "" {
|
for name, value := range s.environmentVariables {
|
||||||
envCommand = fmt.Sprintf("myenv=\"%v\" ", s.environmentVariable)
|
if envCommand == "" {
|
||||||
os.Setenv("myenv", s.environmentVariable)
|
envCommand = fmt.Sprintf("%v=\"%v\" ", name, value)
|
||||||
|
} else {
|
||||||
|
envCommand = fmt.Sprintf("%v %v=\"%v\" ", envCommand, name, value)
|
||||||
|
}
|
||||||
|
os.Setenv(name, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.document != "" {
|
if s.document != "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user