-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture_test.go
More file actions
288 lines (264 loc) · 6.73 KB
/
capture_test.go
File metadata and controls
288 lines (264 loc) · 6.73 KB
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
package testutils
import (
"fmt"
"os"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
func TestCaptureStdout(t *testing.T) {
tests := []struct {
name string
want string
f func()
}{
{
name: "simple output",
want: "hello world\n",
f: func() {
fmt.Fprintf(os.Stdout, "hello world\n")
},
},
{
name: "multiple lines",
want: "line1\nline2\n",
f: func() {
fmt.Fprintln(os.Stdout, "line1")
fmt.Fprintln(os.Stdout, "line2")
},
},
{
name: "empty output",
want: "",
f: func() {},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := CaptureStdout(t, tt.f)
require.Equal(t, tt.want, got, "CaptureStdout() returned unexpected output")
})
}
}
func TestCaptureStderr(t *testing.T) {
tests := []struct {
name string
want string
f func()
}{
{
name: "simple output",
want: "hello world\n",
f: func() {
fmt.Fprintf(os.Stderr, "hello world\n")
},
},
{
name: "multiple lines",
want: "line1\nline2\n",
f: func() {
fmt.Fprintln(os.Stderr, "line1")
fmt.Fprintln(os.Stderr, "line2")
},
},
{
name: "empty output",
want: "",
f: func() {},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := CaptureStderr(t, tt.f)
require.Equal(t, tt.want, got, "CaptureStderr() returned unexpected output")
})
}
}
func TestCaptureStdoutAndStderr(t *testing.T) {
tests := []struct {
name string
wantOut string
wantErr string
f func()
}{
{
name: "both outputs",
wantOut: "stdout\n",
wantErr: "stderr\n",
f: func() {
fmt.Fprintln(os.Stdout, "stdout")
fmt.Fprintln(os.Stderr, "stderr")
},
},
{
name: "only stdout",
wantOut: "stdout\n",
wantErr: "",
f: func() {
fmt.Fprintln(os.Stdout, "stdout")
},
},
{
name: "only stderr",
wantOut: "",
wantErr: "stderr\n",
f: func() {
fmt.Fprintln(os.Stderr, "stderr")
},
},
{
name: "empty function",
wantOut: "",
wantErr: "",
f: func() {},
},
{
name: "large output",
wantOut: strings.Repeat("a", 100000) + "\n",
wantErr: strings.Repeat("b", 100000) + "\n",
f: func() {
fmt.Fprintln(os.Stdout, strings.Repeat("a", 100000))
fmt.Fprintln(os.Stderr, strings.Repeat("b", 100000))
},
},
{
name: "binary data",
wantOut: string([]byte{0, 1, 2, 3}),
wantErr: string([]byte{4, 5, 6, 7}),
f: func() {
_, err := os.Stdout.Write([]byte{0, 1, 2, 3})
if err != nil {
t.Logf("failed to write to stdout: %v", err)
}
_, err = os.Stderr.Write([]byte{4, 5, 6, 7})
if err != nil {
t.Logf("failed to write to stderr: %v", err)
}
},
},
{
name: "concurrent writes",
wantOut: "out1\nout2\nout3\n",
wantErr: "err1\nerr2\nerr3\n",
f: func() {
var wg sync.WaitGroup
wg.Add(6)
go func() { defer wg.Done(); fmt.Fprintln(os.Stdout, "out1") }()
go func() { defer wg.Done(); fmt.Fprintln(os.Stderr, "err1") }()
go func() { defer wg.Done(); fmt.Fprintln(os.Stdout, "out2") }()
go func() { defer wg.Done(); fmt.Fprintln(os.Stderr, "err2") }()
go func() { defer wg.Done(); fmt.Fprintln(os.Stdout, "out3") }()
go func() { defer wg.Done(); fmt.Fprintln(os.Stderr, "err3") }()
wg.Wait()
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOut, gotErr := CaptureStdoutAndStderr(t, tt.f)
// for concurrent writes, we can't guarantee order
if tt.name == "concurrent writes" {
// check that all expected lines are present, ignoring order
for _, line := range []string{"out1", "out2", "out3"} {
require.Contains(t, gotOut, line)
}
for _, line := range []string{"err1", "err2", "err3"} {
require.Contains(t, gotErr, line)
}
} else {
require.Equal(t, tt.wantOut, gotOut, "CaptureStdoutAndStderr() stdout returned unexpected output")
require.Equal(t, tt.wantErr, gotErr, "CaptureStdoutAndStderr() stderr returned unexpected output")
}
})
}
}
// TestCaptureFunctionErrors tests edge cases for the capture functions
func TestCaptureFunctionErrors(t *testing.T) {
t.Run("panic in function", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic")
}
}()
CaptureStdout(t, func() {
panic("test panic")
})
})
t.Run("stderr with panic", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic")
}
}()
CaptureStderr(t, func() {
panic("test panic")
})
})
t.Run("stdout and stderr with panic", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic")
}
}()
CaptureStdoutAndStderr(t, func() {
panic("test panic")
})
})
}
// TestCaptureWithOutput tests capturing output when the function has early returns
func TestCaptureWithOutput(t *testing.T) {
t.Run("stdout with basic output", func(t *testing.T) {
output := CaptureStdout(t, func() {
fmt.Fprintln(os.Stdout, "before early return")
})
require.Equal(t, "before early return\n", output)
require.NotContains(t, output, "after early return")
})
t.Run("stderr with basic output", func(t *testing.T) {
output := CaptureStderr(t, func() {
fmt.Fprintln(os.Stderr, "before early return")
})
require.Equal(t, "before early return\n", output)
require.NotContains(t, output, "after early return")
})
t.Run("stdout and stderr with basic output", func(t *testing.T) {
stdout, stderr := CaptureStdoutAndStderr(t, func() {
fmt.Fprintln(os.Stdout, "stdout before early return")
fmt.Fprintln(os.Stderr, "stderr before early return")
})
require.Equal(t, "stdout before early return\n", stdout)
require.Equal(t, "stderr before early return\n", stderr)
require.NotContains(t, stdout, "after early return")
require.NotContains(t, stderr, "after early return")
})
}
// TestCaptureWithLargeOutput tests capturing large amounts of output
func TestCaptureWithLargeOutput(t *testing.T) {
const largeSize = 1000
largeData := strings.Repeat("a", largeSize)
t.Run("large stdout", func(t *testing.T) {
output := CaptureStdout(t, func() {
fmt.Fprint(os.Stdout, largeData)
})
require.Len(t, output, largeSize)
require.Equal(t, largeData, output)
})
t.Run("large stderr", func(t *testing.T) {
output := CaptureStderr(t, func() {
fmt.Fprint(os.Stderr, largeData)
})
require.Len(t, output, largeSize)
require.Equal(t, largeData, output)
})
t.Run("large stdout and stderr", func(t *testing.T) {
stdout, stderr := CaptureStdoutAndStderr(t, func() {
fmt.Fprint(os.Stdout, largeData)
fmt.Fprint(os.Stderr, largeData)
})
require.Len(t, stdout, largeSize)
require.Len(t, stderr, largeSize)
require.Equal(t, largeData, stdout)
require.Equal(t, largeData, stderr)
})
}