-
Notifications
You must be signed in to change notification settings - Fork 1
/
configs_test.go
124 lines (106 loc) · 2.97 KB
/
configs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package logrusiowriter
import (
"bytes"
"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")
}
})
t.Run("trimming", func(t *testing.T) {
const newLineAppendedByLogrus = "\n"
for _, tc := range []struct {
desc string
trimming bool
input string
expected string
}{
{
desc: "zerolength with trimming",
trimming: true,
input: "",
expected: "level=info" + newLineAppendedByLogrus,
},
{
desc: "zerolength without trimming",
trimming: false,
input: "",
expected: "level=info" + newLineAppendedByLogrus,
},
{
desc: "only newline with trimming",
trimming: true,
input: "\n",
expected: "level=info" + newLineAppendedByLogrus,
},
{
desc: "only newline without trimming",
trimming: false,
input: "\n",
expected: `level=info msg="\n"` + newLineAppendedByLogrus,
},
{
desc: "with trailing newline and trimming",
trimming: true,
input: "message\n",
expected: "level=info msg=message" + newLineAppendedByLogrus,
},
{
desc: "with trailing newline and no trimming",
trimming: false,
input: "message\n",
expected: `level=info msg="message\n"` + newLineAppendedByLogrus,
},
{
desc: "no newline with trimming",
trimming: true,
input: "message",
expected: "level=info msg=message" + newLineAppendedByLogrus,
},
} {
t.Run(tc.desc, func(t *testing.T) {
buf := &bytes.Buffer{}
bufLogger := logrus.New()
bufLogger.SetOutput(buf)
bufLogger.Formatter.(*logrus.TextFormatter).DisableTimestamp = true
writer := New(WithLogger(bufLogger), WithTrailingNewLineTrimming(tc.trimming))
_, _ = writer.Write([]byte(tc.input))
if buf.String() != tc.expected {
t.Errorf("Unexpected output\nExpected: '%s'\nGot: '%s'", tc.expected, buf.String())
}
})
}
})
}