-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample_test.go
42 lines (33 loc) · 1.05 KB
/
example_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
package termtest_test
import (
"os/exec"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ActiveState/termtest"
)
func Test_Basic(t *testing.T) {
tt, err := termtest.New(exec.Command("bash"), termtest.OptTestErrorHandler(t))
require.NoError(t, err)
tt.SendLine("echo ABC")
tt.Expect("ABC")
tt.SendLine("echo DEF")
tt.Expect("DEF")
tt.SendLine("exit")
tt.ExpectExitCode(0)
}
func Test_DontMatchInput(t *testing.T) {
tt, err := termtest.New(exec.Command("bash"))
require.NoError(t, err)
tt.Expect("$") // Wait for prompt
tt.SendLine("FOO=bar")
tt.Expect("FOO=bar") // This matches the input, not the output
expectError := tt.Expect("FOO=bar",
// options:
termtest.OptExpectTimeout(100*time.Millisecond),
termtest.OptExpectErrorHandler(termtest.SilenceErrorHandler()), // Prevent errors from bubbling up as panics
)
require.ErrorIs(t, expectError, termtest.TimeoutError, "Should have thrown an expect timeout error because FOO=bar was only sent via STDIN, output: %s", tt.Output())
tt.SendLine("exit")
tt.ExpectExitCode(0)
}