Documentation
¶
Overview ¶
Package expect provides an expect-like interface to automate control of applications. It is unlike expect in that it does not spawn or manage process lifecycle. This package only focuses on expecting output and sending input through it's psuedoterminal.
Index ¶
- func EOF(opts *ExpectOpts) error
- func NewTestWriter(t *testing.T) (io.Writer, error)
- func PTSClosed(opts *ExpectOpts) error
- func StdinClosed(opts *ExpectOpts) error
- func StripTrailingEmptyLines(out string) string
- type CallbackMatcher
- type Console
- func (c *Console) Close() error
- func (c *Console) CloseReaders() (err error)
- func (c *Console) Expect(opts ...ExpectOpt) (string, error)
- func (c *Console) ExpectEOF() (string, error)
- func (c *Console) ExpectLongString(s string) (string, error)
- func (c *Console) ExpectString(s string) (string, error)
- func (c *Console) Expectf(format string, args ...interface{}) (string, error)
- func (c *Console) Fd() uintptr
- func (c *Console) Log(v ...interface{})
- func (c *Console) Logf(format string, v ...interface{})
- func (c *Console) Send(s string) (int, error)
- func (c *Console) SendLine(s string) (int, error)
- func (c *Console) SendOSLine(s string) (int, error)
- func (c *Console) Tty() *os.File
- func (c *Console) Write(b []byte) (int, error)
- type ConsoleCallback
- type ConsoleOpt
- func WithCloser(closer ...io.Closer) ConsoleOpt
- func WithDefaultTimeout(timeout time.Duration) ConsoleOpt
- func WithExpectObserver(observers ...ExpectObserver) ConsoleOpt
- func WithLogger(logger *log.Logger) ConsoleOpt
- func WithSendObserver(observers ...SendObserver) ConsoleOpt
- func WithStdin(readers ...io.Reader) ConsoleOpt
- func WithStdout(writers ...io.Writer) ConsoleOpt
- func WithTermCols(cols int) ConsoleOpt
- func WithTermRows(rows int) ConsoleOpt
- type ConsoleOpts
- type ExpectObserver
- type ExpectOpt
- func All(expectOpts ...ExpectOpt) ExpectOpt
- func Any(expectOpts ...ExpectOpt) ExpectOpt
- func Error(errs ...error) ExpectOpt
- func LongString(strs ...string) ExpectOpt
- func Regexp(res ...*regexp.Regexp) ExpectOpt
- func RegexpPattern(ps ...string) ExpectOpt
- func String(strs ...string) ExpectOpt
- func WithTimeout(timeout time.Duration) ExpectOpt
- type ExpectOpts
- type MatchState
- type Matcher
- type SendObserver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EOF ¶
func EOF(opts *ExpectOpts) error
EOF adds an Expect condition to exit if io.EOF is returned from reading Console's tty.
func NewTestWriter ¶
NewTestWriter returns an io.Writer where bytes written to the file are logged by go's testing logger. Bytes are flushed to the logger on line end.
func PTSClosed ¶
func PTSClosed(opts *ExpectOpts) error
PTSClosed adds an Expect condition to exit if we get an "read /dev/ptmx: input/output error" error which can occur on Linux while reading from the ptm after the pts is closed. Further Reading: https://github.com/kr/pty/issues/21#issuecomment-129381749
func StdinClosed ¶
func StdinClosed(opts *ExpectOpts) error
StdinClosed adds an Expect condition to exit if we read from stdin after it has been closed which can occur on Windows while reading from the Pseudo-terminal after it is closed
func StripTrailingEmptyLines ¶
StripTrailingEmptyLines returns a copy of s stripped of trailing lines that consist of only space characters.
Types ¶
type CallbackMatcher ¶
type CallbackMatcher interface {
// Callback executes the matcher's callback with the terminal state at the
// time of match.
Callback(matchState *MatchState) error
}
CallbackMatcher is a matcher that provides a Callback function.
type Console ¶
type Console struct {
Pty *xpty.Xpty
MatchState *MatchState
// contains filtered or unexported fields
}
Console is an interface to automate input and output for interactive applications. Console can block until a specified output is received and send input back on it's tty. Console can also multiplex other sources of input and multiplex its output to other writers.
func NewConsole ¶
func NewConsole(opts ...ConsoleOpt) (*Console, error)
NewConsole returns a new Console with the given options.
func NewTestConsole ¶
func NewTestConsole(t *testing.T, opts ...ConsoleOpt) (*Console, error)
NewTestConsole returns a new Console that multiplexes the application's stdout to go's testing logger. Primarily so that outputs from parallel tests using t.Parallel() is not interleaved.
func (*Console) Close ¶
Close closes both the TTY and afterwards all the readers You may want to split this up to give the readers time to read all the data until they reach the EOF error
func (*Console) CloseReaders ¶
CloseReaders closes everything that is trying to read from the terminal Call this function once you are sure that you have consumed all bytes
func (*Console) Expect ¶
Expect reads from Console's tty until a condition specified from opts is encountered or an error occurs, and returns the buffer read by console. No extra bytes are read once a condition is met, so if a program isn't expecting input yet, it will be blocked. Sends are queued up in tty's internal buffer so that the next Expect will read the remaining bytes (i.e. rest of prompt) as well as its conditions.
func (*Console) ExpectEOF ¶
ExpectEOF reads from Console's tty until EOF or an error occurs, and returns the buffer read by Console. We also treat the PTSClosed error as an EOF.
func (*Console) ExpectLongString ¶ added in v0.5.6
ExpectLongString reads from Console's tty until the provided long string is read or an error occurs, and returns the buffer read by Console. This function ignores mismatches caused by newline and space characters to account for wrappings at the maximum terminal width.
func (*Console) ExpectString ¶
ExpectString reads from Console's tty until the provided string is read or an error occurs, and returns the buffer read by Console.
func (*Console) Expectf ¶
Expectf reads from the Console's tty until the provided formatted string is read or an error occurs, and returns the buffer read by Console.
func (*Console) Log ¶
func (c *Console) Log(v ...interface{})
Log prints to Console's logger. Arguments are handled in the manner of fmt.Print.
func (*Console) Logf ¶
Logf prints to Console's logger. Arguments are handled in the manner of fmt.Printf.
func (*Console) SendOSLine ¶ added in v0.7.0
SendOSLine writes string s to Console's tty with a trailing newline separator native to the base OS.
func (*Console) Tty ¶
Tty returns Console's pts (slave part of a pty). A pseudoterminal, or pty is a pair of pseudo-devices, one of which, the slave, emulates a real text terminal device.