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

Commit 79711ee

Browse files
Ignoring leading whitespace
Signed-off-by: Dave Henderson <[email protected]>
1 parent 69ed1d9 commit 79711ee

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

godotenv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func parseLine(line string, envMap map[string]string) (key string, value string,
256256
if strings.HasPrefix(key, "export") {
257257
key = strings.TrimPrefix(key, "export")
258258
}
259-
key = strings.Trim(key, " ")
259+
key = strings.TrimSpace(key)
260260

261261
// Parse the value
262262
value = parseValue(splitString[1], envMap)
@@ -327,7 +327,7 @@ func expandVariables(v string, m map[string]string) string {
327327
}
328328

329329
func isIgnoredLine(line string) bool {
330-
trimmedLine := strings.Trim(line, " \n\t")
330+
trimmedLine := strings.TrimSpace(line)
331331
return len(trimmedLine) == 0 || strings.HasPrefix(trimmedLine, "#")
332332
}
333333

godotenv_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"os"
77
"reflect"
8-
"testing"
98
"strings"
9+
"testing"
1010
)
1111

1212
var noopPresets = make(map[string]string)
@@ -355,6 +355,11 @@ func TestParsing(t *testing.T) {
355355
parseAndCompare(t, `KEY="`, "KEY", "\"")
356356
parseAndCompare(t, `KEY="value`, "KEY", "\"value")
357357

358+
// leading whitespace should be ignored
359+
parseAndCompare(t, " KEY =value", "KEY", "value")
360+
parseAndCompare(t, " KEY=value", "KEY", "value")
361+
parseAndCompare(t, "\tKEY=value", "KEY", "value")
362+
358363
// it 'throws an error if line format is incorrect' do
359364
// expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
360365
badlyFormattedLine := "lol$wut"
@@ -371,6 +376,10 @@ func TestLinesToIgnore(t *testing.T) {
371376
t.Error("Line with nothing but line break wasn't ignored")
372377
}
373378

379+
if !isIgnoredLine("\r\n") {
380+
t.Error("Line with nothing but windows-style line break wasn't ignored")
381+
}
382+
374383
if !isIgnoredLine("\t\t ") {
375384
t.Error("Line full of whitespace wasn't ignored")
376385
}

0 commit comments

Comments
 (0)