-
Notifications
You must be signed in to change notification settings - Fork 0
/
zappretty_test.go
110 lines (88 loc) · 2.2 KB
/
zappretty_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
package zappretty
import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
gofuzz "github.com/google/gofuzz"
"github.com/fatih/color"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var epoch = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)
func TestMain(m *testing.M) {
color.NoColor = false
goleak.VerifyTestMain(m)
}
func TestPrettyOutput(t *testing.T) {
testcases := []struct {
name string
entry zapcore.Entry
want string
}{
{
name: "info with caller",
entry: zapcore.Entry{
Level: zapcore.InfoLevel,
Time: epoch,
LoggerName: "main",
Message: "hello world",
Caller: zapcore.EntryCaller{
Defined: true,
File: "foo.go",
Line: 42,
Function: "foo.Bar",
},
Stack: "foo",
},
want: fmt.Sprintf("\x1b[37m[%s]\x1b[0m \x1b[32mINFO \x1b[0m \x1b[90mmain\x1b[0m \x1b[90m(foo.go:42)\x1b[0m \x1b[97mhello world\x1b[0m \n", epoch.Format(timeFormat)),
},
}
for _, tc := range testcases {
encoder := NewCLIEncoder(EncoderTestEncoderConfig())
t.Run(tc.name, func(t *testing.T) {
out, err := encoder.EncodeEntry(tc.entry, nil)
assert.NoError(t, err)
assert.Equal(t, tc.want, out.String(), "Unexpected output")
})
}
}
func TestFuzzLog(t *testing.T) {
atom := zap.NewAtomicLevel()
cfg := zap.NewProductionEncoderConfig()
Register(cfg)
cliEncoder := NewCLIEncoder(cfg)
jsonEncoder := zapcore.NewJSONEncoder(cfg)
tmp, err := os.CreateTemp("", "test.*.log")
assert.NoError(t, err)
writer, closer, err := zap.Open(tmp.Name())
assert.NoError(t, err)
defer closer()
leveler := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level >= atom.Level()
})
core := zapcore.NewTee(
zapcore.NewCore(cliEncoder, zapcore.AddSync(ioutil.Discard), leveler),
zapcore.NewCore(jsonEncoder, writer, leveler),
)
logger := zap.New(core).Named("zappretty")
defer logger.Sync()
corpus := make([]string, 0)
f := gofuzz.New()
for i := 0; i < 1000; i++ {
var s string
f.Fuzz(&s)
corpus = append(corpus, s)
}
go func() {
for i := 0; i < 1000; i++ {
logger.Info(corpus[i])
}
}()
for i := 0; i < 1000; i++ {
logger.Info(corpus[i])
}
}