-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_test.go
305 lines (248 loc) Β· 7.05 KB
/
msg_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package msg_test
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"testing"
"github.com/FollowTheProcess/hue"
"github.com/FollowTheProcess/msg"
)
const (
successCode = "\x1b[1;32m"
errorCode = "\x1b[1;31m"
causeCode = "\x1b[1m"
warnCode = "\x1b[1;33m"
infoCode = "\x1b[1;36m"
titleCode = "\x1b[96m"
resetCode = "\x1b[0m"
)
func TestMain(m *testing.M) {
// Disable colour auto-detection so CI passes
hue.Enabled(true)
code := m.Run()
os.Exit(code)
}
func TestSuccess(t *testing.T) {
buf := new(bytes.Buffer)
msg.Fsuccess(buf, "Something went well: %v", 42)
want := fmt.Sprintf("%sSuccess%s: Something went well: 42\n", successCode, resetCode)
if got := buf.String(); got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestSuccessCaptured(t *testing.T) {
successFunc := func() {
msg.Success("Worked")
}
got := captureStdout(t, successFunc)
want := fmt.Sprintf("%sSuccess%s: Worked\n", successCode, resetCode)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestError(t *testing.T) {
buf := new(bytes.Buffer)
msg.Ferror(buf, "Something broke: %v", true)
want := fmt.Sprintf("%sError%s: Something broke: true\n", errorCode, resetCode)
if got := buf.String(); got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestErrorCaptured(t *testing.T) {
errorFunc := func() {
msg.Error("Bad number (%v)", 42)
}
got := captureStderr(t, errorFunc)
want := fmt.Sprintf("%sError%s: Bad number (42)\n", errorCode, resetCode)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestErr(t *testing.T) {
t.Run("nil", func(t *testing.T) {
buf := new(bytes.Buffer)
var err error = nil
msg.Ferr(buf, err)
if buf.Len() != 0 {
t.Fatalf("nil err wrote output: %s", buf.String())
}
})
t.Run("plain", func(t *testing.T) {
buf := new(bytes.Buffer)
err := errors.New("Something broke")
msg.Ferr(buf, err)
want := fmt.Sprintf("%sError%s: Something broke\n", errorCode, resetCode)
if got := buf.String(); got != want {
t.Errorf("got %q, wanted %q", got, want)
}
})
t.Run("wrapped", func(t *testing.T) {
buf := new(bytes.Buffer)
root := errors.New("something broke")
one := fmt.Errorf("could not frobnicate the baz: %w", root)
two := fmt.Errorf("failed to process file: %w", one)
three := fmt.Errorf("could not deposit money: %w", two)
msg.Ferr(buf, three)
wantTemplate := `%[1]sError%[2]s: could not deposit money
β°β %[3]scause%[2]s: failed to process file
β°β %[3]scause%[2]s: could not frobnicate the baz
β°β %[3]scause%[2]s: something broke
`
want := fmt.Sprintf(wantTemplate, errorCode, resetCode, causeCode)
if got := buf.String(); got != want {
t.Errorf("got %q, wanted %q", got, want)
}
})
}
func TestErrCaptured(t *testing.T) {
t.Run("plain", func(t *testing.T) {
errorFunc := func() {
err := errors.New("Bang!")
msg.Err(err)
}
got := captureStderr(t, errorFunc)
want := fmt.Sprintf("%sError%s: Bang!\n", errorCode, resetCode)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
})
t.Run("wrapped", func(t *testing.T) {
errorFunc := func() {
root := errors.New("bang!")
one := fmt.Errorf("dingle cannot dangle on version <2: %w", root)
two := fmt.Errorf("could not read file: %w", one)
three := fmt.Errorf("you have no money: %w", two)
msg.Err(three)
}
wantTemplate := `%[1]sError%[2]s: you have no money
β°β %[3]scause%[2]s: could not read file
β°β %[3]scause%[2]s: dingle cannot dangle on version <2
β°β %[3]scause%[2]s: bang!
`
got := captureStderr(t, errorFunc)
want := fmt.Sprintf(wantTemplate, errorCode, resetCode, causeCode)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
})
}
func TestWarn(t *testing.T) {
buf := new(bytes.Buffer)
msg.Fwarn(buf, "skipping directory %s", "./tmp")
want := fmt.Sprintf("%sWarning%s: skipping directory ./tmp\n", warnCode, resetCode)
if got := buf.String(); got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestWarnCaptured(t *testing.T) {
warnFunc := func() {
msg.Warn("Skipping something (%d)", 42)
}
got := captureStdout(t, warnFunc)
want := fmt.Sprintf("%sWarning%s: Skipping something (42)\n", warnCode, resetCode)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestInfo(t *testing.T) {
buf := new(bytes.Buffer)
msg.Finfo(buf, "You have %d projects on GitHub", 27)
want := fmt.Sprintf("%sInfo%s: You have 27 projects on GitHub\n", infoCode, resetCode)
if got := buf.String(); got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestInfoCaptured(t *testing.T) {
infoFunc := func() {
msg.Info("You are %d years old", 29)
}
got := captureStdout(t, infoFunc)
want := fmt.Sprintf("%sInfo%s: You are 29 years old\n", infoCode, resetCode)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestTitle(t *testing.T) {
buf := new(bytes.Buffer)
msg.Ftitle(buf, "Section Header")
want := fmt.Sprintf("\n%sSection Header%s\n\n", titleCode, resetCode)
if got := buf.String(); got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestTitleCaptured(t *testing.T) {
titleFunc := func() {
msg.Title("Section Header")
}
got := captureStdout(t, titleFunc)
want := fmt.Sprintf("\n%sSection Header%s\n\n", titleCode, resetCode)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
func TestVisual(t *testing.T) {
msg.Title("Your CLI output:")
msg.Success("compiled 42 packages")
msg.Warn("directory is empty, skipping")
msg.Info("using value from config file")
fmt.Println()
err := errors.New("bad file permissions")
one := fmt.Errorf("could not read DB config: %w", err)
two := fmt.Errorf("failed to insert new record: %w", one)
three := fmt.Errorf("could not complete transaction: %w", two)
msg.Err(three)
}
func captureStdout(t *testing.T, printer func()) string {
t.Helper()
old := os.Stdout // Backup of the real one
defer func() {
os.Stdout = old // Set it back even if we error later
}()
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("os.Pipe() returned an error: %v", err)
}
// Set stdout to our new pipe
os.Stdout = w
capture := make(chan string)
// Copy in a goroutine so printing can't block forever
go func() {
buf := new(bytes.Buffer)
io.Copy(buf, r) //nolint: errcheck
capture <- buf.String()
}()
// Call our test function that prints to stdout
printer()
// Close the writer
w.Close()
captured := <-capture
return captured
}
func captureStderr(t *testing.T, printer func()) string {
t.Helper()
old := os.Stderr // Backup of the real one
defer func() {
os.Stderr = old // Set it back even if we error later
}()
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("os.Pipe() returned an error: %v", err)
}
// Set stderr to our new pipe
os.Stderr = w
capture := make(chan string)
// Copy in a goroutine so printing can't block forever
go func() {
buf := new(bytes.Buffer)
io.Copy(buf, r) //nolint: errcheck
capture <- buf.String()
}()
// Call our test function that prints to stderr
printer()
// Close the writer
w.Close()
captured := <-capture
return captured
}