-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect_test.go
140 lines (107 loc) · 2.67 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
package expect
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestUnknownCommand(t *testing.T) {
ep := NewExpect("/nopenopenope")
err := ep.Start()
require.EqualError(t, err, "fork/exec /nopenopenope: no such file or directory")
}
func TestFailingCommand(t *testing.T) {
ep := NewExpect("false")
err := ep.Start()
require.NoError(t, err)
_, err = ep.Expect("")
require.EqualError(t, err, "process stopped: exit status 1")
}
func TestExpectFunc(t *testing.T) {
ep := NewExpect("/bin/echo", "hello world")
err := ep.Start()
require.NoError(t, err)
wstr := "hello world\n"
l, eerr := ep.ExpectFunc(func(a string) bool { return len(a) > 10 })
require.NoError(t, eerr)
require.Equal(t, wstr, l)
cerr := ep.Close()
require.NoError(t, cerr)
}
func TestEcho(t *testing.T) {
ep := NewExpect("/bin/echo", "hello world")
err := ep.Start()
require.NoError(t, err)
l, eerr := ep.Expect("world")
require.NoError(t, eerr)
wstr := "hello world"
require.Equal(t, wstr, l[:len(wstr)])
cerr := ep.Close()
require.NoError(t, cerr)
}
func TestExited(t *testing.T) {
ep := NewExpect("/bin/echo", "")
err := ep.Start()
require.NoError(t, err)
l, err := ep.Expect("foobar")
require.EqualError(t, err, "process stopped")
require.Equal(t, "", l)
cerr := ep.Close()
require.NoError(t, cerr)
}
func TestClose(t *testing.T) {
ep := NewExpect("/bin/echo", "")
err := ep.Start()
require.NoError(t, err)
err = ep.Close()
require.NoError(t, err)
err = ep.Close()
require.EqualError(t, err, "already closed")
}
func TestStop(t *testing.T) {
ep := NewExpect("/bin/sleep", "100")
err := ep.Start()
require.NoError(t, err)
err = ep.Stop()
require.NoError(t, err)
err = ep.Stop()
require.EqualError(t, err, "already closed")
}
func TestExpectOnClosed(t *testing.T) {
ep := NewExpect("/bin/echo", "hello world")
err := ep.Start()
require.NoError(t, err)
err = ep.Close()
require.NoError(t, err)
_, err = ep.Expect("...")
require.EqualError(t, err, "already closed")
}
func TestSend(t *testing.T) {
ep := NewExpect("/usr/bin/tr", "a", "b")
err := ep.Start()
require.NoError(t, err)
err = ep.Send("a\n")
require.NoError(t, err)
_, err = ep.Expect("b")
require.NoError(t, err)
err = ep.Stop()
require.NoError(t, err)
}
func TestSignal(t *testing.T) {
ep := NewExpect("/bin/sleep", "100")
err := ep.Start()
require.NoError(t, err)
err = ep.Signal(os.Interrupt)
require.NoError(t, err)
done := make(chan struct{})
go func() {
defer close(done)
cerr := ep.Close()
require.EqualError(t, cerr, "process stopped: signal: interrupt")
}()
select {
case <-time.After(5 * time.Second):
t.Fatalf("signal test timed out")
case <-done:
}
}