Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env file quotes handling #3660

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 24 additions & 5 deletions opts/envfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -35,6 +37,15 @@ func TestParseEnvFileGoodFile(t *testing.T) {
_foobar=foobaz
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
Expand All @@ -55,10 +66,18 @@ and_underscore=working too
"_foobar=foobaz",
"with.dots=working",
"and_underscore=working too",
}

if !reflect.DeepEqual(lines, expectedLines) {
t.Fatal("lines not equal to expectedLines")
"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 `,
}
for i, expected := range expectedLines {
assert.Check(t, is.Equal(lines[i], expected))
}
}

Expand Down
4 changes: 2 additions & 2 deletions opts/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func parseKeyValueFile(filename string, emptyFn func(string) (string, bool)) ([]
}

if len(data) > 1 {
// pass the value through, no trimming
lines = append(lines, fmt.Sprintf("%s=%s", variable, data[1]))
// pass the value through, trimming leading and trailing quotes
lines = append(lines, variable+"="+trimQuotes(data[1]))
} else {
var value string
var present bool
Expand Down