Skip to content

Commit 9ccec54

Browse files
authored
Merge pull request #217 from compose-spec/inline-comments-unquoted
Handle inline comments on unquoted values
2 parents ad79316 + 8a0daf8 commit 9ccec54

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

dotenv/fixtures/plain.env

Lines changed: 2 additions & 1 deletion
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

dotenv/fixtures/quoted.env

Lines changed: 1 addition & 1 deletion
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

dotenv/godotenv_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func TestReadPlainEnv(t *testing.T) {
7979
"OPTION_E": "5",
8080
"OPTION_F": "",
8181
"OPTION_G": "",
82+
"OPTION_H": "my string",
8283
}
8384

8485
envMap, err := Read(envFileName)
@@ -92,7 +93,7 @@ func TestReadPlainEnv(t *testing.T) {
9293

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

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

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

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

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

dotenv/parser.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ 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
136135
if end < 0 {
137-
value := strings.TrimRightFunc(string(src), unicode.IsSpace)
138-
rest = nil
139-
return expandVariables(value, envMap, lookupFn), rest, nil
136+
value := strings.Split(string(src), "#")[0] // Remove inline comments on unquoted lines
137+
value = strings.TrimRightFunc(value, unicode.IsSpace)
138+
return expandVariables(value, envMap, lookupFn), nil, nil
140139
}
141140

142-
value = strings.TrimRightFunc(string(src[0:end]), unicode.IsSpace)
141+
value := strings.Split(string(src[0:end]), "#")[0]
142+
value = strings.TrimRightFunc(value, unicode.IsSpace)
143143
rest = src[end:]
144144
return expandVariables(value, envMap, lookupFn), rest, nil
145145
}

0 commit comments

Comments
 (0)