-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty_test.go
401 lines (365 loc) Β· 9.82 KB
/
pretty_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package zl
import (
"bytes"
"errors"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io"
"log"
"os"
"os/exec"
"strings"
"testing"
)
func Test_newPrettyLogger(t *testing.T) {
tests := []struct {
name string
setOutputType Output
setOmitKeys []Key
out io.Writer
expectedOutput *os.File
expectedFlags int
expectedNil bool
}{
{
name: "not PrettyOutput type",
setOutputType: ConsoleOutput,
out: os.Stderr,
expectedOutput: os.Stderr,
expectedNil: true,
},
{
name: "not set options",
setOutputType: PrettyOutput,
out: os.Stderr,
expectedOutput: os.Stderr,
expectedFlags: log.Ldate | log.Ltime | log.Lshortfile,
},
{
name: "set omitKeys",
setOutputType: PrettyOutput,
setOmitKeys: []Key{TimeKey},
out: os.Stderr,
expectedOutput: os.Stderr,
expectedFlags: log.Lshortfile,
},
{
name: "set isStdOut and omitKeys",
setOutputType: PrettyOutput,
setOmitKeys: []Key{TimeKey},
out: os.Stdout,
expectedOutput: os.Stdout,
expectedFlags: log.Lshortfile,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup
outputType = tt.setOutputType
omitKeys = tt.setOmitKeys
// Execute
logger := newPrettyLogger(tt.out, os.Stderr)
// Assert
if tt.expectedNil {
assert.Nil(t, logger)
} else {
assert.Equal(t, logger.Logger.Writer(), tt.expectedOutput)
assert.Equal(t, logger.Logger.Flags(), tt.expectedFlags)
}
ResetGlobalLoggerSettings()
})
}
}
// faultyWriter always returns an error when Write is called.
type faultyWriter struct{}
func (fw *faultyWriter) Write(p []byte) (n int, err error) {
return 0, errors.New("forced writer error")
}
func Test_prettyLogger_log(t *testing.T) {
tests := []struct {
severityLevel zapcore.Level
level zapcore.Level
message string
expectedMsg string
}{
{
zapcore.DebugLevel,
zapcore.DebugLevel,
"Debug Message",
"\u001B[90mDEBUG\u001B[0m \u001B[2mDebug Message\u001B[0m",
},
{
zapcore.DebugLevel,
zapcore.InfoLevel,
"Info Message",
"\u001B[94mINFO\u001B[0m Info Message",
},
{
zapcore.DebugLevel,
zapcore.WarnLevel,
"Warn Message",
"\u001B[33mWARN\u001B[0m Warn Message",
},
{
zapcore.DebugLevel,
zapcore.ErrorLevel,
"Error Message",
"\u001B[31mERROR\u001B[0m Error Message",
},
{
zapcore.DebugLevel,
zapcore.FatalLevel,
"Fatal Message",
"\u001B[31mFATAL\u001B[0m Fatal Message",
},
{
zapcore.ErrorLevel,
zapcore.InfoLevel,
"Debug Message",
"",
},
}
for _, tt := range tests {
t.Run(tt.severityLevel.String()+"_"+tt.level.String()+"_Level", func(t *testing.T) {
outputType = PrettyOutput
omitKeys = []Key{TimeKey}
severityLevel = tt.severityLevel
var buf bytes.Buffer
logger := newPrettyLogger(&buf, os.Stderr)
logger.log(tt.message, tt.level, nil)
assert.Contains(t, buf.String(), tt.expectedMsg)
if tt.expectedMsg == "" {
assert.Empty(t, buf.String())
}
ResetGlobalLoggerSettings()
})
}
t.Run("capture internal error", func(t *testing.T) {
outputType = PrettyOutput
severityLevel = zapcore.DebugLevel
var buf bytes.Buffer
l := newPrettyLogger(&faultyWriter{}, &buf)
l.log("test message", zapcore.InfoLevel, nil)
assert.Contains(t, buf.String(), "[INTERNAL ERROR] ")
})
}
func Test_prettyLogger_logWithError(t *testing.T) {
tests := []struct {
severityLevel zapcore.Level
level zapcore.Level
message string
err error
expectedMsg string
}{
{
zapcore.DebugLevel,
zapcore.DebugLevel,
"Debug Message",
errors.New("some error"),
"\u001B[90mDEBUG\u001B[0m \u001B[2mDebug Message",
},
{
zapcore.DebugLevel,
zapcore.InfoLevel,
"Info Message",
errors.New("some error"),
"\u001B[94mINFO\u001B[0m Info Message",
},
{
zapcore.DebugLevel,
zapcore.WarnLevel,
"Warn Message",
errors.New("some error"),
"\u001B[33mWARN\u001B[0m Warn Message",
},
{
zapcore.DebugLevel,
zapcore.ErrorLevel,
"Error Message",
errors.New("some error"),
"\u001B[31mERROR\u001B[0m Error Message",
},
{
zapcore.DebugLevel,
zapcore.FatalLevel,
"Fatal Message",
errors.New("some error"),
"\u001B[31mFATAL\u001B[0m Fatal Message",
},
{
zapcore.ErrorLevel,
zapcore.InfoLevel,
"Debug Message",
errors.New("some error"),
"",
},
}
for _, tt := range tests {
t.Run(tt.severityLevel.String()+"_"+tt.level.String()+"_Level", func(t *testing.T) {
outputType = PrettyOutput
omitKeys = []Key{TimeKey}
severityLevel = tt.severityLevel
var buf bytes.Buffer
logger := newPrettyLogger(&buf, os.Stderr)
logger.logWithError(tt.message, tt.level, tt.err, nil)
assert.Contains(t, buf.String(), tt.expectedMsg)
if tt.expectedMsg == "" {
assert.Empty(t, buf.String())
}
ResetGlobalLoggerSettings()
})
}
t.Run("capture internal error", func(t *testing.T) {
outputType = PrettyOutput
severityLevel = zapcore.DebugLevel
var buf bytes.Buffer
l := newPrettyLogger(&faultyWriter{}, &buf)
l.logWithError("test message", zapcore.InfoLevel, errors.New("some error"), nil)
assert.Contains(t, buf.String(), "[INTERNAL ERROR] ")
})
t.Run("when error is nil", func(t *testing.T) {
outputType = PrettyOutput
severityLevel = zapcore.DebugLevel
var buf bytes.Buffer
l := newPrettyLogger(&buf, os.Stderr)
l.logWithError("test message", zapcore.InfoLevel, nil, nil)
assert.Contains(t, buf.String(), "<nil>")
})
}
func Test_prettyLogger_coloredLevel(t *testing.T) {
// Initialize the prettyLogger instance
logger := &prettyLogger{}
// Create tt cases
tests := []struct {
level zapcore.Level
expected string
}{
{level: zapcore.FatalLevel, expected: "\u001B[31mFATAL\u001B[0m"},
{level: zapcore.ErrorLevel, expected: "\u001B[31mERROR\u001B[0m"},
{level: zapcore.WarnLevel, expected: "\u001B[33mWARN\u001B[0m"},
{level: zapcore.InfoLevel, expected: "\u001B[94mINFO\u001B[0m"},
{level: zapcore.DebugLevel, expected: "\u001B[90mDEBUG\u001B[0m"},
{level: zapcore.Level(10 /* Undefined level */), expected: "\u001B[90m\u001B[0m"},
}
// Run test cases
for _, tt := range tests {
t.Run(tt.level.String(), func(t *testing.T) {
coloredString := logger.coloredLevel(tt.level).String()
assert.Equal(t, tt.expected, coloredString)
ResetGlobalLoggerSettings()
})
}
}
func Test_prettyLogger_showErrorReport(t *testing.T) {
t.Run("no error", func(t *testing.T) {
// Prepare expected string
expected := "" +
"\n" +
"\n\u001B[1;31mERROR REPORT" +
"\n\u001B[0m \u001B[36mErrorCount\u001B[0m: 1" +
"\n \u001B[36mPID\u001B[0m: 123" +
"\n\n" +
"\n\u001B[1m1\u001B[0m. pretty_test.go:218: \u001B[31mERROR\u001B[0m SOME_ERROR \u001B[35msome error\u001B[0m" +
"\n \u001B[36mTimestamp\u001B[0m:\t2023-09-09T15:53:17.287179+09:00" +
"\n \u001B[36mLogFile\u001B[0m:\t/PATH/TO/PROJECT/ROOT/testdata/basic.jsonl:1" +
"\n \u001B[36mStackTrace\u001B[0m: " +
"\n\tgithub.com/nkmr-jp/zl.Test_prettyLogger_showErrorReport" +
"\n\t\t/PATH/TO/PROJECT/ROOT/pretty_test.go:218" +
"\n\ttesting.tRunner" +
"\n\t\t/PATH/TO/GO/ROOT/src/testing/testing.go:1595" +
"\n\n\n"
// Prepare Logger
var buf bytes.Buffer
l := newPrettyLogger(&buf, os.Stderr)
// Execute
fileName = "./testdata/basic.jsonl"
l.showErrorReport(fileName, 123)
str := buf.String()
// Replace
goModPath, err := exec.Command("go", "env", "GOMOD").CombinedOutput()
assert.NoError(t, err)
pjRoot := strings.ReplaceAll(string(goModPath), "/go.mod\n", "")
replacedStr := strings.ReplaceAll(str, pjRoot, "/PATH/TO/PROJECT/ROOT")
// Assert
assert.Equal(t, expected, replacedStr)
ResetGlobalLoggerSettings()
})
t.Run("error", func(t *testing.T) {
tests := []struct {
name string
fileName string
pid int
output io.Writer
expected string
}{
{
name: "file not found",
fileName: "./testdata/not-found.jsonl",
pid: 123,
output: os.Stderr,
expected: "no such file or directory",
},
{
name: "faulty log file",
fileName: "./testdata/faulty.jsonl",
pid: 123,
output: os.Stderr,
expected: "unexpected end of JSON input",
},
{
name: "faulty writer",
fileName: "./testdata/basic.jsonl",
pid: 123,
output: &faultyWriter{},
expected: "forced writer error",
},
}
// Run test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Prepare
var errBuf bytes.Buffer
l := newPrettyLogger(tt.output, &errBuf)
// Execute
l.showErrorReport(tt.fileName, tt.pid)
// Assert
assert.Contains(t, errBuf.String(), tt.expected)
ResetGlobalLoggerSettings()
})
}
})
}
func Test_prettyLogger_consoleMsg(t *testing.T) {
var buf bytes.Buffer
l := newPrettyLogger(&buf, os.Stderr)
consoleFields = []string{"name", "id"}
expected := separator + "\u001B[36mAlice\u001B[0m" + separator + "\u001B[34m1\u001B[0m"
actual := l.consoleMsg([]zap.Field{
zap.String("name", "Alice"),
zap.Int("id", 1),
})
assert.Equal(t, expected, actual)
}
func Test_prettyLogger_dump(t *testing.T) {
t.Run("dump", func(t *testing.T) {
var buf bytes.Buffer
l := newPrettyLogger(&buf, os.Stderr)
l.dump("test dump")
assert.Contains(t, buf.String(), "\u001B[1;31mDUMP\u001B[0m (string) (len=9) \"test dump\"\n")
})
t.Run("not prettyOutput", func(t *testing.T) {
var buf bytes.Buffer
outputType = ConsoleOutput
l := newPrettyLogger(&buf, os.Stderr)
l.dump("test dump")
assert.Empty(t, buf.String())
ResetGlobalLoggerSettings()
})
t.Run("faulty output", func(t *testing.T) {
var errBuf bytes.Buffer
l := newPrettyLogger(&faultyWriter{}, &errBuf)
l.dump("test dump")
assert.Contains(t, errBuf.String(), "[INTERNAL ERROR] ")
})
}