Skip to content

Commit 40b60bf

Browse files
authored
Merge pull request #32 from coder/fix/allow-empty-env-values
fix: allow empty environment variable values to override defaults
2 parents 3184310 + e9b0682 commit 40b60bf

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

‎option.go‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,7 @@ func (optSet *OptionSet) ParseEnv(vs []EnvVar) error {
292292
// because the agent lacked the environment variables to authenticate with Git.
293293
envVal, ok = envs[`HOMEBREW_`+opt.Env]
294294
}
295-
// Currently, empty values are treated as if the environment variable is
296-
// unset. This behavior is technically not correct as there is now no
297-
// way for a user to change a Default value to an empty string from
298-
// the environment. Unfortunately, we have old configuration files
299-
// that rely on the faulty behavior.
300-
//
301-
// TODO: We should remove this hack in May 2023, when deployments
302-
// have had months to migrate to the new behavior.
303-
if !ok || envVal == "" {
295+
if !ok {
304296
continue
305297
}
306298

‎option_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,35 @@ func TestOptionSet_ParseEnv(t *testing.T) {
152152

153153
err = os.ParseEnv(serpent.ParseEnviron([]string{"CODER_WORKSPACE_NAME="}, "CODER_"))
154154
require.NoError(t, err)
155+
// An explicitly empty environment variable should override the
156+
// default value, allowing users to clear a default.
157+
require.EqualValues(t, "", workspaceName)
158+
require.Equal(t, serpent.ValueSourceEnv, os[0].ValueSource)
159+
})
160+
161+
t.Run("EmptyValueUnset", func(t *testing.T) {
162+
t.Parallel()
163+
164+
var workspaceName serpent.String
165+
166+
os := serpent.OptionSet{
167+
serpent.Option{
168+
Name: "Workspace Name",
169+
Value: &workspaceName,
170+
Default: "defname",
171+
Env: "WORKSPACE_NAME",
172+
},
173+
}
174+
175+
err := os.SetDefaults()
176+
require.NoError(t, err)
177+
178+
// An env var that is not present at all should preserve the
179+
// default value.
180+
err = os.ParseEnv(serpent.ParseEnviron([]string{}, "CODER_"))
181+
require.NoError(t, err)
155182
require.EqualValues(t, "defname", workspaceName)
183+
require.Equal(t, serpent.ValueSourceDefault, os[0].ValueSource)
156184
})
157185

158186
t.Run("StringSlice", func(t *testing.T) {

0 commit comments

Comments
 (0)