yq/pkg/yqlib/operator_env_test.go

106 lines
2.7 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
{
description: "Read string environment variable",
environmentVariable: "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)`,
expected: []string{
"D0, P[], ()::a: true\n",
},
},
{
description: "Read numeric environment variable",
environmentVariable: "12",
expression: `.a = env(myenv)`,
expected: []string{
"D0, P[], ()::a: 12\n",
},
},
{
description: "Read yaml environment variable",
environmentVariable: "{b: fish}",
expression: `.a = env(myenv)`,
expected: []string{
"D0, P[], ()::a: {b: fish}\n",
},
},
2021-01-08 10:07:46 +00:00
{
2021-01-08 10:09:43 +00:00
description: "Read boolean environment variable as a string",
environmentVariable: "true",
2021-01-09 01:06:19 +00:00
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
},
},
{
2021-01-08 10:09:43 +00:00
description: "Read numeric environment variable as a string",
environmentVariable: "12",
2021-01-09 01:06:19 +00:00
expression: `.a = strenv(myenv)`,
expected: []string{
"D0, P[], ()::a: \"12\"\n",
},
},
{
description: "Dynamic key lookup with environment variable",
environmentVariable: "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
{
description: "Replace strings with envsubst",
environmentVariable: "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`,
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`,
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`,
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
}