Merge pull request #22 from mmilata/dont-swallow-errors

Improve error handling
This commit is contained in:
John Barton 2016-12-17 10:05:37 +11:00 committed by GitHub
commit 726cc8b906
3 changed files with 28 additions and 3 deletions

2
fixtures/invalid1.env Normal file
View File

@ -0,0 +1,2 @@
INVALID LINE
foo=bar

View File

@ -143,13 +143,19 @@ func readFile(filename string) (envMap map[string]string, err error) {
lines = append(lines, scanner.Text()) lines = append(lines, scanner.Text())
} }
if err = scanner.Err(); err != nil {
return
}
for _, fullLine := range lines { for _, fullLine := range lines {
if !isIgnoredLine(fullLine) { if !isIgnoredLine(fullLine) {
key, value, err := parseLine(fullLine) var key, value string
key, value, err = parseLine(fullLine)
if err == nil { if err != nil {
envMap[key] = value return
} }
envMap[key] = value
} }
} }
return return

View File

@ -269,3 +269,20 @@ func TestLinesToIgnore(t *testing.T) {
t.Error("ignoring a perfectly valid line to parse") 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)
}
}
func TestErrorParsing(t *testing.T) {
envFileName := "fixtures/invalid1.env"
envMap, err := Read(envFileName)
if err == nil {
t.Errorf("Expected error, got %v", envMap)
}
}