Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2884aa6

Browse files
committed
fix: treat empty env as defaults (#6538)
1 parent 2b02e65 commit 2884aa6

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

cli/clibase/option.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ func (s *OptionSet) ParseEnv(vs []EnvVar) error {
121121
}
122122

123123
envVal, ok := envs[opt.Env]
124-
if !ok {
124+
// Currently, empty values are treated as if the environment variable is
125+
// unset. This behavior is technically not correct as there is now no
126+
// way for a user to change a Default value to an empty string from
127+
// the environment. Unfortunately, we have old configuration files
128+
// that rely on the faulty behavior.
129+
if !ok || envVal == "" {
125130
continue
126131
}
127132

cli/clibase/option_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,26 @@ func TestOptionSet_ParseEnv(t *testing.T) {
9393
require.NoError(t, err)
9494
require.EqualValues(t, "foo", workspaceName)
9595
})
96+
97+
t.Run("EmptyValue", func(t *testing.T) {
98+
t.Parallel()
99+
100+
var workspaceName clibase.String
101+
102+
os := clibase.OptionSet{
103+
clibase.Option{
104+
Name: "Workspace Name",
105+
Value: &workspaceName,
106+
Default: "defname",
107+
Env: "WORKSPACE_NAME",
108+
},
109+
}
110+
111+
err := os.SetDefaults()
112+
require.NoError(t, err)
113+
114+
err = os.ParseEnv(clibase.ParseEnviron([]string{"CODER_WORKSPACE_NAME="}, "CODER_"))
115+
require.NoError(t, err)
116+
require.EqualValues(t, "defname", workspaceName)
117+
})
96118
}

0 commit comments

Comments
 (0)