-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmdlog_test.go
191 lines (161 loc) · 4.32 KB
/
cmdlog_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
package cmdlog_test
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"time"
"github.com/creack/pty"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/cmdlog"
"oss.terrastruct.com/util-go/xos"
)
func TestLogger(t *testing.T) {
t.Parallel()
var tca = []struct {
name string
run func(t *testing.T, ctx context.Context, env *xos.Env)
}{
{
name: "COLOR=1",
run: func(t *testing.T, ctx context.Context, env *xos.Env) {
b := &bytes.Buffer{}
env.Setenv("COLOR", "1")
l := cmdlog.New(env, b)
testLogger(l)
t.Log(b.String())
assert.TestdataJSON(t, b.String())
},
},
{
name: "COLOR=",
run: func(t *testing.T, ctx context.Context, env *xos.Env) {
b := &bytes.Buffer{}
l := cmdlog.New(env, b)
testLogger(l)
t.Log(b.String())
assert.TestdataJSON(t, b.String())
},
},
{
name: "tty",
run: func(t *testing.T, ctx context.Context, env *xos.Env) {
ptmx, tty, err := pty.Open()
if err != nil {
t.Fatalf("failed to open pty: %v", err)
}
defer assert.Close(t, ptmx)
defer assert.Close(t, tty)
l := cmdlog.New(env, tty)
testLogger(l)
timer := time.AfterFunc(time.Second*5, func() {
// For some reason ptmx.SetDeadline() does not work.
// tty has to be closed for a read on ptmx to unblock.
tty.Close()
t.Error("read took too long, update expLen")
})
defer timer.Stop()
// If the expected output changes, increase this to 9999, rerun and then update
// for new length.
const expLen = 415
out, err := io.ReadAll(io.LimitReader(ptmx, expLen))
if err != nil {
t.Fatalf("failed to read log output: %v", err)
}
t.Log(len(out), string(out))
assert.TestdataJSON(t, string(out))
},
},
{
name: "testing.TB",
run: func(t *testing.T, ctx context.Context, env *xos.Env) {
ft := &fakeTB{
TB: t,
logf: func(f string, v ...interface{}) {
t.Helper()
assert.String(t, "info: what's up\n", fmt.Sprintf(f, v...))
},
}
env.Setenv("COLOR", "0")
l := cmdlog.NewTB(env, ft)
l.Info.Printf("what's up")
},
},
{
name: "WithPrefix",
run: func(t *testing.T, ctx context.Context, env *xos.Env) {
b := &bytes.Buffer{}
env.Setenv("COLOR", "1")
l := cmdlog.New(env, b)
l2 := l.WithCCPrefix("lochness")
if l2 == l {
t.Fatalf("expected l and l2 to be different loggers")
}
l2 = l2.WithCCPrefix("imgbundler")
l2 = l2.WithCCPrefix("cache")
testLogger(l)
testLogger(l2)
t.Log(b.String())
assert.TestdataJSON(t, b.String())
},
},
{
name: "multiline",
run: func(t *testing.T, ctx context.Context, env *xos.Env) {
b := &bytes.Buffer{}
env.Setenv("COLOR", "1")
l := cmdlog.New(env, b)
l.NoLevel.Print("")
l.SetTS(true)
l.NoLevel.Print("")
l.SetTS(false)
l2 := l.WithCCPrefix("lochness")
l2 = l2.WithCCPrefix("imgbundler")
l2 = l2.WithCCPrefix("cache")
l2.Warn.Print(``)
l2.Warn.Print("\n\n\n")
l2.SetTS(true)
l2.Warn.Printf(`yes %d
yes %d`, 3, 4)
t.Log(b.String())
assert.TestdataJSON(t, b.String())
},
},
}
ctx := context.Background()
for _, tc := range tca {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
env := xos.NewEnv(nil)
tc.run(t, ctx, env)
})
}
}
func testLogger(l *cmdlog.Logger) {
l.NoLevel.Println("Somehow, the world always affects you more than you affect it.")
l.SetDebug(true)
l.Debug.Println("Man is a rational animal who always loses his temper when he is called upon.")
l.SetDebug(false)
l.Debug.Println("You can never trust a woman; she may be true to you.")
l.SetTS(true)
l.Success.Println("An alcoholic is someone you don't like who drinks as much as you do.")
l.Info.Println("There once was this swami who lived above a delicatessan.")
l.SetTSFormat(time.UnixDate)
l.Warn.Println("Telephone books are like dictionaries -- if you know the answer before.")
l.SetTS(false)
l.Error.Println("Nothing can be done in one trip.")
l.Error.Println(`Good day to let down old friends who need help.
I believe in getting into hot water; it keeps you clean.`)
}
type fakeTB struct {
testing.TB
logf func(string, ...interface{})
}
func (ftb *fakeTB) Logf(f string, v ...interface{}) {
ftb.TB.Helper()
ftb.logf(f, v...)
}