-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_test.go
193 lines (152 loc) · 4.3 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
package msg_test
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/FollowTheProcess/msg"
"github.com/FollowTheProcess/msg/internal/colour"
)
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", colour.CodeSuccess, colour.CodeReset)
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", colour.CodeSuccess, colour.CodeReset)
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", colour.CodeError, colour.CodeReset)
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", colour.CodeError, colour.CodeReset)
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", colour.CodeWarn, colour.CodeReset)
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", colour.CodeWarn, colour.CodeReset)
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", colour.CodeInfo, colour.CodeReset)
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", colour.CodeInfo, colour.CodeReset)
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", colour.CodeTitle, colour.CodeReset)
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", colour.CodeTitle, colour.CodeReset)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
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
}