-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger_interfaces_test.go
65 lines (60 loc) · 1.45 KB
/
logger_interfaces_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
package hedwig_test
import (
"context"
"errors"
"io"
"log"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/cloudchacho/hedwig-go"
)
func Test_StdLogger(t *testing.T) {
tmpDir := t.TempDir()
tests := []struct {
name string
fcall func(logger hedwig.StdLogger)
want string
}{
{
name: "Debug",
fcall: func(logger hedwig.StdLogger) {
var ctx context.Context
logger.Debug(ctx, "Test message", "key", "value", "key2", "value2")
},
want: "[DEBUG] Test message map[key:value key2:value2]",
},
{
name: "Error",
fcall: func(logger hedwig.StdLogger) {
var ctx context.Context
logger.Error(ctx, errors.New("oops"), "Test message", "key", "value", "key2", "value2")
},
want: "[ERROR] Test message: oops map[key:value key2:value2]",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tmpFile, err := os.CreateTemp(tmpDir, "stderr")
require.NoError(t, err)
log.Default().SetOutput(tmpFile)
t.Cleanup(func() {
log.Default().SetOutput(os.Stderr)
})
logger := hedwig.StdLogger{}
test.fcall(logger)
err = tmpFile.Close()
require.NoError(t, err)
tmpFile, err = os.Open(tmpFile.Name())
require.NoError(t, err)
t.Cleanup(func() {
err = tmpFile.Close()
require.NoError(t, err)
})
contents, err := io.ReadAll(tmpFile)
require.NoError(t, err)
assert.Contains(t, string(contents), test.want)
})
}
}