From 861984c215bcdfd5113951cecf84939ec2675d59 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Mon, 12 Dec 2016 14:43:30 +0100 Subject: [PATCH] Don't hide line parsing errors --- fixtures/invalid1.env | 2 ++ godotenv.go | 8 +++++--- godotenv_test.go | 8 ++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 fixtures/invalid1.env diff --git a/fixtures/invalid1.env b/fixtures/invalid1.env new file mode 100644 index 0000000..38f7e0e --- /dev/null +++ b/fixtures/invalid1.env @@ -0,0 +1,2 @@ +INVALID LINE +foo=bar diff --git a/godotenv.go b/godotenv.go index d1b54bd..a098dd2 100644 --- a/godotenv.go +++ b/godotenv.go @@ -149,11 +149,13 @@ func readFile(filename string) (envMap map[string]string, err error) { for _, fullLine := range lines { if !isIgnoredLine(fullLine) { - key, value, err := parseLine(fullLine) + var key, value string + key, value, err = parseLine(fullLine) - if err == nil { - envMap[key] = value + if err != nil { + return } + envMap[key] = value } } return diff --git a/godotenv_test.go b/godotenv_test.go index 84f8d4b..2d26ee4 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -278,3 +278,11 @@ func TestErrorReadDirectory(t *testing.T) { t.Errorf("Expected error, got %v", envMap) } } + +func TestErrorParsing(t *testing.T) { + envFileName := "fixtures/invalid1.env" + envMap, err := Read(envFileName) + if err == nil { + t.Errorf("Expected error, got %v", envMap) + } +}