Add option to panic in test.NewNullLogger to allow testing of
calls to `Fatal*` See #813
This commit is contained in:
+13
-1
@@ -39,12 +39,24 @@ func NewLocal(logger *logrus.Logger) *Hook {
|
||||
|
||||
}
|
||||
|
||||
type TestOption func(logger *logrus.Logger)
|
||||
|
||||
func FatalPanics(logger *logrus.Logger) {
|
||||
logger.Exit = func(code int) {
|
||||
panic(code)
|
||||
}
|
||||
}
|
||||
|
||||
// NewNullLogger creates a discarding logger and installs the test hook.
|
||||
func NewNullLogger() (*logrus.Logger, *Hook) {
|
||||
func NewNullLogger(options ...TestOption) (*logrus.Logger, *Hook) {
|
||||
|
||||
logger := logrus.New()
|
||||
logger.Out = ioutil.Discard
|
||||
|
||||
for _, option := range options {
|
||||
option(logger)
|
||||
}
|
||||
|
||||
return logger, NewLocal(logger)
|
||||
|
||||
}
|
||||
|
||||
@@ -71,3 +71,14 @@ func TestLoggingWithHooksRace(t *testing.T) {
|
||||
entries := hook.AllEntries()
|
||||
assert.Equal(100, len(entries))
|
||||
}
|
||||
|
||||
func TestFatalWithPanic(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
logger, hook := NewNullLogger(FatalPanics)
|
||||
|
||||
assert.Nil(hook.LastEntry())
|
||||
assert.Equal(0, len(hook.Entries))
|
||||
|
||||
assert.Panics(func() { logger.Fatal("something went wrong") })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user