-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtesting.go
87 lines (69 loc) · 1.31 KB
/
testing.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
package zapwriter
import (
"bytes"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var _testBuffer testBuffer
type testBuffer struct {
bytes.Buffer
mu sync.Mutex
}
func (b *testBuffer) Write(p []byte) (n int, err error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.Buffer.Write(p)
}
func (b *testBuffer) Sync() error {
return nil
}
func (b *testBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.Buffer.String()
}
func (b *testBuffer) Reset() {
b.mu.Lock()
defer b.mu.Unlock()
b.Buffer.Reset()
}
// Capture = String + Reset
func (b *testBuffer) Capture() string {
b.mu.Lock()
defer b.mu.Unlock()
out := b.Buffer.String()
b.Buffer.Reset()
return out
}
func Test() func() {
cfg := NewConfig()
return testWithConfig(cfg)
}
func testWithConfig(cfg Config) func() {
encoder, _, _ := cfg.encoder()
logger := zap.New(
zapcore.NewCore(
encoder,
&_testBuffer,
zapcore.DebugLevel,
),
)
m := &manager{
writers: make(map[string]WriteSyncer),
cores: make(map[string][]zapcore.Core),
loggers: make(map[string]*zap.Logger),
}
m.loggers[""] = logger
_testBuffer.Reset()
prev := replaceGlobalManager(m)
return func() {
replaceGlobalManager(prev)
}
}
func TestCapture() string {
return _testBuffer.Capture()
}
func TestString() string {
return _testBuffer.String()
}