Skip to content

Commit

Permalink
Allow variables to be set to empty string in envFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
sapenroo authored and danehlim committed Jul 17, 2023
1 parent 9999b1d commit e7ae769
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions agent/taskresource/envFiles/envfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,11 @@ func (envfile *EnvironmentFileResource) readEnvVarsFromFile(envfilePath string)
// only read the line that has "="
if strings.Contains(line, envVariableDelimiter) {
variables := strings.SplitN(line, envVariableDelimiter, 2)
// verify that there is at least a character on each side
if len(variables[0]) > 0 && len(variables[1]) > 0 {
// verify that the key is non-empty, the value can be empty
if len(variables[0]) > 0 {
envVars[variables[0]] = variables[1]
} else {
seelog.Infof("Not applying line %d of environment file %s, key or value is empty.", lineNum, envfilePath)
seelog.Infof("Not applying line %d of environment file %s, key is empty.", lineNum, envfilePath)
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions agent/taskresource/envFiles/envfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func TestReadEnvVarsFromEnvfiles(t *testing.T) {

envfileContentLine1 := "key1=value"
envFileContentLine2 := "key2=val1=val2"
envFileContentLine3 := "key3="

tempOpen := open
open = func(name string) (oswrapper.File, error) {
Expand All @@ -333,6 +334,8 @@ func TestReadEnvVarsFromEnvfiles(t *testing.T) {
mockScanner.EXPECT().Text().Return(envfileContentLine1),
mockScanner.EXPECT().Scan().Return(true),
mockScanner.EXPECT().Text().Return(envFileContentLine2),
mockScanner.EXPECT().Scan().Return(true),
mockScanner.EXPECT().Text().Return(envFileContentLine3),
mockScanner.EXPECT().Scan().Return(false),
mockScanner.EXPECT().Err().Return(nil),
)
Expand All @@ -343,6 +346,9 @@ func TestReadEnvVarsFromEnvfiles(t *testing.T) {
assert.Equal(t, 1, len(envVarsList))
assert.Equal(t, "value", envVarsList[0]["key1"])
assert.Equal(t, "val1=val2", envVarsList[0]["key2"])
key3Value, ok := envVarsList[0]["key3"]
assert.True(t, ok)
assert.Equal(t, "", key3Value)
}

func TestReadEnvVarsCommentFromEnvfiles(t *testing.T) {
Expand Down

0 comments on commit e7ae769

Please sign in to comment.