-
Notifications
You must be signed in to change notification settings - Fork 7
/
testing.go
105 lines (93 loc) · 2.21 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package zlog
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strconv"
"sync"
"testing"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/baggage"
)
const (
testNameKey = "zlog.testname"
)
var (
setup sync.Once
sink logsink
)
// Test configures and wires up the global logger for testing.
//
// Once called, log messages that do not use a Context returned by a call to
// Test will cause a panic.
//
// Passing a nil Context will return a Context derived from context.Background.
func Test(ctx context.Context, t testing.TB) context.Context {
t.Helper()
setup.Do(sink.Setup)
t.Cleanup(func() {
sink.Remove(t)
})
sink.Create(t)
if ctx == nil {
ctx = context.Background()
}
m, err := baggage.NewMember(testNameKey, t.Name())
if err != nil {
t.Fatal(err)
}
b, err := baggage.FromContext(ctx).SetMember(m)
if err != nil {
t.Fatal(err)
}
return baggage.ContextWithBaggage(ctx, b)
}
// Logsink holds the files and does the routing for log messages.
type logsink struct {
mu sync.RWMutex
ts map[string]testing.TB
}
// Setup configures the logsink and configures the logger.
func (s *logsink) Setup() {
s.ts = make(map[string]testing.TB)
// Set up caller information be default, because the testing package's line
// information will be incorrect.
zerolog.CallerMarshalFunc = func(_ uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
l := zerolog.New(s).With().Caller().Logger()
Set(&l)
}
// Create initializes a new log stream.
func (s *logsink) Create(t testing.TB) {
s.mu.Lock()
defer s.mu.Unlock()
s.ts[t.Name()] = t
}
// Remove tears down a log stream.
func (s *logsink) Remove(t testing.TB) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.ts, t.Name())
}
// Write routes writes to the correct stream.
func (s *logsink) Write(b []byte) (int, error) {
var ev ev
if err := json.Unmarshal(b, &ev); err != nil {
return -1, err
}
l := len(b)
s.mu.RLock()
defer s.mu.RUnlock()
t, ok := s.ts[ev.Name]
if !ok {
panic(fmt.Sprintf("log write to unknown test %q:\n%s", ev.Name, string(b)))
}
t.Log(string(b[:l-1]))
return l, nil
}
// Ev is used to pull the test name out of the zerolog Event.
type ev struct {
Name string `json:"zlog.testname"`
}