Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing a couple whitespace bugs: ignoring leading whitespace, and supporting more kinds of empty lines #69

Merged
merged 2 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ func parseLine(line string, envMap map[string]string) (key string, value string,
}

// Parse the key
re := regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
key = splitString[0]
if strings.HasPrefix(key, "export") {
key = strings.TrimPrefix(key, "export")
}
key = strings.TrimSpace(key)

re := regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
key = re.ReplaceAllString(splitString[0], "$1")

// Parse the value
Expand Down Expand Up @@ -324,7 +330,7 @@ func expandVariables(v string, m map[string]string) string {
}

func isIgnoredLine(line string) bool {
trimmedLine := strings.Trim(line, " \n\t")
trimmedLine := strings.TrimSpace(line)
return len(trimmedLine) == 0 || strings.HasPrefix(trimmedLine, "#")
}

Expand Down
9 changes: 9 additions & 0 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ func TestParsing(t *testing.T) {
parseAndCompare(t, `KEY="`, "KEY", "\"")
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
// expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
badlyFormattedLine := "lol$wut"
Expand All @@ -378,6 +383,10 @@ func TestLinesToIgnore(t *testing.T) {
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 ") {
t.Error("Line full of whitespace wasn't ignored")
}
Expand Down