-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexpect_test.go
242 lines (220 loc) · 6.16 KB
/
expect_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
package termtest
import (
"errors"
"fmt"
"os/exec"
"regexp"
"runtime"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_Expect(t *testing.T) {
tt := newTermTest(t, exec.Command("bash", "-c", "echo HELLO"), true)
tt.Expect("HELLO")
tt.ExpectExitCode(0, OptExpectTimeout(time.Hour))
}
func Test_Expect_Cmd(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping test on non-windows platform")
}
tt := newTermTest(t, exec.Command("cmd", "/c", "echo HELLO"), true)
tt.Expect("HELLO")
tt.ExpectExitCode(0)
}
func Test_ExpectRe(t *testing.T) {
tt := newTermTest(t, exec.Command("bash", "-c", "echo HELLO"), true)
tt.ExpectRe(regexp.MustCompile(`HEL(LO)`))
tt.ExpectExitCode(0)
}
func Test_ExpectRe_Cmd(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping test on non-windows platform")
}
tt := newTermTest(t, exec.Command("cmd", "/c", "echo HELLO"), true)
tt.ExpectRe(regexp.MustCompile(`HEL(LO)`))
tt.ExpectExitCode(0)
}
func Test_ExpectCustom(t *testing.T) {
customErr := fmt.Errorf("custom error")
type args struct {
consumer consumer
opts []SetExpectOpt
}
tests := []struct {
name string
tt func(t *testing.T) *TermTest
args args
wantErrMsg string
wantErr error
}{
{
"Simple Hello World Match",
func(t *testing.T) *TermTest {
return newTermTest(t, exec.Command("bash", "-c", "echo Hello World"), true)
},
args{
func(buffer string) (endPos int, err error) {
return indexEndPos(buffer, "Hello World"), nil
},
[]SetExpectOpt{},
},
"",
nil,
},
{
"No match by process end",
func(t *testing.T) *TermTest {
return newTermTest(t, exec.Command("bash", "-c", "echo Hello World"), true)
},
args{
func(buffer string) (endPos int, err error) {
return 0, nil
},
[]SetExpectOpt{OptExpectTimeout(time.Second)},
},
"",
PtyEOF,
},
{
"Custom error",
func(t *testing.T) *TermTest {
return newTermTest(t, exec.Command("bash", "-c", "echo Custom Error"), true)
},
args{
func(buffer string) (endPos int, err error) {
return 0, customErr
},
[]SetExpectOpt{OptExpectTimeout(time.Second)},
},
"",
customErr,
},
{
"error with messaging",
func(t *testing.T) *TermTest {
return newTermTest(t, exec.Command("bash", "-c", "echo Custom Error"), true)
},
args{
func(buffer string) (endPos int, err error) {
return 0, customErr
},
[]SetExpectOpt{OptExpectTimeout(time.Second), OptExpectErrorMessage("Error Message")},
},
fmt.Errorf("Error Message: consumer threw error: %w", customErr).Error(),
customErr,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tt := tc.tt(t)
err := tt.ExpectCustom(tc.args.consumer, append(tc.args.opts, OptExpectSilenceErrorHandler())...)
require.ErrorIs(t, err, tc.wantErr)
require.NotErrorIs(t, tt.Wait(5*time.Second), TimeoutError)
})
}
}
func Test_ExpectCustom_Cmd(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping test on non-windows platform")
}
customErr := fmt.Errorf("custom error")
type args struct {
consumer consumer
opts []SetExpectOpt
}
tests := []struct {
name string
tt func(t *testing.T) *TermTest
args args
wantErr error
}{
{
"Simple Hello World Match",
func(t *testing.T) *TermTest {
return newTermTest(t, exec.Command("cmd", "/c", "echo Hello World"), true)
},
args{
func(buffer string) (endPos int, err error) {
return indexEndPos(buffer, "Hello World"), nil
},
[]SetExpectOpt{},
},
nil,
},
{
"No match by process end",
func(t *testing.T) *TermTest {
return newTermTest(t, exec.Command("cmd", "/c", "echo Hello World"), true)
},
args{
func(buffer string) (endPos int, err error) {
return 0, nil
},
[]SetExpectOpt{OptExpectTimeout(time.Second)},
},
PtyEOF,
},
{
"Custom error",
func(t *testing.T) *TermTest {
return newTermTest(t, exec.Command("cmd", "/c", "echo Custom Error"), true)
},
args{
func(buffer string) (endPos int, err error) {
return 0, customErr
},
[]SetExpectOpt{OptExpectTimeout(time.Second)},
},
customErr,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tt := tc.tt(t)
err := tt.ExpectCustom(tc.args.consumer, append(tc.args.opts, OptExpectSilenceErrorHandler())...)
require.ErrorIs(t, err, tc.wantErr)
require.NotErrorIs(t, tt.Wait(5*time.Second), TimeoutError)
})
}
}
func Test_Expect_Timeout(t *testing.T) {
tt := newTermTest(t, exec.Command("bash", "-c", "echo HELLO && sleep 1"), false)
durations := []time.Duration{
100 * time.Millisecond,
200 * time.Millisecond,
500 * time.Millisecond,
}
for _, duration := range durations {
ti := time.Now()
err := tt.Expect("No match", OptExpectTimeout(duration), OptExpectSilenceErrorHandler())
if strings.Index(err.Error(), "Expected:") == -1 {
t.Errorf("Should include error message asserting what it timed out waiting for, got: %s", err)
}
if !errors.Is(err, TimeoutError) {
t.Errorf("Expected timeout, but got %s", err)
}
if time.Since(ti) > duration+(50*time.Millisecond) { // 50ms of margin
t.Errorf("Expected timeout under %s, but got %s", duration, time.Since(ti))
}
}
tt.ExpectExitCode(0, OptExpectTimeout(time.Hour))
}
func Test_ExpectMatchTwiceSameBuffer(t *testing.T) {
tt := newTermTest(t, exec.Command("bash"), false)
tt.Expect("$") // Wait for bash to be ready, so we don't timeout on bash startup
tt.SendLine("echo ONE TWO THREE")
tt.Expect("echo ONE TWO THREE", OptExpectTimeout(time.Second)) // Match stdin first
tt.Expect("ONE", OptExpectTimeout(time.Second))
err := tt.Expect("ONE", OptExpectTimeout(time.Second), OptExpectSilenceErrorHandler())
require.ErrorIs(t, err, TimeoutError)
tt.Expect("TWO", OptExpectTimeout(time.Second))
err = tt.Expect("TWO", OptExpectTimeout(time.Second), OptExpectSilenceErrorHandler())
require.ErrorIs(t, err, TimeoutError)
tt.Expect("THREE", OptExpectTimeout(time.Second))
err = tt.Expect("THREE", OptExpectTimeout(time.Second), OptExpectSilenceErrorHandler())
require.ErrorIs(t, err, TimeoutError)
tt.SendLine("exit")
tt.ExpectExitCode(0)
}