-
Notifications
You must be signed in to change notification settings - Fork 158
/
process_test.go
314 lines (265 loc) · 6.52 KB
/
process_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
306
307
308
309
310
311
312
313
314
package actionlint
import (
"fmt"
"runtime"
"strings"
"sync/atomic" // Note: atomic.Bool was added at Go 1.19
"testing"
"time"
)
func testStartEchoCommand(t *testing.T, proc *concurrentProcess, done *atomic.Bool) {
t.Helper()
done.Store(false)
echo := testSkipIfNoCommand(t, proc, "echo")
echo.run([]string{}, "", func(b []byte, err error) error {
if err != nil {
t.Error(err)
return err
}
done.Store(true)
return nil
})
// This function does not wait the command finishes
}
func testSkipIfNoCommand(t *testing.T, p *concurrentProcess, cmd string) *externalCommand {
t.Helper()
c, err := p.newCommandRunner(cmd, false)
if err != nil {
t.Skipf("%s command is necessary to run this test: %s", cmd, err)
}
return c
}
func TestProcessRunConcurrently(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("this test is flaky on Windows")
}
p := newConcurrentProcess(5)
sleep := testSkipIfNoCommand(t, p, "sleep")
start := time.Now()
for i := 0; i < 5; i++ {
sleep.run([]string{"0.1"}, "", func(b []byte, err error) error {
if err != nil {
t.Error(err)
return err
}
return nil
})
}
if err := sleep.wait(); err != nil {
t.Fatal(err)
}
p.wait()
sec := time.Since(start).Seconds()
if sec >= 0.5 {
t.Fatalf("commands did not run concurrently. running five `sleep 0.1` commands took %v seconds", sec)
}
}
func TestProcessRunMultipleCommandsConcurrently(t *testing.T) {
p := newConcurrentProcess(3)
done := make([]bool, 5)
cmds := make([]*externalCommand, 0, 5)
for i := 0; i < 5; i++ {
idx := i
echo := testSkipIfNoCommand(t, p, "echo")
echo.run([]string{"hello"}, "", func(b []byte, err error) error {
if err != nil {
t.Error(err)
return err
}
done[idx] = true
return nil
})
cmds = append(cmds, echo)
}
for i, c := range cmds {
if err := c.wait(); err != nil {
t.Errorf("cmds[%d] failed: %s", i, err)
}
}
for i, b := range done {
if !b {
t.Errorf("cmds[%d] did not finish", i)
}
}
}
func TestProcessWaitMultipleCommandsFinish(t *testing.T) {
p := newConcurrentProcess(2)
done := make([]bool, 3)
for i := 0; i < 3; i++ {
idx := i
echo := testSkipIfNoCommand(t, p, "echo")
echo.run([]string{"hello"}, "", func(b []byte, err error) error {
if err != nil {
t.Error(err)
return err
}
done[idx] = true
return nil
})
}
p.wait()
for i, b := range done {
if !b {
t.Errorf("cmds[%d] did not finish", i)
}
}
}
func TestProcessInputStdin(t *testing.T) {
p := newConcurrentProcess(1)
cat := testSkipIfNoCommand(t, p, "cat")
out := ""
cat.run([]string{}, "this is test", func(b []byte, err error) error {
if err != nil {
t.Error(err)
return err
}
out = string(b)
return nil
})
if err := cat.wait(); err != nil {
t.Fatal(err)
}
p.wait()
if out != "this is test" {
t.Fatalf("stdin was not input to `cat` command: %q", out)
}
}
func TestProcessErrorCommandNotFound(t *testing.T) {
p := newConcurrentProcess(1)
c := &externalCommand{
proc: p,
exe: "this-command-does-not-exist",
}
c.run([]string{}, "", func(b []byte, err error) error {
if err != nil {
return fmt.Errorf("yay! error found! %w", err)
}
t.Error("command not found error did not occur")
return nil
})
echoDone := &atomic.Bool{}
testStartEchoCommand(t, p, echoDone)
err := c.wait()
if err == nil || !strings.Contains(err.Error(), "yay! error found!") {
t.Fatalf("error was not reported by p.Wait(): %v", err)
}
p.wait()
if !echoDone.Load() {
t.Fatal("a command following the error did not run")
}
}
func TestProcessErrorInCallback(t *testing.T) {
p := newConcurrentProcess(1)
echo := testSkipIfNoCommand(t, p, "echo")
echo.run([]string{}, "", func(b []byte, err error) error {
if err != nil {
t.Error(err)
return err
}
return fmt.Errorf("dummy error")
})
echoDone := &atomic.Bool{}
testStartEchoCommand(t, p, echoDone)
err := echo.wait()
if err == nil || err.Error() != "dummy error" {
t.Fatalf("error was not reported by p.Wait(): %v", err)
}
p.wait()
if !echoDone.Load() {
t.Fatal("a command following the error did not run")
}
}
func TestProcessErrorLinterFailed(t *testing.T) {
p := newConcurrentProcess(1)
ls := testSkipIfNoCommand(t, p, "ls")
// Running ls with directory which does not exist emulates external liter's failure.
// For example shellcheck exits with non-zero status but it outputs nothing to stdout when it
// fails to run.
ls.run([]string{"oops-this-directory-does-not-exist"}, "", func(b []byte, err error) error {
if err != nil {
return err
}
t.Error("error did not occur on running the process")
return nil
})
echoDone := &atomic.Bool{}
testStartEchoCommand(t, p, echoDone)
err := ls.wait()
if err == nil {
t.Fatal("error did not occur")
}
msg := err.Error()
if !strings.Contains(msg, "but stdout was empty") || !strings.Contains(msg, "oops-this-directory-does-not-exist") {
t.Fatalf("Error message was unexpected: %q", msg)
}
p.wait()
if !echoDone.Load() {
t.Fatal("a command following the error did not run")
}
}
func TestProcessRunConcurrentlyAndWait(t *testing.T) {
p := newConcurrentProcess(2)
echo := testSkipIfNoCommand(t, p, "echo")
c := make(chan struct{})
for i := 0; i < 3; i++ {
go func() {
for i := 0; i < 5; i++ {
echo.run(nil, "", func(b []byte, err error) error {
return err
})
}
c <- struct{}{}
}()
}
for i := 0; i < 3; i++ {
<-c
}
p.wait()
}
func TestProcessCombineStdoutAndStderr(t *testing.T) {
p := newConcurrentProcess(1)
bash := testSkipIfNoCommand(t, p, "bash")
bash.combineOutput = true
script := "echo 'hello stdout'; echo 'hello stderr' >&2"
done := make(chan string)
bash.run([]string{"-c", script}, "", func(b []byte, err error) error {
if err != nil {
t.Fatal(err)
return err
}
done <- string(b)
return nil
})
out := <-done
if err := bash.wait(); err != nil {
t.Fatal(err)
}
p.wait()
if !strings.Contains(out, "hello stdout") {
t.Errorf("stdout was not captured: %q", out)
}
if !strings.Contains(out, "hello stderr") {
t.Errorf("stderr was not captured: %q", out)
}
}
func TestProcessCommandExitStatusNonZero(t *testing.T) {
p := newConcurrentProcess(1)
bash := testSkipIfNoCommand(t, p, "false")
done := make(chan error)
bash.run([]string{}, "", func(b []byte, err error) error {
done <- err
return nil
})
err := <-done
if err := bash.wait(); err != nil {
t.Fatal(err)
}
p.wait()
if err == nil {
t.Fatal("Error did not happen")
}
msg := err.Error()
if !strings.Contains(msg, "exited with status 1") {
t.Fatalf("Unexpected error happened: %q", msg)
}
}