Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit d97b5f3

Browse files
committed
Handle inline comments on unquoted values
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 4122f16 commit d97b5f3

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

Diff for: fixtures/plain.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ OPTION_C= 3
44
OPTION_D =4
55
OPTION_E = 5
66
OPTION_F =
7-
OPTION_G=
7+
OPTION_G=
8+
OPTION_H = my string # Inline comment

Diff for: fixtures/quoted.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ OPTION_E="1"
66
OPTION_F="2"
77
OPTION_G=""
88
OPTION_H="\n"
9-
OPTION_I = "echo 'asd'"
9+
OPTION_I = "echo 'asd'" # Inline comment
1010
OPTION_J = 'first line
1111
second line
1212
third line

Diff for: godotenv_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func TestReadPlainEnv(t *testing.T) {
8080
"OPTION_E": "5",
8181
"OPTION_F": "",
8282
"OPTION_G": "",
83+
"OPTION_H": "my string",
8384
}
8485

8586
envMap, err := Read(envFileName)
@@ -93,7 +94,7 @@ func TestReadPlainEnv(t *testing.T) {
9394

9495
for key, value := range expectedValues {
9596
if envMap[key] != value {
96-
t.Error("Read got one of the keys wrong")
97+
t.Errorf("Read got one of the keys wrong. Expected: %q got %q", value, envMap[key])
9798
}
9899
}
99100
}
@@ -350,7 +351,7 @@ func TestParsing(t *testing.T) {
350351
// parses yaml style options
351352
parseAndCompare(t, "OPTION_A: 1", "OPTION_A", "1")
352353

353-
//parses yaml values with equal signs
354+
// parses yaml values with equal signs
354355
parseAndCompare(t, "OPTION_A: Foo=bar", "OPTION_A", "Foo=bar")
355356

356357
// parses non-yaml options with colons
@@ -399,7 +400,7 @@ func TestParsing(t *testing.T) {
399400
parseAndCompare(t, `FOO="ba#r"`, "FOO", "ba#r")
400401
parseAndCompare(t, "FOO='ba#r'", "FOO", "ba#r")
401402

402-
//newlines and backslashes should be escaped
403+
// newlines and backslashes should be escaped
403404
parseAndCompare(t, `FOO="bar\n\ b\az"`, "FOO", "bar\n baz")
404405
parseAndCompare(t, `FOO="bar\\\n\ b\az"`, "FOO", "bar\\\n baz")
405406
parseAndCompare(t, `FOO="bar\\r\ b\az"`, "FOO", "bar\\r baz")
@@ -483,14 +484,14 @@ func TestWrite(t *testing.T) {
483484
t.Errorf("Expected '%v' (%v) to write as '%v', got '%v' instead.", env, envMap, expected, actual)
484485
}
485486
}
486-
//just test some single lines to show the general idea
487-
//TestRoundtrip makes most of the good assertions
487+
// just test some single lines to show the general idea
488+
// TestRoundtrip makes most of the good assertions
488489

489-
//values are always double-quoted
490+
// values are always double-quoted
490491
writeAndCompare(`key=value`, `key="value"`)
491-
//double-quotes are escaped
492+
// double-quotes are escaped
492493
writeAndCompare(`key=va"lu"e`, `key="va\"lu\"e"`)
493-
//but single quotes are left alone
494+
// but single quotes are left alone
494495
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
495496
// newlines, backslashes, and some other special chars are escaped
496497
writeAndCompare(`foo="\n\r\\r!"`, `foo="\n\r\\r\!"`)

Diff for: parser.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,15 @@ func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (v
132132
// unquoted value - read until new line
133133
end := bytes.IndexFunc(src, isNewLine)
134134
var rest []byte
135-
var value string
135+
136136
if end < 0 {
137-
value := strings.TrimRightFunc(string(src), unicode.IsSpace)
137+
value := strings.Split(string(src), "#")[0] // Remove inline comments on unquoted lines
138+
value = strings.TrimRightFunc(value, unicode.IsSpace)
138139
rest = nil
139140
return expandVariables(value, envMap, lookupFn), rest, nil
140141
}
141-
142-
value = strings.TrimRightFunc(string(src[0:end]), unicode.IsSpace)
142+
value := strings.Split(string(src[0:end]), "#")[0]
143+
value = strings.TrimRightFunc(value, unicode.IsSpace)
143144
rest = src[end:]
144145
return expandVariables(value, envMap, lookupFn), rest, nil
145146
}
@@ -228,7 +229,6 @@ func isSpace(r rune) bool {
228229
return false
229230
}
230231

231-
232232
// isNewLine reports whether the rune is a new line character
233233
func isNewLine(r rune) bool {
234234
return r == '\n'

0 commit comments

Comments
 (0)