Add option to panic in test.NewNullLogger to allow testing of

calls to `Fatal*`

See #813
This commit is contained in:
Albert Salim
2018-10-06 18:08:19 +08:00
parent 1ed61965b9
commit 2be620216a
5 changed files with 35 additions and 7 deletions
+13 -1
View File
@@ -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)
}
+11
View File
@@ -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") })
}