Categorygithub.com/ActiveState/termtest/expect
modulepackage
0.7.0
Repository: https://github.com/activestate/termtest.git
Documentation: pkg.go.dev

# README

go-expect

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 pseudoterminal.

This is a fork of the original repository Netflix/go-expect mostly to add Windows support. This fork has been added to test the ActiveState state tool

Relevant additions:

  • Windows support (Windows 10 and Windows Sever 2019 only)
  • expect.Console is created with xpty allowing testing of applications that want to talk to an xterm-compatible terminal
  • Filter out VT control characters in output. This is important for Windows support, as the windows pseudo-console creates lots of control-characters that can break up words.

See also ActiveState/termtest for a library that uses this package, but adds more life-cycle management.

Usage

os.Exec example

package main

import (
	"log"
	"os"
	"os/exec"
	"time"

	"github.com/ActiveState/termtest/expect"
)

func main() {
	c, err := expect.NewConsole(expect.WithStdout(os.Stdout))
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()

	cmd := exec.Command("vi")
	cmd.Stdin = c.Tty()
	cmd.Stdout = c.Tty()
	cmd.Stderr = c.Tty()

	go func() {
		c.ExpectEOF()
	}()

	err = cmd.Start()
	if err != nil {
		log.Fatal(err)
	}

	time.Sleep(time.Second)
	c.Send("iHello world\x1b")
	time.Sleep(time.Second)
	c.Send("dd")
	time.Sleep(time.Second)
	c.SendLine(":q!")

	err = cmd.Wait()
	if err != nil {
		log.Fatal(err)
	}
}

golang.org/x/crypto/ssh/terminal example

package main

import (
	"fmt"

	"golang.org/x/crypto/ssh/terminal"

	"github.com/ActiveState/termtest/expect"
)

func getPassword(fd int) string {
	bytePassword, _ := terminal.ReadPassword(fd)

	return string(bytePassword)
}

func main() {
	c, _ := expect.NewConsole()

	defer c.Close()

	donec := make(chan struct{})
	go func() {
		defer close(donec)
		c.SendLine("hunter2")
	}()

	echoText := getPassword(int(c.Tty().Fd()))

	<-donec

	fmt.Printf("\nPassword from stdin: %s", echoText)
}

# Functions

All adds an Expect condition to exit if the content read from Console's tty matches all of the provided ExpectOpt, in any order.
Any adds an Expect condition to exit if the content read from Console's tty matches any of the provided ExpectOpt.
EOF adds an Expect condition to exit if io.EOF is returned from reading Console's tty.
Error adds an Expect condition to exit if reading from Console's tty returns one of the provided errors.
LongString adds an Expect condition to exit if the content read from Console's tty contains any of the given long strings ignoring newlines and spaces to account for potential automatic wrappings at the terminal width.
NewConsole returns a new Console with the given options.
NewTestConsole returns a new Console that multiplexes the application's stdout to go's testing logger.
NewTestWriter returns an io.Writer where bytes written to the file are logged by go's testing logger.
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.
Regexp adds an Expect condition to exit if the content read from Console's tty matches the given Regexp.
RegexpPattern adds an Expect condition to exit if the content read from Console's tty matches the given Regexp patterns.
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.
String adds an Expect condition to exit if the content read from Console's tty contains any of the given strings.
StripTrailingEmptyLines returns a copy of s stripped of trailing lines that consist of only space characters.
WithCloser adds closers that are closed in order when Console is closed.
WithDefaultTimeout sets a default read timeout during Expect statements.
WithExpectObserver adds an ExpectObserver to allow monitoring Expect operations.
WithLogger adds a logger for Console to log debugging information to.
WithSendObserver adds a SendObserver to allow monitoring Send operations.
WithStdin adds readers that bytes read are written to Console's tty.
WithStdout adds writers that Console duplicates writes to, similar to the Unix tee(1) command.
WithTermCols sets the number of columns in the terminal (Default: 80).
WithTermRows sets the number of rows in the terminal (Default: 80).
WithTimeout sets a read timeout for an Expect statement.

# Structs

Console is an interface to automate input and output for interactive applications.
ConsoleOpts provides additional options on creating a Console.
ExpectOpts provides additional options on Expect.
MatchState describes the state of the terminal while trying to match it against an expectation.

# Interfaces

CallbackMatcher is a matcher that provides a Callback function.
Matcher provides an interface for finding a match in content read from Console's tty.

# Type aliases

ConsoleCallback is a callback function to execute if a match is found for the chained matcher.
ConsoleOpt allows setting Console options.
ExpectObserver provides an interface for a function callback that will be called after each Expect operation.
ExpectOpt allows settings Expect options.
SendObserver provides an interface for a function callback that will be called after each Send operation.