@@ -18,16 +18,11 @@ import (
18
18
"bufio"
19
19
"errors"
20
20
"fmt"
21
- "io"
22
- // "io/ioutil"
23
- //"log"
24
- // "os"
25
- // "os/exec"
21
+ "io"
26
22
"runtime/debug"
27
23
"strings"
28
24
"sync"
29
25
"testing"
30
- "time"
31
26
)
32
27
33
28
var (
@@ -70,7 +65,6 @@ func newTestConsole(t *testing.T, opts ...ConsoleOpt) (*Console, error) {
70
65
opts = append ([]ConsoleOpt {
71
66
expectNoError (t ),
72
67
sendNoError (t ),
73
- WithDefaultTimeout (time .Second ),
74
68
}, opts ... )
75
69
return NewTestConsole (t , opts ... )
76
70
}
@@ -131,14 +125,12 @@ func TestExpectf(t *testing.T) {
131
125
c .SendLine ("2" )
132
126
c .Expectf ("What is %s backwards?" , "Netflix" )
133
127
c .SendLine ("xilfteN" )
134
- c .ExpectEOF ()
135
128
}()
136
129
137
130
err = Prompt (c .InTty (), c .OutTty ())
138
131
if err != nil {
139
132
t .Errorf ("Expected no error but got '%s'" , err )
140
133
}
141
- testCloser (t , c )
142
134
wg .Wait ()
143
135
}
144
136
@@ -159,15 +151,12 @@ func TestExpect(t *testing.T) {
159
151
c .SendLine ("2" )
160
152
c .ExpectString ("What is Netflix backwards?" )
161
153
c .SendLine ("xilfteN" )
162
- //c.ExpectEOF()
163
154
}()
164
155
165
156
err = Prompt (c .InTty (), c .OutTty ())
166
157
if err != nil {
167
158
t .Errorf ("Expected no error but got '%s'" , err )
168
159
}
169
- // close the pts so we can expect EOF
170
- testCloser (t , c )
171
160
wg .Wait ()
172
161
}
173
162
@@ -187,174 +176,11 @@ func TestExpectOutput(t *testing.T) {
187
176
defer wg .Done ()
188
177
c .ExpectString ("What is 1+1?" )
189
178
c .SendLine ("3" )
190
- //c.ExpectEOF()
191
179
}()
192
180
193
181
err = Prompt (c .InTty (), c .OutTty ())
194
182
if err == nil || err != ErrWrongAnswer {
195
183
t .Errorf ("Expected error '%s' but got '%s' instead" , ErrWrongAnswer , err )
196
184
}
197
- testCloser (t , c )
198
185
wg .Wait ()
199
186
}
200
-
201
- // TODO: Needs to be updated to work on Windows
202
- // func TestExpectDefaultTimeout(t *testing.T) {
203
- // t.Parallel()
204
-
205
- // c, err := NewTestConsole(t, WithDefaultTimeout(0))
206
- // if err != nil {
207
- // t.Errorf("Expected no error but got'%s'", err)
208
- // }
209
- // defer testCloser(t, c)
210
-
211
- // var wg sync.WaitGroup
212
- // wg.Add(1)
213
- // go func() {
214
- // defer wg.Done()
215
- // Prompt(c.InTty(), c.OutTty())
216
- // }()
217
-
218
- // _, err = c.ExpectString("What is 1+2?")
219
- // if err == nil || !strings.Contains(err.Error(), "i/o timeout") {
220
- // t.Errorf("Expected error to contain 'i/o timeout' but got '%s' instead", err)
221
- // }
222
-
223
- // //Close to unblock Prompt and wait for the goroutine to exit.
224
- // c.Close()
225
- // wg.Wait()
226
- // }
227
-
228
- // func TestExpectTimeout(t *testing.T) {
229
- // t.Parallel()
230
-
231
- // c, err := NewTestConsole(t)
232
- // if err != nil {
233
- // t.Errorf("Expected no error but got'%s'", err)
234
- // }
235
- // defer testCloser(t, c)
236
-
237
- // var wg sync.WaitGroup
238
- // wg.Add(1)
239
- // go func() {
240
- // defer wg.Done()
241
- // Prompt(c.InTty(), c.OutTty())
242
- // }()
243
-
244
- // _, err = c.Expect(String("What is 1+2?"), WithTimeout(0))
245
- // if err == nil || !strings.Contains(err.Error(), "i/o timeout") {
246
- // t.Errorf("Expected error to contain 'i/o timeout' but got '%s' instead", err)
247
- // }
248
-
249
- // //Close to unblock Prompt and wait for the goroutine to exit.
250
- // c.Close()
251
- // wg.Wait()
252
- // }
253
-
254
- // func TestExpectDefaultTimeoutOverride(t *testing.T) {
255
- // t.Parallel()
256
-
257
- // c, err := newTestConsole(t, WithDefaultTimeout(100*time.Millisecond))
258
- // if err != nil {
259
- // t.Errorf("Expected no error but got'%s'", err)
260
- // }
261
- // defer testCloser(t, c)
262
-
263
- // var wg sync.WaitGroup
264
- // wg.Add(1)
265
- // go func() {
266
- // defer wg.Done()
267
- // err = Prompt(c.InTty(), c.OutTty())
268
- // if err != nil {
269
- // t.Errorf("Expected no error but got '%s'", err)
270
- // }
271
- // time.Sleep(200 * time.Millisecond)
272
- // c.Close()
273
- // }()
274
-
275
- // c.ExpectString("What is 1+1?")
276
- // c.SendLine("2")
277
- // c.ExpectString("What is Netflix backwards?")
278
- // c.SendLine("xilfteN")
279
- // c.Expect(EOF, PTSClosed, WithTimeout(time.Second))
280
-
281
- // wg.Wait()
282
- // }
283
-
284
- // func TestEditor(t *testing.T) {
285
- // if _, err := exec.LookPath("vi"); err != nil {
286
- // t.Skip("vi not found in PATH")
287
- // }
288
- // t.Parallel()
289
-
290
- // c, err := NewConsole(expectNoError(t), sendNoError(t))
291
- // if err != nil {
292
- // t.Errorf("Expected no error but got '%s'", err)
293
- // }
294
- // defer testCloser(t, c)
295
-
296
- // file, err := ioutil.TempFile("", "")
297
- // if err != nil {
298
- // t.Errorf("Expected no error but got '%s'", err)
299
- // }
300
-
301
- // cmd := exec.Command("vi", file.Name())
302
- // cmd.Stdin = c.InTty()
303
- // cmd.Stdout = c.OutTty()
304
- // cmd.Stderr = c.OutTty()
305
-
306
- // var wg sync.WaitGroup
307
- // wg.Add(1)
308
- // go func() {
309
- // defer wg.Done()
310
- // c.Send("iHello world\x1b")
311
- // c.SendLine(":wq!")
312
- // c.ExpectEOF()
313
- // }()
314
-
315
- // err = cmd.Run()
316
- // if err != nil {
317
- // t.Errorf("Expected no error but got '%s'", err)
318
- // }
319
-
320
- // testCloser(t, c)
321
- // wg.Wait()
322
-
323
- // data, err := ioutil.ReadFile(file.Name())
324
- // if err != nil {
325
- // t.Errorf("Expected no error but got '%s'", err)
326
- // }
327
- // if string(data) != "Hello world\n" {
328
- // t.Errorf("Expected '%s' to equal '%s'", string(data), "Hello world\n")
329
- // }
330
- // }
331
-
332
- // func ExampleConsole_echo() {
333
- // c, err := NewConsole(WithStdout(os.Stdout))
334
- // if err != nil {
335
- // log.Fatal(err)
336
- // }
337
- // defer c.Close()
338
-
339
- // cmd := exec.Command("echo")
340
- // cmd.Stdin = c.InTty()
341
- // cmd.Stdout = c.OutTty()
342
- // cmd.Stderr = c.OutTty()
343
-
344
- // err = cmd.Start()
345
- // if err != nil {
346
- // log.Fatal(err)
347
- // }
348
-
349
- // c.Send("Hello world")
350
- // c.ExpectString("Hello world")
351
- // c.Close()
352
- // c.ExpectEOF()
353
-
354
- // err = cmd.Wait()
355
- // if err != nil {
356
- // log.Fatal(err)
357
- // }
358
-
359
- // Output: Hello world
360
- // }
0 commit comments