/
expect_test.go
144 lines (129 loc) · 3.18 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
package expect_test
import (
"io"
"os/exec"
"testing"
"time"
"reflect"
"github.com/jamesharr/expect"
"github.com/kr/pty"
)
func TestExpect_timeout(t *testing.T) {
// Start basic
t.Log("Starting Command")
cmd := exec.Command("bash", "-c", "sleep 0.1; echo hello")
p, err := pty.Start(cmd)
if err != nil {
t.Error("Start failed:", err)
}
exp := expect.Create(p, func() {})
defer exp.Close()
exp.SetLogger(expect.TestLogger(t))
// This should timeout
t.Log("Expect - should timeout")
exp.SetTimeout(time.Millisecond)
m, err := exp.Expect("[Hh]ello")
t.Logf(" err=%#v", err)
if err != expect.ErrTimeout {
t.Error("Expecting timeout, but got", err)
}
if m.Before != "" {
t.Errorf("m.Before should be empty, but got %q", m.Before)
}
if !reflect.DeepEqual(m.Groups, []string(nil)) {
t.Errorf("Expecting m.Groups to be empty, got %q", m.Groups)
}
// Try to get get the final text
t.Log("Test - should finish immediately")
t.Logf(" buffer[pre]:%#v", exp.Buffer())
exp.SetTimeout(time.Second)
m, err = exp.Expect("e(l+)o")
t.Logf(" m=%#v, err=%#v", m, err)
if err != nil {
t.Error("Expecting error to be nil, but got", err)
}
m_exp := expect.Match{
Before: "h",
Groups: []string{"ello", "ll"},
}
if !reflect.DeepEqual(m, m_exp) {
t.Errorf("Expecting match to be %v, but got %v", m, m_exp)
}
// Test assert
t.Log("Test should return an EOF")
// t.Logf(" Buffer: %#v", exp.Buffer())
err = exp.ExpectEOF()
t.Logf(" err=%#v", err)
if err != io.EOF {
t.Error("Expecting EOF error, got", err)
}
}
func TestExpect_send(t *testing.T) {
// Start cat
exp, err := expect.Spawn("cat")
if err != nil {
t.Error("Unexpected error spawning 'cat'", err)
}
defer exp.Close()
exp.SetLogger(expect.TestLogger(t))
exp.SetTimeout(time.Second)
// Send some data
err = exp.Send("Hello\nWorld\n")
if err != nil {
t.Error("Unexpected error spawning 'cat'", err)
}
// Get first chunk
m, err := exp.Expect("Hello")
if err != nil {
t.Error("Expect() error:", err)
}
m_exp := expect.Match{
Before: "",
Groups: []string{"Hello"},
}
if !reflect.DeepEqual(m, m_exp) {
t.Errorf("expected match to be %v, got %v", m_exp, m)
}
// Check new lines
m, err = exp.Expect("World\n")
m_exp = expect.Match{
Before: "\n",
Groups: []string{"World\n"},
}
if !reflect.DeepEqual(m, m_exp) {
t.Errorf("expected match to be %v, got %v", m_exp, m)
}
}
func TestExpect_largeBuffer(t *testing.T) {
// Start cat
exp, err := expect.Spawn("cat")
if err != nil {
t.Error("Unexpected error spawning 'cat'", err)
}
defer exp.Close()
exp.SetLogger(expect.TestLogger(t))
exp.SetTimeout(time.Second)
// Sending large amounts of text
t.Log("Generating large amounts of text")
text := make([]byte, 128)
for i := range text {
text[i] = '.'
}
text[len(text)-1] = '\n'
t.Log("Writing large amounts of text")
for i := 0; i < 1024; i++ {
// t.Logf(" Writing %d bytes", i*len(text))
err := exp.Send(string(text))
if err != nil {
t.Logf(" Send Error: %#v", err)
}
}
exp.Send("\nDONE\n")
t.Log("Expecting to see finish message")
match, err := exp.Expect("DONE")
t.Logf(" match.Groups=%#v", match.Groups)
t.Logf(" err=%#v", err)
if err != nil {
t.Error("Unexpected err", err)
}
}