Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open should only return close on success #396

Merged
merged 2 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,11 @@ func (cfg Config) buildOptions(errSink zapcore.WriteSyncer) []Option {
func (cfg Config) openSinks() (zapcore.WriteSyncer, zapcore.WriteSyncer, error) {
sink, closeOut, err := Open(cfg.OutputPaths...)
if err != nil {
closeOut()
return nil, nil, err
}
errSink, closeErr, err := Open(cfg.ErrorOutputPaths...)
errSink, _, err := Open(cfg.ErrorOutputPaths...)
if err != nil {
closeOut()
closeErr()
return nil, nil, err
}
return sink, errSink, nil
Expand Down
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"},
{"neither output directory exists", "/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.")
})
}
}
16 changes: 11 additions & 5 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func open(paths []string) ([]zapcore.WriteSyncer, func(), error) {
var errs multierror.Error
writers := make([]zapcore.WriteSyncer, 0, len(paths))
files := make([]*os.File, 0, len(paths))
close := func() {
for _, f := range files {
f.Close()
}
}
for _, path := range paths {
switch path {
case "stdout":
Expand All @@ -67,12 +72,13 @@ func open(paths []string) ([]zapcore.WriteSyncer, func(), error) {
files = append(files, f)
}
}
close := func() {
for _, f := range files {
f.Close()
}

if err := errs.AsError(); err != nil {
close()
return writers, nil, err
}
return writers, close, errs.AsError()

return writers, close, nil
}

// CombineWriteSyncers combines multiple WriteSyncers into a single, locked
Expand Down
25 changes: 24 additions & 1 deletion writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ func TestOpen(t *testing.T) {

for _, tt := range tests {
wss, cleanup, err := open(tt.paths)
defer cleanup()
if err == nil {
defer cleanup()
}

if tt.error == "" {
assert.NoError(t, err, "Unexpected error opening paths %v.", tt.paths)
} else {
Expand All @@ -82,6 +85,26 @@ func TestOpen(t *testing.T) {
}
}

func TestOpenFails(t *testing.T) {
tests := []struct {
paths []string
wantErr string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're not using this, can we remove it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}{
{
paths: []string{"./non-existent-dir/file"},
},
{
paths: []string{"stdout", "./non-existent-dir/file"},
},
}

for _, tt := range tests {
_, cleanup, err := Open(tt.paths...)
require.Nil(t, cleanup, "Cleanup function should never be nil")
assert.Error(t, err, "Open with non-existent directory should fail")
}
}

type testWriter struct {
expected string
t testing.TB
Expand Down