config: accept env vars set to "", discern from unset

Fixes #7831 to a certain extent.

Signed-off-by: Stephan Renatus <stephan@styra.com>
This commit is contained in:
Stephan Renatus
2025-08-11 20:08:37 +02:00
committed by Stephan Renatus
parent deb2cecced
commit 4a5d9735c8
2 changed files with 17 additions and 2 deletions
+2 -1
View File
@@ -138,7 +138,8 @@ func subEnvVars(s string) string {
// Lookup the variable in the environment. We do not
// play by bash rules: if its undefined we'll keep it
// as-is, it could be replaced somewhere down the line.
if lu := os.Getenv(varName); lu != "" {
// If it's set to "", we'll return that.
if lu, ok := os.LookupEnv(varName); ok {
return lu
}
return s
+15 -1
View File
@@ -96,7 +96,7 @@ func TestSubEnvVarsVarsSubMissingEnvVar(t *testing.T) {
envKey := setTestEnvVar(t, "var1", "foo")
configYaml := fmt.Sprintf("field1: '${%s}'", envKey)
// Remove the env var and expect the system to sub in ""
// Remove the env var and expect the system to keep the key as-is
os.Unsetenv(envKey)
expected := configYaml // untouched
@@ -118,6 +118,20 @@ func TestSubEnvVarsVarsSubEmptyVarName(t *testing.T) {
}
}
func TestSubEnvVarsVarsSubEmptyEnvVar(t *testing.T) {
envKey := setTestEnvVar(t, "var1", "foo")
configYaml := fmt.Sprintf("field1: '${%s}'", envKey)
t.Setenv(envKey, "")
expected := "field1: ''"
actual := subEnvVars(configYaml)
if actual != expected {
t.Errorf("Expected: '%s'\nActual: '%s'", expected, actual)
}
}
func TestMergeValuesNoOverride(t *testing.T) {
dest := map[string]any{}
src := map[string]any{