diff --git a/opts/envfile_test.go b/opts/envfile_test.go index 0119e7899178..bda760e0c367 100644 --- a/opts/envfile_test.go +++ b/opts/envfile_test.go @@ -3,9 +3,11 @@ package opts import ( "bufio" "os" - "reflect" "strings" "testing" + + "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" ) func tmpFileWithContent(t *testing.T, content string) string { @@ -37,6 +39,13 @@ with.dots=working and_underscore=working too single_quotes='quotes working' double_quotes="quotes working" +mixed_quotes1='quotes working" +mixed_quotes2="quotes working' +nested_quotes1="'quotes working'" +nested_quotes2='"quotes working"' +whitespace1= string with whitespace +whitespace2=' string with single-quoted whitespace ' +whitespace3=" string with double-quoted whitespace " ` // Adding a newline + a line with pure whitespace. // This is being done like this instead of the block above @@ -59,10 +68,16 @@ double_quotes="quotes working" "and_underscore=working too", "single_quotes=quotes working", "double_quotes=quotes working", - } - - if !reflect.DeepEqual(lines, expectedLines) { - t.Fatal("lines not equal to expectedLines") + `mixed_quotes1='quotes working"`, + `mixed_quotes2="quotes working'`, + `nested_quotes1='quotes working'`, + `nested_quotes2="quotes working"`, + `whitespace1= string with whitespace `, + `whitespace2= string with single-quoted whitespace `, + `whitespace3= string with double-quoted whitespace `, + } + for i, expected := range expectedLines { + assert.Check(t, is.Equal(lines[i], expected)) } } diff --git a/opts/file.go b/opts/file.go index 6b8ebc919dcc..2d92b74d5040 100644 --- a/opts/file.go +++ b/opts/file.go @@ -10,10 +10,7 @@ import ( "unicode/utf8" ) -const ( - whiteSpaces = " \t" - quotes = "'\"" -) +const whiteSpaces = " \t" // ErrBadKey typed error for bad environment variable type ErrBadKey struct { @@ -62,11 +59,7 @@ func parseKeyValueFile(filename string, emptyFn func(string) (string, bool)) ([] if len(data) > 1 { // pass the value through, trimming leading and trailing quotes - value := data[1] - value = strings.TrimLeft(value, quotes) - value = strings.TrimRight(value, quotes) - - lines = append(lines, fmt.Sprintf("%s=%s", variable, value)) + lines = append(lines, variable+"="+trimQuotes(data[1])) } else { var value string var present bool