yq/pkg/yqlib/operator_env_test.go

116 lines
3.5 KiB
Go
Raw Normal View History

2021-01-08 10:07:46 +00:00
package yqlib
import (
"testing"
)
var envOperatorScenarios = []expressionScenario{
2021-01-09 01:06:19 +00:00
{
2022-02-01 03:58:53 +00:00
description: "Read string environment variable",
environmentVariables: map[string]string{"myenv": "cat meow"},
expression: `.a = env(myenv)`,
2021-01-09 01:06:19 +00:00
expected: []string{
"D0, P[], ()::a: cat meow\n",
},
},
{
2022-02-01 03:58:53 +00:00
description: "Read boolean environment variable",
environmentVariables: map[string]string{"myenv": "true"},
expression: `.a = env(myenv)`,
2021-01-09 01:06:19 +00:00
expected: []string{
"D0, P[], ()::a: true\n",
},
},
{
2022-02-01 03:58:53 +00:00
description: "Read numeric environment variable",
environmentVariables: map[string]string{"myenv": "12"},
expression: `.a = env(myenv)`,
2021-01-09 01:06:19 +00:00
expected: []string{
"D0, P[], ()::a: 12\n",
},
},
{
2022-02-01 03:58:53 +00:00
description: "Read yaml environment variable",
environmentVariables: map[string]string{"myenv": "{b: fish}"},
expression: `.a = env(myenv)`,
2021-01-09 01:06:19 +00:00
expected: []string{
"D0, P[], ()::a: {b: fish}\n",
},
},
2021-01-08 10:07:46 +00:00
{
2022-02-01 03:58:53 +00:00
description: "Read boolean environment variable as a string",
environmentVariables: map[string]string{"myenv": "true"},
expression: `.a = strenv(myenv)`,
2021-01-08 10:07:46 +00:00
expected: []string{
2021-01-09 01:06:19 +00:00
"D0, P[], ()::a: \"true\"\n",
2021-01-08 10:07:46 +00:00
},
},
{
2022-02-01 03:58:53 +00:00
description: "Read numeric environment variable as a string",
environmentVariables: map[string]string{"myenv": "12"},
expression: `.a = strenv(myenv)`,
2021-01-09 01:06:19 +00:00
expected: []string{
"D0, P[], ()::a: \"12\"\n",
},
},
2022-02-01 03:47:51 +00:00
{
2022-02-01 03:58:53 +00:00
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)`,
2022-02-01 03:47:51 +00:00
expected: []string{
2022-02-01 03:58:53 +00:00
"D0, P[], (doc)::{a: {b: [{name: moo}, {name: cat}]}}\n",
2022-02-01 03:47:51 +00:00
},
},
2021-01-09 01:06:19 +00:00
{
2022-02-01 03:58:53 +00:00
description: "Dynamic key lookup with environment variable",
environmentVariables: map[string]string{"myenv": "cat"},
document: `{cat: meow, dog: woof}`,
expression: `.[env(myenv)]`,
2021-01-08 10:07:46 +00:00
expected: []string{
2021-01-09 01:06:19 +00:00
"D0, P[cat], (!!str)::meow\n",
2021-01-08 10:07:46 +00:00
},
},
2022-01-26 22:20:53 +00:00
{
2022-02-01 03:58:53 +00:00
description: "Replace strings with envsubst",
environmentVariables: map[string]string{"myenv": "cat"},
expression: `"the ${myenv} meows" | envsubst`,
2022-01-26 22:20:53 +00:00
expected: []string{
"D0, P[], (!!str)::the cat meows\n",
},
},
{
2022-02-01 03:58:53 +00:00
description: "Replace strings with envsubst, missing variables",
environmentVariables: map[string]string{"myenv": "cat"},
expression: `"the ${myenvnonexisting} meows" | envsubst`,
2022-01-26 22:20:53 +00:00
expected: []string{
"D0, P[], (!!str)::the meows\n",
},
},
{
2022-02-01 03:58:53 +00:00
description: "Replace strings with envsubst, missing variables with defaults",
environmentVariables: map[string]string{"myenv": "cat"},
expression: `"the ${myenvnonexisting-dog} meows" | envsubst`,
2022-01-26 22:20:53 +00:00
expected: []string{
"D0, P[], (!!str)::the dog meows\n",
},
},
{
2022-02-01 03:58:53 +00:00
description: "Replace string environment variable in document",
environmentVariables: map[string]string{"myenv": "cat meow"},
document: "{v: \"${myenv}\"}",
expression: `.v |= envsubst`,
2022-01-26 22:20:53 +00:00
expected: []string{
"D0, P[], (doc)::{v: \"cat meow\"}\n",
},
},
2021-01-08 10:07:46 +00:00
}
func TestEnvOperatorScenarios(t *testing.T) {
for _, tt := range envOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "env-variable-operators", envOperatorScenarios)
2021-01-08 10:07:46 +00:00
}