Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,7 @@ func (optSet *OptionSet) ParseEnv(vs []EnvVar) error {
// because the agent lacked the environment variables to authenticate with Git.
envVal, ok = envs[`HOMEBREW_`+opt.Env]
}
// Currently, empty values are treated as if the environment variable is
// unset. This behavior is technically not correct as there is now no
// way for a user to change a Default value to an empty string from
// the environment. Unfortunately, we have old configuration files
// that rely on the faulty behavior.
//
// TODO: We should remove this hack in May 2023, when deployments
// have had months to migrate to the new behavior.
if !ok || envVal == "" {
if !ok {
continue
}

Expand Down
28 changes: 28 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,35 @@ func TestOptionSet_ParseEnv(t *testing.T) {

err = os.ParseEnv(serpent.ParseEnviron([]string{"CODER_WORKSPACE_NAME="}, "CODER_"))
require.NoError(t, err)
// An explicitly empty environment variable should override the
// default value, allowing users to clear a default.
require.EqualValues(t, "", workspaceName)
require.Equal(t, serpent.ValueSourceEnv, os[0].ValueSource)
})

t.Run("EmptyValueUnset", func(t *testing.T) {
t.Parallel()

var workspaceName serpent.String

os := serpent.OptionSet{
serpent.Option{
Name: "Workspace Name",
Value: &workspaceName,
Default: "defname",
Env: "WORKSPACE_NAME",
},
}

err := os.SetDefaults()
require.NoError(t, err)

// An env var that is not present at all should preserve the
// default value.
err = os.ParseEnv(serpent.ParseEnviron([]string{}, "CODER_"))
require.NoError(t, err)
require.EqualValues(t, "defname", workspaceName)
require.Equal(t, serpent.ValueSourceDefault, os[0].ValueSource)
})

t.Run("StringSlice", func(t *testing.T) {
Expand Down
Loading