Skip to content

Commit

Permalink
fix(dagent): adjust of env variables, empty string as valid value (#1009
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nandor-magyar authored Nov 7, 2024
1 parent 251571d commit 5c155b0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
6 changes: 6 additions & 0 deletions golang/internal/util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ func JoinV(separator string, items ...string) string {
return strings.Join(clean, separator)
}

// JoinV is a variadic alternative for strings.Join
// it keeps empty values
func JoinVEmpty(separator string, items ...string) string {
return strings.Join(append([]string{}, items...), separator)
}

// variadic string fallback, accepting string params
// returns the first non-empty value or empty if there is none
func Fallback(str ...string) string {
Expand Down
21 changes: 13 additions & 8 deletions golang/internal/util/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import (
)

func TestJoinV(t *testing.T) {
assert.Equal(t, util.JoinV(""), "")
assert.Equal(t, util.JoinV(":", "o", "o"), "o:o")
assert.Equal(t, util.JoinV("\\", "i"), "i")
assert.Equal(t, util.JoinV("/", "i", "i", "i"), "i/i/i")
assert.Equal(t, "", util.JoinV(""))
assert.Equal(t, "o:o", util.JoinV(":", "o", "o"))
assert.Equal(t, "i", util.JoinV("\\", "i"))
assert.Equal(t, "i/i/i", util.JoinV("/", "i", "i", "i"))
assert.Equal(t, "ENVKEY", util.JoinV("=", "ENVKEY", "")) // this is intentionally a bad example
}

func TestJoinVEmpty(t *testing.T) {
assert.Equal(t, "ENVKEY=", util.JoinVEmpty("=", "ENVKEY", "")) // this is intentionally a bad example
}

func TestFallback(t *testing.T) {
assert.Equal(t, util.Fallback(""), "")
assert.Equal(t, util.Fallback("1", "2", "3"), "1")
assert.Equal(t, util.Fallback("", "2"), "2")
assert.Equal(t, util.Fallback("", "", "", "4"), "4")
assert.Equal(t, "", util.Fallback(""))
assert.Equal(t, "1", util.Fallback("1", "2", "3"))
assert.Equal(t, "2", util.Fallback("", "2"))
assert.Equal(t, "4", util.Fallback("", "", "", "4"))
}
2 changes: 1 addition & 1 deletion golang/pkg/dagent/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func EnvMapToSlice(envs map[string]string) []string {
arr := []string{}

for key, value := range envs {
arr = append(arr, util.JoinV("=", key, value))
arr = append(arr, util.JoinVEmpty("=", key, value))
}

return arr
Expand Down

0 comments on commit 5c155b0

Please sign in to comment.