fix: if a line contains multiple # characters, there will be issues w… (#238)

* fix: if a line contains multiple # characters, there will be issues when traversing from back to front

* fix: typo
main v1.6.0-pre.2
柚子uccs 2024-12-16 12:14:19 +08:00 committed by GitHub
parent a7f6c4c583
commit 3a7a190201
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 14 deletions

View File

@ -1,4 +1,7 @@
# Full line comment # Full line comment
qux=thud # fred # other
thud=fred#qux # other
fred=qux#baz # other # more
foo=bar # baz foo=bar # baz
bar=foo#baz bar=foo#baz
baz="foo"#bar baz="foo"#bar

View File

@ -477,9 +477,12 @@ func TestErrorParsing(t *testing.T) {
func TestComments(t *testing.T) { func TestComments(t *testing.T) {
envFileName := "fixtures/comments.env" envFileName := "fixtures/comments.env"
expectedValues := map[string]string{ expectedValues := map[string]string{
"foo": "bar", "qux": "thud",
"bar": "foo#baz", "thud": "fred#qux",
"baz": "foo", "fred": "qux#baz",
"foo": "bar",
"bar": "foo#baz",
"baz": "foo",
} }
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
@ -588,42 +591,42 @@ func TestWhitespace(t *testing.T) {
}{ }{
"Leading whitespace": { "Leading whitespace": {
input: " A=a\n", input: " A=a\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Leading tab": { "Leading tab": {
input: "\tA=a\n", input: "\tA=a\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Leading mixed whitespace": { "Leading mixed whitespace": {
input: " \t \t\n\t \t A=a\n", input: " \t \t\n\t \t A=a\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Leading whitespace before export": { "Leading whitespace before export": {
input: " \t\t export A=a\n", input: " \t\t export A=a\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Trailing whitespace": { "Trailing whitespace": {
input: "A=a \t \t\n", input: "A=a \t \t\n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Trailing whitespace with export": { "Trailing whitespace with export": {
input: "export A=a\t \t \n", input: "export A=a\t \t \n",
key: "A", key: "A",
value: "a", value: "a",
}, },
"No EOL": { "No EOL": {
input: "A=a", input: "A=a",
key: "A", key: "A",
value: "a", value: "a",
}, },
"Trailing whitespace with no EOL": { "Trailing whitespace with no EOL": {
input: "A=a ", input: "A=a ",
key: "A", key: "A",
value: "a", value: "a",
}, },
} }

View File

@ -143,9 +143,9 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
} }
// Work backwards to check if the line ends in whitespace then // Work backwards to check if the line ends in whitespace then
// a comment (ie asdasd # some comment) // a comment, ie: foo=bar # baz # other
for i := endOfVar - 1; i >= 0; i-- { for i := 0; i < endOfVar; i++ {
if line[i] == charComment && i > 0 { if line[i] == charComment && i < endOfVar {
if isSpace(line[i-1]) { if isSpace(line[i-1]) {
endOfVar = i endOfVar = i
break break