Skip to content
Merged
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
25 changes: 20 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 @@ -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
Expand All @@ -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))
}
}

Expand Down
11 changes: 2 additions & 9 deletions opts/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import (
"unicode/utf8"
)

const (
whiteSpaces = " \t"
quotes = "'\""
)
const whiteSpaces = " \t"

// ErrBadKey typed error for bad environment variable
type ErrBadKey struct {
Expand Down Expand Up @@ -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
Expand Down