-
Notifications
You must be signed in to change notification settings - Fork 784
/
console_wrapper.go
76 lines (65 loc) · 2.23 KB
/
console_wrapper.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
package tests
import (
"bytes"
"strings"
"testing"
"github.com/Netflix/go-expect"
"github.com/acarl005/stripansi"
"github.com/hinshun/vt10x"
"github.com/stretchr/testify/assert"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// ConsoleWrapper is a wrapper around the go-expect Console that takes a test object and will report failures
// This prevents users having to manually detect and report errors during the tests
type ConsoleWrapper struct {
tester *testing.T
console *expect.Console
state *vt10x.State
terminal.Stdio
}
// Analogous to https://github.com/Netflix/go-expect/blob/9d1f4485533ba0665fa4ca2ef6f1210198f7eaae/expect_opt.go#L158-L177 but ignoring ANSI escapes.
type strippedStringMatcher struct {
str string
}
func (sm *strippedStringMatcher) Match(v interface{}) bool {
buf, ok := v.(*bytes.Buffer)
if !ok {
return false
}
if strings.Contains(stripansi.Strip(buf.String()), sm.str) {
return true
}
return false
}
func (sm *strippedStringMatcher) Criteria() interface{} {
return sm.str
}
// ExpectString expects a string to be present on the console and fails the test if it is not
func (c *ConsoleWrapper) ExpectString(s string) {
out, err := c.console.Expect(func(opts *expect.ExpectOpts) error {
// analogous to https://github.com/Netflix/go-expect/blob/9d1f4485533ba0665fa4ca2ef6f1210198f7eaae/expect_opt.go#L245-L251
opts.Matchers = append(opts.Matchers, &strippedStringMatcher{
str: s,
})
return nil
})
assert.NoError(c.tester, err, "Expected string: %q\nActual string: %q", s, stripansi.Strip(out))
}
// SendLine sends a string to the console and fails the test if something goes wrong
func (c *ConsoleWrapper) SendLine(s string) {
_, err := c.console.SendLine(s)
assert.NoError(c.tester, err, "Error sending line %s", s)
}
// ExpectEOF expects an EOF to be present on the console and reports an error if it is not
func (c *ConsoleWrapper) ExpectEOF() {
out, err := c.console.ExpectEOF()
assert.NoError(c.tester, err, "Expected EOF. Got %q", stripansi.Strip(out))
}
// Close closes the console
func (c *ConsoleWrapper) Close() error {
return c.console.Tty().Close()
}
// CurrentState gets the last line of text currently on the console
func (c *ConsoleWrapper) CurrentState() string {
return c.state.String()
}