ptytest

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2026 License: GPL-3.0, LGPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package ptytest provides a PTY-based test harness for interactive terminal testing. It spawns commands in a pseudo-terminal and provides event-driven Send/WaitFor primitives.

Index

Constants

View Source
const (
	KeyEnter     = '\r'
	KeyEscape    = '\x1b'
	KeyBackspace = '\x7f'
	KeyCtrlC     = '\x03'
	KeyCtrlD     = '\x04'
)

Predefined key constants for SendKey.

Variables

This section is empty.

Functions

func LogProcessTree

func LogProcessTree(t *testing.T, label string, rootPID int)

LogProcessTree logs the process tree rooted at rootPID to the test log. label is a descriptive name used to identify the tree in the log. This is a standalone diagnostic helper, not tied to any Console, useful for diagnosing external processes (e.g. sshd) that may be hanging when a test fails.

func ProcessRawOutput

func ProcessRawOutput(raw string) string

ProcessRawOutput processes raw terminal output through a minimal terminal emulator, properly handling cursor movement and screen clearing sequences that bubbletea uses for in-place rendering.

Unlike simple ANSI stripping (which loses cursor movement semantics and produces non-deterministic output depending on render batching), this function resolves cursor-up, clear-to-end, and other positioning sequences to produce the text that would actually be visible on a terminal screen.

The resulting output is deterministic because regardless of how many intermediate renders bubbletea performed, the final screen state is the same.

func SanitizeOutput

func SanitizeOutput(s string) string

SanitizeOutput provides basic output sanitization compatible with the existing golden file format: strips ANSI, removes trailing whitespace from lines, and normalizes line endings.

func StripANSI

func StripANSI(s string) string

StripANSI removes ANSI escape sequences from s. Exported for use in tests.

Types

type Console

type Console struct {
	// contains filtered or unexported fields
}

Console represents a running command in a PTY.

func Start

func Start(t *testing.T, name string, args []string, opts ...Option) *Console

Start spawns the command in a PTY and returns a Console. The command is automatically terminated and the PTY cleaned up when the test ends.

func (*Console) Close

func (c *Console) Close(t *testing.T)

Close terminates the command (if still running) and cleans up the PTY. It is safe to call multiple times. It is also called automatically on test cleanup.

func (*Console) CmdPath

func (c *Console) CmdPath() string

CmdPath returns the executable path of the spawned command.

func (*Console) DiscardLastSnapshot

func (c *Console) DiscardLastSnapshot()

DiscardLastSnapshot removes the most recently captured snapshot. Useful when a WaitFor call captures a timing-sensitive intermediate state that should not appear in the golden file.

func (*Console) Env

func (c *Console) Env(key string) (string, bool)

Env returns the value of an environment variable passed to the spawned command.

func (*Console) LauncherPid

func (c *Console) LauncherPid() int

LauncherPid returns the PID of the launcher command.

func (*Console) Output

func (c *Console) Output() string

Output returns all terminal output captured so far, with ANSI escape sequences stripped.

func (*Console) Pid

func (c *Console) Pid() int

Pid returns the PID of the spawned command.

func (*Console) RawOutput

func (c *Console) RawOutput() string

RawOutput returns all terminal output captured so far, including ANSI escape sequences.

func (*Console) RequireExitCode

func (c *Console) RequireExitCode(t *testing.T, expectedExitCode int)

RequireExitCode waits for command exit and requires the expected non-zero exit code.

func (*Console) RequireSuccessfulExit

func (c *Console) RequireSuccessfulExit(t *testing.T)

RequireSuccessfulExit waits for command exit and requires exit code 0.

func (*Console) ResetSnapshots

func (c *Console) ResetSnapshots()

ResetSnapshots discards all snapshots captured so far. Useful to ignore preliminary interaction steps (e.g. waiting for a prompt before the meaningful flow begins) when WithSnapshots is enabled.

func (*Console) RewriteLastSnapshot

func (c *Console) RewriteLastSnapshot(rewrite func(string) string)

RewriteLastSnapshot rewrites the most recently captured snapshot in place. If no snapshots were captured yet, it does nothing.

func (*Console) Send

func (c *Console) Send(t *testing.T, s string)

Send writes raw text to the PTY (as if typed by the user).

func (*Console) SendKey

func (c *Console) SendKey(t *testing.T, key byte)

SendKey sends a single control byte to the PTY.

func (*Console) SendLine

func (c *Console) SendLine(t *testing.T, s string)

SendLine writes s followed by Enter.

func (*Console) Signal

func (c *Console) Signal(t *testing.T, sig os.Signal)

Signal sends a signal to the spawned command.

func (*Console) Snapshots

func (c *Console) Snapshots() []string

Snapshots returns the terminal screen states captured at each WaitFor match point and after WaitForExit. Consecutive duplicate snapshots are removed. Only populated when WithSnapshots() option was used.

func (*Console) WaitFor

func (c *Console) WaitFor(t *testing.T, pattern string) string

WaitFor blocks until the accumulated terminal output (from the current scan position) matches the given regexp pattern, or the default timeout expires. On timeout, the test is failed with diagnostic output. Returns the matched output.

func (*Console) WaitForExit

func (c *Console) WaitForExit(t *testing.T) error

WaitForExit blocks until the command exits and all PTY output has been drained. Returns the exit error (nil on success).

func (*Console) WaitForTimeout

func (c *Console) WaitForTimeout(t *testing.T, pattern string, timeout time.Duration) string

WaitForTimeout is like WaitFor but with an explicit timeout.

type Option

type Option func(*options)

Option configures a PTY test session.

func WithDir

func WithDir(dir string) Option

WithDir sets the working directory for the spawned command.

func WithEnv

func WithEnv(env []string) Option

WithEnv sets environment variables for the spawned command.

func WithSize

func WithSize(cols, rows uint16) Option

WithSize sets the terminal size (columns, rows).

func WithSnapshots

func WithSnapshots() Option

WithSnapshots enables automatic terminal screen snapshot capture after each successful WaitFor call. Use Snapshots() to retrieve the captured states. This is useful for TUI applications that redraw in-place (e.g. bubbletea), where the final terminal state alone loses intermediate interaction steps.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the default timeout for WaitFor operations.

Directories

Path Synopsis
package main is a helper binary for ptytest tests that launches a child process.
package main is a helper binary for ptytest tests that launches a child process.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL