-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathlogrus_test.go
111 lines (93 loc) · 2.68 KB
/
logrus_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
package hijack_test
import (
"context"
"testing"
"time"
"github.com/coder/envbuilder/log"
"github.com/coder/envbuilder/log/hijack"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestLogrus_Info(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)
messages := make(chan *logrus.Entry)
logf := func(entry *logrus.Entry) {
t.Logf("got msg level: %s msg: %q", entry.Level, entry.Message)
messages <- entry
}
hijack.Logrus(log.LevelInfo, logf)
done := make(chan struct{})
go func() {
defer close(done)
// The following should be filtered out.
logrus.Trace("Tracing!")
logrus.Debug("Debugging!")
// We should receive the below.
logrus.Info("Testing!")
logrus.Warn("Warning!")
logrus.Error("Error!")
}()
require.Equal(t, "Testing!", rcvCtx(ctx, t, messages).Message)
require.Equal(t, "Warning!", rcvCtx(ctx, t, messages).Message)
require.Equal(t, "Error!", rcvCtx(ctx, t, messages).Message)
<-done
}
func TestLogrus_Debug(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)
messages := make(chan *logrus.Entry)
logf := func(entry *logrus.Entry) {
t.Logf("got msg level: %s msg: %q", entry.Level, entry.Message)
messages <- entry
}
hijack.Logrus(log.LevelDebug, logf)
done := make(chan struct{})
go func() {
defer close(done)
// The following should be filtered out.
logrus.Trace("Tracing!")
// We should receive the below.
logrus.Debug("Debugging!")
logrus.Info("Testing!")
logrus.Warn("Warning!")
logrus.Error("Error!")
}()
require.Equal(t, "Debugging!", rcvCtx(ctx, t, messages).Message)
require.Equal(t, "Testing!", rcvCtx(ctx, t, messages).Message)
require.Equal(t, "Warning!", rcvCtx(ctx, t, messages).Message)
require.Equal(t, "Error!", rcvCtx(ctx, t, messages).Message)
<-done
}
func TestLogrus_Error(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)
messages := make(chan *logrus.Entry)
logf := func(entry *logrus.Entry) {
t.Logf("got msg level: %s msg: %q", entry.Level, entry.Message)
messages <- entry
}
hijack.Logrus(log.LevelError, logf)
done := make(chan struct{})
go func() {
defer close(done)
// The following should be filtered out.
logrus.Trace("Tracing!")
logrus.Debug("Debugging!")
logrus.Info("Testing!")
logrus.Warn("Warning!")
// We should receive the below.
logrus.Error("Error!")
}()
require.Equal(t, "Error!", rcvCtx(ctx, t, messages).Message)
<-done
}
func rcvCtx[T any](ctx context.Context, t *testing.T, ch <-chan T) (v T) {
t.Helper()
select {
case <-ctx.Done():
t.Fatal("timeout")
case v = <-ch:
}
return v
}