forked from charmbracelet/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_test.go
436 lines (424 loc) · 11.4 KB
/
text_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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package log
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func _zeroTime(time.Time) time.Time {
return time.Time{}.AddDate(1, 0, 0)
}
func TestNilStyles(t *testing.T) {
st := DefaultStyles()
l := New(io.Discard)
l.SetStyles(nil)
assert.Equal(t, st, l.styles)
}
func TestTextCaller(t *testing.T) {
var buf bytes.Buffer
logger := New(&buf)
logger.SetReportCaller(true)
// We calculate the caller offset based on the caller line number.
_, file, line, _ := runtime.Caller(0)
cases := []struct {
name string
expected string
msg string
kvs []interface{}
f func(msg interface{}, kvs ...interface{})
}{
{
name: "simple caller",
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+14),
msg: "info",
kvs: nil,
f: func(msg interface{}, kvs ...interface{}) {
logger.Info(msg, kvs...)
},
},
{
name: "helper caller",
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+58),
msg: "info",
kvs: nil,
f: func(msg interface{}, kvs ...interface{}) {
logger.Helper()
logger.Info(msg, kvs...)
},
},
{
name: "nested helper caller",
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+37),
msg: "info",
kvs: nil,
f: func(msg interface{}, kvs ...interface{}) {
fun := func(msg interface{}, kvs ...interface{}) {
logger.Helper()
logger.Info(msg, kvs...)
}
fun(msg, kvs...)
},
},
{
name: "double nested helper caller",
expected: fmt.Sprintf("INFO <log/%s:%d> info\n", filepath.Base(file), line+58),
msg: "info",
kvs: nil,
f: func(msg interface{}, kvs ...interface{}) {
logger.Helper()
fun := func(msg interface{}, kvs ...interface{}) {
logger.Helper()
logger.Info(msg, kvs...)
}
fun(msg, kvs...)
},
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
c.f(c.msg, c.kvs...)
assert.Equal(t, c.expected, buf.String())
})
}
}
func TestTextLogger(t *testing.T) {
var buf bytes.Buffer
logger := New(&buf)
cases := []struct {
name string
expected string
msg string
kvs []interface{}
f func(msg interface{}, kvs ...interface{})
}{
{
name: "simple message",
expected: "INFO info\n",
msg: "info",
kvs: nil,
f: logger.Info,
},
{
name: "ignored message",
expected: "",
msg: "this is a debug message",
kvs: nil,
f: logger.Debug,
},
{
name: "message with keyvals",
expected: "INFO info key1=val1 key2=val2\n",
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2"},
f: logger.Info,
},
{
name: "error message with keyvals",
expected: "ERRO info key1=val1 key2=val2\n",
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2"},
f: logger.Error,
},
{
name: "error message with multiline",
expected: "ERRO info\n key1=\n │ val1\n │ val2\n",
msg: "info",
kvs: []interface{}{"key1", "val1\nval2"},
f: logger.Error,
},
{
name: "odd number of keyvals",
expected: "ERRO info key1=val1 key2=val2 key3=\"missing value\"\n",
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2", "key3"},
f: logger.Error,
},
{
name: "error field",
expected: "ERRO info key1=\"error value\"\n",
msg: "info",
kvs: []interface{}{"key1", errors.New("error value")},
f: logger.Error,
},
{
name: "struct field",
expected: "ERRO info key1={foo:bar}\n",
msg: "info",
kvs: []interface{}{"key1", struct{ foo string }{foo: "bar"}},
f: logger.Error,
},
{
name: "struct field quoted",
expected: "ERRO info key1=\"{foo:bar baz}\"\n",
msg: "info",
kvs: []interface{}{"key1", struct{ foo string }{foo: "bar baz"}},
f: logger.Error,
},
{
name: "slice of strings",
expected: "ERRO info key1=\"[foo bar]\"\n",
msg: "info",
kvs: []interface{}{"key1", []string{"foo", "bar"}},
f: logger.Error,
},
{
name: "slice of structs",
expected: "ERRO info key1=\"[{foo:bar} {foo:baz}]\"\n",
msg: "info",
kvs: []interface{}{"key1", []struct{ foo string }{{foo: "bar"}, {foo: "baz"}}},
f: logger.Error,
},
{
name: "slice of errors",
expected: "ERRO info key1=\"[error value1 error value2]\"\n",
msg: "info",
kvs: []interface{}{"key1", []error{errors.New("error value1"), errors.New("error value2")}},
f: logger.Error,
},
{
name: "map of strings",
expected: "ERRO info key1=\"map[baz:qux foo:bar]\"\n",
msg: "info",
kvs: []interface{}{"key1", map[string]string{"foo": "bar", "baz": "qux"}},
f: logger.Error,
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
c.f(c.msg, c.kvs...)
assert.Equal(t, c.expected, buf.String())
})
}
}
func TestTextHelper(t *testing.T) {
var buf bytes.Buffer
logger := New(&buf)
logger.SetReportCaller(true)
helper := func() {
logger.Helper()
logger.Info("helper func")
}
helper()
_, file, line, ok := runtime.Caller(0)
require.True(t, ok)
assert.Equal(t, fmt.Sprintf("INFO <log/%s:%d> helper func\n", filepath.Base(file), line-1), buf.String())
}
func TestTextFatal(t *testing.T) {
var buf bytes.Buffer
logger := New(&buf)
logger.SetReportCaller(true)
if os.Getenv("FATAL") == "1" {
logger.Fatal("i'm dead")
logger.Fatalf("bye %s", "bye")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestTextFatal")
cmd.Env = append(os.Environ(), "FATAL=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}
func TestTextValueStyles(t *testing.T) {
var buf bytes.Buffer
logger := New(&buf)
logger.SetColorProfile(termenv.ANSI256)
lipgloss.SetColorProfile(termenv.ANSI256)
st := DefaultStyles()
st.Value = lipgloss.NewStyle().Bold(true)
st.Values["key3"] = st.Value.Copy().Underline(true)
logger.SetStyles(st)
cases := []struct {
name string
expected string
msg string
kvs []interface{}
f func(msg interface{}, kvs ...interface{})
}{
{
name: "simple message",
expected: fmt.Sprintf("%s info\n", st.Levels[InfoLevel]),
msg: "info",
kvs: nil,
f: logger.Info,
},
{
name: "ignored message",
expected: "",
msg: "this is a debug message",
kvs: nil,
f: logger.Debug,
},
{
name: "message with keyvals",
expected: fmt.Sprintf(
"%s info %s%s%s %s%s%s\n",
st.Levels[InfoLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render("val1"),
st.Key.Render("key2"), st.Separator.Render(separator), st.Value.Render("val2"),
),
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2"},
f: logger.Info,
},
{
name: "error message with multiline",
expected: fmt.Sprintf(
"%s info\n %s%s\n%s%s\n%s%s\n",
st.Levels[ErrorLevel],
st.Key.Render("key1"), st.Separator.Render(separator),
st.Separator.Render(indentSeparator), st.Value.Render("val1"),
st.Separator.Render(indentSeparator), st.Value.Render("val2"),
),
msg: "info",
kvs: []interface{}{"key1", "val1\nval2"},
f: logger.Error,
},
{
name: "error message with keyvals",
expected: fmt.Sprintf(
"%s info %s%s%s %s%s%s\n",
st.Levels[ErrorLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render("val1"),
st.Key.Render("key2"), st.Separator.Render(separator), st.Value.Render("val2"),
),
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2"},
f: logger.Error,
},
{
name: "odd number of keyvals",
expected: fmt.Sprintf(
"%s info %s%s%s %s%s%s %s%s%s\n",
st.Levels[ErrorLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render("val1"),
st.Key.Render("key2"), st.Separator.Render(separator), st.Value.Render("val2"),
st.Key.Render("key3"), st.Separator.Render(separator), st.Values["key3"].Render(`"missing value"`),
),
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2", "key3"},
f: logger.Error,
},
{
name: "error field",
expected: fmt.Sprintf(
"%s info %s%s%s\n",
st.Levels[ErrorLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render(`"error value"`),
),
msg: "info",
kvs: []interface{}{"key1", errors.New("error value")},
f: logger.Error,
},
{
name: "struct field",
expected: fmt.Sprintf(
"%s info %s%s%s\n",
st.Levels[InfoLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render("{foo:bar}"),
),
msg: "info",
kvs: []interface{}{"key1", struct{ foo string }{foo: "bar"}},
f: logger.Info,
},
{
name: "struct field quoted",
expected: fmt.Sprintf(
"%s info %s%s%s\n",
st.Levels[InfoLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render(`"{foo:bar baz}"`),
),
msg: "info",
kvs: []interface{}{"key1", struct{ foo string }{foo: "bar baz"}},
f: logger.Info,
},
{
name: "slice of strings",
expected: fmt.Sprintf(
"%s info %s%s%s\n",
st.Levels[ErrorLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render(`"[foo bar]"`),
),
msg: "info",
kvs: []interface{}{"key1", []string{"foo", "bar"}},
f: logger.Error,
},
{
name: "slice of structs",
expected: fmt.Sprintf(
"%s info %s%s%s\n",
st.Levels[ErrorLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render(`"[{foo:bar} {foo:baz}]"`),
),
msg: "info",
kvs: []interface{}{"key1", []struct{ foo string }{{foo: "bar"}, {foo: "baz"}}},
f: logger.Error,
},
{
name: "slice of errors",
expected: fmt.Sprintf(
"%s info %s%s%s\n",
st.Levels[ErrorLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render(`"[error value1 error value2]"`),
),
msg: "info",
kvs: []interface{}{"key1", []error{errors.New("error value1"), errors.New("error value2")}},
f: logger.Error,
},
{
name: "map of strings",
expected: fmt.Sprintf(
"%s info %s%s%s\n",
st.Levels[ErrorLevel],
st.Key.Render("key1"), st.Separator.Render(separator), st.Value.Render(`"map[baz:qux foo:bar]"`),
),
msg: "info",
kvs: []interface{}{"key1", map[string]string{"foo": "bar", "baz": "qux"}},
f: logger.Error,
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
c.f(c.msg, c.kvs...)
assert.Equal(t, c.expected, buf.String())
})
}
}
func TestColorProfile(t *testing.T) {
cases := []termenv.Profile{
termenv.Ascii,
termenv.ANSI,
termenv.ANSI256,
termenv.TrueColor,
}
l := New(io.Discard)
for _, p := range cases {
l.SetColorProfile(p)
assert.Equal(t, p, l.re.ColorProfile())
}
}
func TestCustomLevelStyle(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
st := DefaultStyles()
lvl := Level(1234)
st.Levels[lvl] = lipgloss.NewStyle().Bold(true).SetString("FUNKY")
l.SetStyles(st)
l.SetLevel(lvl)
l.Log(lvl, "foobar")
assert.Equal(t, "FUNKY foobar\n", buf.String())
}