Skip to content

Commit

Permalink
Add tests for OnLevelParseError
Browse files Browse the repository at this point in the history
  • Loading branch information
colega committed Aug 9, 2019
1 parent 0a19cbe commit b02b7a9
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions configs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package logrusiowriter

import (
"testing"

"github.com/sirupsen/logrus"
)

func TestWithConfig(t *testing.T) {
t.Run("can't parse level, configures info level by default", func(t *testing.T) {
expectedLevel := logrus.InfoLevel

cfg := Config{
Level: "none",
Fields: logrus.Fields{},
}

w := New(WithConfig(cfg))

configuredLevel := w.(*writer).level
if configuredLevel != expectedLevel {
t.Errorf("Configured level should be %s, but it was %s", expectedLevel, configuredLevel)
}
})

t.Run("custom OnLevelParseError", func(t *testing.T) {
originalOnLevelParseError := OnLevelParseError
defer func() { OnLevelParseError = originalOnLevelParseError }()

expectedLevel := logrus.WarnLevel

cfg := Config{
Level: "none",
Fields: logrus.Fields{},
}

var providedErr error
OnLevelParseError = func(err error) logrus.Level {
providedErr = err
return expectedLevel
}

w := New(WithConfig(cfg))

configuredLevel := w.(*writer).level
if configuredLevel != expectedLevel {
t.Errorf("Configured level should be %s, but it was %s", expectedLevel, configuredLevel)
}

if providedErr == nil {
t.Errorf("Error provided to OnLevelParseError should not be nil")
}
})
}

0 comments on commit b02b7a9

Please sign in to comment.