From 0ff0c0fc7aae2d2fed041c5f41622bdff3acd835 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Mon, 12 Dec 2016 14:41:36 +0100 Subject: [PATCH 1/2] Propagate errors encountered when reading file --- godotenv.go | 4 ++++ godotenv_test.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/godotenv.go b/godotenv.go index 94b2676..d1b54bd 100644 --- a/godotenv.go +++ b/godotenv.go @@ -143,6 +143,10 @@ func readFile(filename string) (envMap map[string]string, err error) { lines = append(lines, scanner.Text()) } + if err = scanner.Err(); err != nil { + return + } + for _, fullLine := range lines { if !isIgnoredLine(fullLine) { key, value, err := parseLine(fullLine) diff --git a/godotenv_test.go b/godotenv_test.go index e93539e..84f8d4b 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -269,3 +269,12 @@ func TestLinesToIgnore(t *testing.T) { t.Error("ignoring a perfectly valid line to parse") } } + +func TestErrorReadDirectory(t *testing.T) { + envFileName := "fixtures/" + envMap, err := Read(envFileName) + + if err == nil { + t.Errorf("Expected error, got %v", envMap) + } +} From 861984c215bcdfd5113951cecf84939ec2675d59 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Mon, 12 Dec 2016 14:43:30 +0100 Subject: [PATCH 2/2] 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) + } +}