-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmder_test.go
274 lines (256 loc) · 7.5 KB
/
cmder_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
package cmder
import (
"context"
"errors"
"fmt"
"runtime"
"strings"
"testing"
"time"
)
type Check[T any] interface {
Name() string
Apply(t T) error
}
type check[T any] struct {
name string
apply func(t T) error
}
func (c check[T]) Name() string {
return c.name
}
func (c check[T]) Apply(t T) error {
return c.apply(t)
}
func NewCheck[T any](name string, apply func(t T) error) Check[T] {
return check[T]{
name: name,
apply: apply,
}
}
var errorIsNilCheck = NewCheck[Result]("error is nil", func(result Result) error {
if result.Err != nil {
return fmt.Errorf("expected no error, got %v", result.Err)
}
return nil
})
var stdOutNonEmptyCheck = NewCheck[Result]("StdOut not empty", func(result Result) error {
if result.StdOut == "" {
return errors.New("empty StdOut")
}
return nil
})
var stdOutEmptyCheck = NewCheck[Result]("StdOut not empty", func(result Result) error {
if result.StdOut != "" {
return errors.New("non-nempty StdOut")
}
return nil
})
var zeroExitCode = NewCheck[Result]("exit code is zero", func(result Result) error {
if result.ExitCode != 0 {
return fmt.Errorf("expected exit code 0, got %d", result.ExitCode)
}
return nil
})
var ls = func() string {
if isWindows() {
return "dir"
}
return "ls"
}()
func TestCommand_Run(t *testing.T) {
tests := []struct {
name string
cmd Spec
checks []Check[Result]
unixOnly bool
}{
{
name: "simple ls command",
cmd: New(ls).WithAttemptTimeout(5 * time.Second),
checks: []Check[Result]{errorIsNilCheck, stdOutNonEmptyCheck, zeroExitCode},
},
{
name: "command with different working directory",
cmd: New("pwd").WithWorkingDirectory("/tmp").WithAttemptTimeout(5 * time.Second),
checks: []Check[Result]{
errorIsNilCheck,
NewCheck[Result]("StdOut contains /tmp", func(result Result) error {
if !strings.Contains(result.StdOut, "/tmp") {
return fmt.Errorf("expected StdOut to contain /tmp, got %v", result.StdOut)
}
return nil
}),
zeroExitCode,
},
unixOnly: true,
},
{
name: "command with standard input",
cmd: New("cat").WithStdIn(strings.NewReader("hello world")).WithAttemptTimeout(5 * time.Second),
checks: []Check[Result]{
errorIsNilCheck,
NewCheck[Result]("StdOut contains hello world", func(result Result) error {
if !strings.Contains(result.StdOut, "hello world") {
return fmt.Errorf("expected StdOut to contain 'hello world', got %v", result.StdOut)
}
return nil
}),
zeroExitCode,
},
unixOnly: true,
},
{
name: "command with custom retry filter",
cmd: New("false").WithRetries(3).WithRetryFilter(func(err error, isAttemptTimeout bool) bool {
return true // Always retry
}).WithAttemptTimeout(1 * time.Second),
checks: []Check[Result]{
NewCheck[Result]("retries 3 times", func(result Result) error {
if result.Attempts != 4 { // 1 initial + 3 retries
return fmt.Errorf("expected 4 attempts, got %d", result.Attempts)
}
return nil
}),
},
},
{
name: "command with verbose logging",
cmd: New("echo", "verbose test").WithVerbose(true).WithAttemptTimeout(5 * time.Second),
checks: []Check[Result]{
errorIsNilCheck,
stdOutNonEmptyCheck,
zeroExitCode,
},
},
{
name: "command with CollectAllOutput disabled",
cmd: New("echo", "test").WithCollectAllOutput(false).WithAttemptTimeout(5 * time.Second),
checks: []Check[Result]{
errorIsNilCheck,
zeroExitCode,
stdOutEmptyCheck,
},
},
{
name: "command with StdOut and StdErr forwarded",
cmd: New("echo", "forward test").WithStdOutErrForwarded().WithAttemptTimeout(5 * time.Second),
checks: []Check[Result]{
errorIsNilCheck,
zeroExitCode,
},
},
{
name: "command with different retry counts",
cmd: New("false").WithRetries(2).WithAttemptTimeout(1 * time.Second).WithRetryFilter(func(err error, isAttemptTimeout bool) bool {
return true // Always retry
}),
checks: []Check[Result]{
NewCheck[Result]("retries 2 times", func(result Result) error {
if result.Attempts != 3 { // 1 initial + 2 retries
return fmt.Errorf("expected 3 attempts, got %d", result.Attempts)
}
return nil
}),
},
},
{
name: "command with different timeout configurations",
cmd: New("sleep", "2").WithAttemptTimeout(1 * time.Second).WithTotalTimeout(3 * time.Second),
checks: []Check[Result]{
NewCheck[Result]("error is context.DeadlineExceeded", func(result Result) error {
if result.Err == nil {
return errors.New("expected error")
}
if !errors.Is(result.Err, context.DeadlineExceeded) {
return fmt.Errorf("expected error to be context.DeadlineExceeded, got %v", result.Err)
}
if result.ExitCode == 0 {
return errors.New("expected non-zero exit code")
}
return nil
}),
},
},
{
name: "timing out command",
cmd: New("sleep", "10").WithAttemptTimeout(1 * time.Second).WithRetries(4).WithVerbose(true),
checks: []Check[Result]{
NewCheck[Result]("error is context.DeadlineExceeded", func(result Result) error {
if result.Err == nil {
return errors.New("expected error")
}
if !errors.Is(result.Err, context.DeadlineExceeded) {
return fmt.Errorf("expected error to be context.DeadlineExceeded, got %v", result.Err)
}
if result.ExitCode == 0 {
return errors.New("expected non-zero exit code")
}
return nil
}),
},
},
{
name: "timing out command with total timeout",
cmd: New("sleep", "10").WithTotalTimeout(4 * time.Second),
checks: []Check[Result]{
NewCheck[Result]("error is context.DeadlineExceeded", func(result Result) error {
if result.Err == nil {
return errors.New("expected error")
}
if !errors.Is(result.Err, context.DeadlineExceeded) {
return fmt.Errorf("expected error to be context.DeadlineExceeded, got %v", result.Err)
}
if result.ExitCode == 0 {
return errors.New("expected non-zero exit code")
}
return nil
}),
},
},
{
name: "Failing command fails immedaitely",
cmd: New("abc123").WithTotalTimeout(10 * time.Second).WithRetries(5).WithAttemptTimeout(1 * time.Second),
checks: []Check[Result]{
NewCheck[Result]("error without retries", func(result Result) error {
if result.Err == nil {
return errors.New("expected error")
}
if errors.Is(result.Err, context.DeadlineExceeded) {
return errors.New("expected error to be different from context.DeadlineExceeded")
}
if result.Attempts != 1 {
return fmt.Errorf("expected 1 attempt, got %d", result.Attempts)
}
if result.ExitCode == 0 {
return errors.New("expected non-zero exit code")
}
return nil
}),
},
},
{
name: "command writing output every second for 4 seconds does not time out",
cmd: New("bash", "-c", "for i in {1..4}; do echo $i; sleep 1; done").WithAttemptTimeout(2 * time.Second).WithResetAttemptTimeoutOnOutput(true),
checks: []Check[Result]{errorIsNilCheck, stdOutNonEmptyCheck, zeroExitCode},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.unixOnly && isWindows() {
t.Skip("Test is Unix only")
}
cmdResult := tt.cmd.Run(context.Background())
for _, check := range tt.checks {
fmt.Printf(" - Running check '%v'...", check.Name())
if err := check.Apply(cmdResult); err != nil {
t.Errorf("Spec.Run() check '%v' failed: %v", check.Name(), err)
}
fmt.Printf("OK \n")
}
})
}
}
func isWindows() bool {
return runtime.GOOS == "windows"
}