Watch
1
0
Fork
You've already forked golang-github-pete-woods-go-expect
0
No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-18 12:04:47 +02:00
.claude Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
.github Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
debian Updating golang metapackge in build-depends to golang-go instead of obsolete golang-any. 2026-08-18 12:04:47 +02:00
tools Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
.gitignore Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
.golangci.yml Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
console.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
console_unix.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
console_windows.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
doc.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
expect.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
expect_opt.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
expect_opt_test.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
expect_opt_unix.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
expect_opt_windows.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
expect_subprocess_test.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
expect_test.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
go.mod Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
go.sum Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
helpers_test.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
LICENSE Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
OSSMETADATA Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
passthrough_pipe.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
passthrough_pipe_test.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
reader_lease.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
reader_lease_test.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
README.md Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
Taskfile.yml Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00
test_log.go Adding upstream version 0.1.4. 2026-07-09 12:21:19 +02:00

go-expect

Go GoDoc OSS Lifecycle

Package expect provides an expect-like interface to automate control of interactive applications through a pseudo-terminal.

This project is a fork of the original dead Netflix/go-expect, with the goal of providing cross-platform support (specifcally Windows) via charmbracelet/x/xpty.

Usage

Cross-platform exec.Cmd example

package main

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

	expect "github.com/pete-woods/go-expect"
)

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

	// Start attaches the command to the console's pseudo-terminal on all
	// platforms. (On Unix you can alternatively assign c.Tty() to the
	// command's Stdin/Stdout/Stderr yourself.)
	cmd := exec.Command("vi")
	if err = c.Start(cmd); err != nil {
		log.Fatal(err)
	}

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

	// On Windows cmd.Wait does not work for processes attached to a ConPTY,
	// so use expect.WaitProcess instead.
	if err = expect.WaitProcess(context.Background(), cmd); err != nil {
		log.Fatal(err)
	}

	// The console holds the pseudo-terminal open, so the process exiting
	// does not produce an EOF by itself. Close the console to unblock
	// ExpectEOF once you are done.
	c.Close()
	c.ExpectEOF()
}

Expecting output

c.ExpectString("What is 1+1?")
c.SendLine("2")
c.ExpectEOF()

Windows notes

  • Console.Tty() only exists on Unix: ConPTY has no slave file. Use Console.Start to attach commands.
  • SendLine terminates lines with "\n" on Unix and "\r" on Windows, where the ConPTY treats carriage return as enter.
  • Wait for processes with expect.WaitProcess rather than cmd.Wait.
  • ConPTY output contains VT escape sequences (cursor movement, colors) in addition to the raw program output. String matchers still work because the program's text appears literally within the stream, but exact-match assertions on full output will differ from Unix.