Skip to content

Commit

Permalink
Don't panic when invalid paths are configured
Browse files Browse the repository at this point in the history
If the config struct includes invalid output paths, make sure that we don't try
to execute a nil function pointer.

Fixes #390.
  • Loading branch information
Akshay Shah committed Mar 28, 2017
1 parent 7cb1af2 commit c73ef88
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,25 @@ func TestConfig(t *testing.T) {
})
}
}

func TestConfigWithInvalidPaths(t *testing.T) {
tests := []struct {
desc string
output string
errOutput string
}{
{"output directory doesn't exist", "/tmp/not-there/foo.log", "stderr"},
{"error output directory doesn't exist", "stdout", "/tmp/not-there/foo-errors.log"},
{"both output directories don't exist", "/tmp/not-there/foo.log", "/tmp/not-there/foo-errors.log"},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
cfg := NewProductionConfig()
cfg.OutputPaths = []string{tt.output}
cfg.ErrorOutputPaths = []string{tt.errOutput}
_, err := cfg.Build()
assert.Error(t, err, "Expected an error opening a non-existent directory.")
})
}
}
2 changes: 1 addition & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
func Open(paths ...string) (zapcore.WriteSyncer, func(), error) {
writers, close, err := open(paths)
if err != nil {
return nil, nil, err
return nil, close, err
}

writer := CombineWriteSyncers(writers...)
Expand Down

0 comments on commit c73ef88

Please sign in to comment.