Skip to content

Commit 861984c

Browse files
committed
Don't hide line parsing errors
1 parent 0ff0c0f commit 861984c

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

fixtures/invalid1.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INVALID LINE
2+
foo=bar

godotenv.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ func readFile(filename string) (envMap map[string]string, err error) {
149149

150150
for _, fullLine := range lines {
151151
if !isIgnoredLine(fullLine) {
152-
key, value, err := parseLine(fullLine)
152+
var key, value string
153+
key, value, err = parseLine(fullLine)
153154

154-
if err == nil {
155-
envMap[key] = value
155+
if err != nil {
156+
return
156157
}
158+
envMap[key] = value
157159
}
158160
}
159161
return

godotenv_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,11 @@ func TestErrorReadDirectory(t *testing.T) {
278278
t.Errorf("Expected error, got %v", envMap)
279279
}
280280
}
281+
282+
func TestErrorParsing(t *testing.T) {
283+
envFileName := "fixtures/invalid1.env"
284+
envMap, err := Read(envFileName)
285+
if err == nil {
286+
t.Errorf("Expected error, got %v", envMap)
287+
}
288+
}

0 commit comments

Comments
 (0)