forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
survey_posix_test.go
46 lines (37 loc) · 1.01 KB
/
survey_posix_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
//go:build !windows
// +build !windows
package survey
import (
"testing"
"github.com/AlecAivazis/survey/v2/terminal"
expect "github.com/Netflix/go-expect"
pseudotty "github.com/creack/pty"
"github.com/hinshun/vt10x"
)
func RunTest(t *testing.T, procedure func(expectConsole), test func(terminal.Stdio) error) {
t.Helper()
t.Parallel()
pty, tty, err := pseudotty.Open()
if err != nil {
t.Fatalf("failed to open pseudotty: %v", err)
}
term := vt10x.New(vt10x.WithWriter(tty))
c, err := expect.NewConsole(expect.WithStdin(pty), expect.WithStdout(term), expect.WithCloser(pty, tty))
if err != nil {
t.Fatalf("failed to create console: %v", err)
}
defer c.Close()
donec := make(chan struct{})
go func() {
defer close(donec)
procedure(&consoleWithErrorHandling{console: c, t: t})
}()
stdio := terminal.Stdio{In: c.Tty(), Out: c.Tty(), Err: c.Tty()}
if err := test(stdio); err != nil {
t.Error(err)
}
if err := c.Tty().Close(); err != nil {
t.Errorf("error closing Tty: %v", err)
}
<-donec
}