forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_test.go
292 lines (245 loc) · 6 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
package actionlint
import (
"fmt"
"runtime"
"strings"
"sync"
"testing"
"time"
)
func testStartEchoCommand(t *testing.T, proc *concurrentProcess, done *bool) {
*done = false
echo := testSkipIfNoCommand(t, proc, "echo")
echo.run([]string{}, "", func(b []byte, err error) error {
if err != nil {
t.Error(err)
return err
}
*done = true
return nil
})
// This function does not wait the command finishes
}
func testSkipIfNoCommand(t *testing.T, p *concurrentProcess, cmd string) *externalCommand {
c, err := p.newCommandRunner(cmd)
if err != nil {
t.Skipf("%s command is necessary to run this test: %s", cmd, err)
}
return c
}
func TestProcessRunProcessSerial(t *testing.T) {
p := newConcurrentProcess(1)
ret := []string{}
mu := sync.Mutex{}
starts := []time.Time{}
ends := []time.Time{}
numProcs := 3
echo := testSkipIfNoCommand(t, p, "echo")
for i := 0; i < numProcs; i++ {
in := fmt.Sprintf("message %d", i)
echo.run([]string{in}, "", func(b []byte, err error) error {
mu.Lock()
defer mu.Unlock()
starts = append(starts, time.Now())
defer func() {
ends = append(ends, time.Now())
}()
if err != nil {
t.Error(err)
return err
}
ret = append(ret, string(b))
return nil
})
}
if err := echo.wait(); err != nil {
t.Fatal(err)
}
p.wait()
if len(ret) != numProcs {
t.Fatalf("wanted %d outputs but got %#v", numProcs, ret)
}
// Check error messages
for i := 0; i < numProcs; i++ {
e := fmt.Sprintf("message %d", i)
if !strings.HasPrefix(ret[i], e) {
t.Fatalf("ret[%d] does not start with %q: %#v", i, e, ret)
}
}
starts = starts[1:]
ends = ends[:len(ends)-1]
for i, s := range starts {
e := ends[i]
if s.Before(e) {
t.Errorf("Command #%d started at %s before previous command #%d ends at %s", i+1, s, i, e)
}
}
}
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
})
var echoDone 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 {
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")
})
var echoDone 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 {
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
})
var echoDone 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 {
t.Fatal("a command following the error did not run")
}
}