forked from coop-cloud-mirrors/godotenv
Merge pull request #69 from hairyhenderson/ignore-leading-whitespace
Fixing a couple whitespace bugs: ignoring leading whitespace, and supporting more kinds of empty lines
This commit is contained in:
commit
5c0e6c6ab1
@ -252,6 +252,12 @@ func parseLine(line string, envMap map[string]string) (key string, value string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse the key
|
// Parse the key
|
||||||
|
key = splitString[0]
|
||||||
|
if strings.HasPrefix(key, "export") {
|
||||||
|
key = strings.TrimPrefix(key, "export")
|
||||||
|
}
|
||||||
|
key = strings.TrimSpace(key)
|
||||||
|
|
||||||
re := regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
|
re := regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
|
||||||
key = re.ReplaceAllString(splitString[0], "$1")
|
key = re.ReplaceAllString(splitString[0], "$1")
|
||||||
|
|
||||||
@ -324,7 +330,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, "#")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -362,6 +362,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"
|
||||||
@ -378,6 +383,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