ptytest

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ptytest opens a pseudo-terminal for tests, with the standard library alone: /dev/ptmx and the platform's grant and unlock requests, no module (spc-2610030911534855, B6). It is a test helper: no production package imports it. The answer loop's restore tests run a child on the terminal end and read its attributes before and after; guided connect's hidden key read (spc-2610031241482088, TestHiddenKeyRestoresTerminalOnInterrupt) reuses it.

Both ends are opened blocking and outside the runtime's poller, so a child handed the terminal end inherits an ordinary blocking descriptor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attrs

func Attrs(f *os.File) (syscall.Termios, error)

Attrs reads f's terminal attributes, as tcgetattr(3) does.

func Same

func Same(a, b syscall.Termios) bool

Same reports whether two sets of terminal attributes are the same, the kernel's PENDIN bit aside: a BSD kernel sets it whenever canonical mode is entered again, to mark input for retyping, so a terminal restored exactly can read back with it set.

func SetAttrs

func SetAttrs(f *os.File, tio syscall.Termios) error

SetAttrs writes f's terminal attributes, as tcsetattr(3) with TCSANOW does.

func SetSize

func SetSize(f *os.File, cols, rows int) error

SetSize sets the window size of the terminal f is an end of.

Types

type Pty

type Pty struct {
	Master   *os.File
	Terminal *os.File
	// contains filtered or unexported fields
}

Pty is one pseudo-terminal: Master is the controlling end a test types into and reads the child's output from, Terminal the end a child takes as its stdin, stdout and stderr.

func Open

func Open(t testing.TB, cols, rows int) *Pty

Open opens a pseudo-terminal, sizes it cols by rows, and starts draining the master end into a buffer Output and WaitFor read, so a child never blocks on a full terminal. Both ends are closed when the test ends.

func (*Pty) Close

func (p *Pty) Close()

Close closes the terminal end, then the master end, which ends the drain.

func (*Pty) Output

func (p *Pty) Output() string

Output is everything the child has written so far.

func (*Pty) Settled

func (p *Pty) Settled(t testing.TB, quiet time.Duration) string

Settled is the output once it has settled: the kernel holds no byte for the master end the drain has not read (unread), and neither that nor the output has changed for quiet, watched from the call on. A check that something did NOT reach the terminal reads this, never Output, since a read taken before the drain has read the child's last bytes passes falsely. The quiet window also covers a chunk the drain has read and not yet appended, and bytes the kernel has not yet queued for the master end. It fails the test when the output has not settled within settleDeadline. A check that something DID reach the terminal waits for it with WaitFor instead.

func (*Pty) Type

func (p *Pty) Type(t testing.TB, s string)

Type writes s to the master end, as keys typed at the terminal.

func (*Pty) WaitFor

func (p *Pty) WaitFor(t testing.TB, offset int, sub string, timeout time.Duration) int

WaitFor waits until the output after offset bytes holds sub, and returns the offset just past it. It fails the test after timeout.

Jump to

Keyboard shortcuts

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