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

Make DPanic fatal in zaptest #1274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions zaptest/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func WrapOptions(zapOpts ...zap.Option) LoggerOption {
// You may also pass zap.Option's to customize test logger.
//
// logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))
//
// The logger is put into development mode so DPanic causes immediate panic.
func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger {
cfg := loggerOptions{
Level: zapcore.DebugLevel,
Expand All @@ -87,6 +89,8 @@ func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger {
// Send zap errors to the same writer and mark the test as failed if
// that happens.
zap.ErrorOutput(writer.WithMarkFailed(true)),
// Enable development mode to make DPanic fatal in tests.
zap.Development(),
}
zapOptions = append(zapOptions, cfg.zapOptions...)

Expand Down
7 changes: 7 additions & 0 deletions zaptest/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ func TestTestLoggerErrorOutput(t *testing.T) {
}
}

func TestDPanic(t *testing.T) {
log := NewLogger(t)
assert.Panics(t, func() {
log.DPanic("foo")
})
}

// testLogSpy is a testing.TB that captures logged messages.
type testLogSpy struct {
testing.TB
Expand Down