pipe

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

The pipe package provides the StdioPipe for reading/writing from stdin/stdout in automated testing.

The create a new pipe, use the Open method with the desired buffer sizes.

// open pipe then defer close
io := pipe.OpenStdio(1, 1, true)
defer io.Close()

If only input or output is desired, use the appropriate open variant.

// stdin only
in := pipe.OpenStdin(1)
defer in.Close()

// stdout only
out := pipe.OpenStdout(1)
defer out.Close()

To queue input for stdin, supply the input with the expected prompt using the Queue method.

// queue a [prompt, input] pair
in.Queue("prompt: ", INPUT)

If using both input and output (ie. OpenStdio), then the queue sequence must be marked as complete.

// queue the pair then mark finished
io.Queue("prompt: ", INPUT)
io.EndQueue()

To read output from stdout (newline separated), use the ReadLine or ReadLines variant.

// read single line
line := out.ReadLine()

// read multiple lines (returns slice)
lines := out.ReadLines(2)

To read a line, but ignore it's value, use SkipLines.

// skip the following lines
out.SkipLines(2)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StdioPipe

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

StdioPipe is a testing utility for inputting stdin and reading stdout in automated test cases.

func OpenStdin

func OpenStdin(bufSize int) StdioPipe

Open just the stdin pipe. Equivalent to a StdioPipe with no output buffer.

func OpenStdio

func OpenStdio(inBufSize, outBufSize int, enableEcho bool) StdioPipe

Open the Stdio pipe with the requested buffer sizes. After calling, stdin/stdout will read/write to the pipe until Close is called. Do to the redirecting of stdin and stdout, usage of the StdioPipe is NOT thread safe.

Note the process will stall if either buffer is too small to store the required values, so ensure a large enough buffer size to accommodate all expected data. If either buffer size is zero, then that buffer is effectively disabled (see OpenStdin/OpenStdout).

Echoing involves copying input to the output buffer to mimic terminal echoing. Does nothing if either buffer is disabled.

func OpenStdout

func OpenStdout(bufSize int) StdioPipe

Open just the stdout pipe. Equivalent to a StdioPipe with no input buffer.

func (StdioPipe) Close

func (p StdioPipe) Close()

Close the pipe and restore stdin/stdout.

func (StdioPipe) EndQueue

func (p StdioPipe) EndQueue()

Notify the pipe that no more input is expected. Must be called to continue reading output after the final input.

Should only be called once per pipe. Calling multiple times may cause the program to stall.

Note: if only using StdinPipe, this step is optionally.

func (StdioPipe) Queue

func (p StdioPipe) Queue(prompt string, input any)

Queue the next [prompt, input] pair to the buffer. Stalls if the input buffer is full (ensure a larger enough buffer size).

The pipe will read stdout until the prompt has been read exactly, then the associated input will be written to stdin.

func (StdioPipe) ReadLine

func (p StdioPipe) ReadLine() string

Read the next line from the output buffer. Blocks until a line is available.

func (StdioPipe) ReadLines

func (p StdioPipe) ReadLines(count int) []string

Read multiple lines at once from output buffer (see ReadLine).

func (StdioPipe) SkipLines

func (p StdioPipe) SkipLines(count int)

Read then discard multiple lines from output buffer (see ReadLine).

Jump to

Keyboard shortcuts

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