forked from coop-cloud-mirrors/godotenv
Ignoring leading whitespace
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
This commit is contained in:
parent
69ed1d913a
commit
79711eebaf
@ -256,7 +256,7 @@ func parseLine(line string, envMap map[string]string) (key string, value string,
|
|||||||
if strings.HasPrefix(key, "export") {
|
if strings.HasPrefix(key, "export") {
|
||||||
key = strings.TrimPrefix(key, "export")
|
key = strings.TrimPrefix(key, "export")
|
||||||
}
|
}
|
||||||
key = strings.Trim(key, " ")
|
key = strings.TrimSpace(key)
|
||||||
|
|
||||||
// Parse the value
|
// Parse the value
|
||||||
value = parseValue(splitString[1], envMap)
|
value = parseValue(splitString[1], envMap)
|
||||||
@ -327,7 +327,7 @@ func expandVariables(v string, m map[string]string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isIgnoredLine(line string) bool {
|
func isIgnoredLine(line string) bool {
|
||||||
trimmedLine := strings.Trim(line, " \n\t")
|
trimmedLine := strings.TrimSpace(line)
|
||||||
return len(trimmedLine) == 0 || strings.HasPrefix(trimmedLine, "#")
|
return len(trimmedLine) == 0 || strings.HasPrefix(trimmedLine, "#")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var noopPresets = make(map[string]string)
|
var noopPresets = make(map[string]string)
|
||||||
@ -355,6 +355,11 @@ func TestParsing(t *testing.T) {
|
|||||||
parseAndCompare(t, `KEY="`, "KEY", "\"")
|
parseAndCompare(t, `KEY="`, "KEY", "\"")
|
||||||
parseAndCompare(t, `KEY="value`, "KEY", "\"value")
|
parseAndCompare(t, `KEY="value`, "KEY", "\"value")
|
||||||
|
|
||||||
|
// leading whitespace should be ignored
|
||||||
|
parseAndCompare(t, " KEY =value", "KEY", "value")
|
||||||
|
parseAndCompare(t, " KEY=value", "KEY", "value")
|
||||||
|
parseAndCompare(t, "\tKEY=value", "KEY", "value")
|
||||||
|
|
||||||
// it 'throws an error if line format is incorrect' do
|
// it 'throws an error if line format is incorrect' do
|
||||||
// expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
|
// expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
|
||||||
badlyFormattedLine := "lol$wut"
|
badlyFormattedLine := "lol$wut"
|
||||||
@ -371,6 +376,10 @@ func TestLinesToIgnore(t *testing.T) {
|
|||||||
t.Error("Line with nothing but line break wasn't ignored")
|
t.Error("Line with nothing but line break wasn't ignored")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !isIgnoredLine("\r\n") {
|
||||||
|
t.Error("Line with nothing but windows-style line break wasn't ignored")
|
||||||
|
}
|
||||||
|
|
||||||
if !isIgnoredLine("\t\t ") {
|
if !isIgnoredLine("\t\t ") {
|
||||||
t.Error("Line full of whitespace wasn't ignored")
|
t.Error("Line full of whitespace wasn't ignored")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user