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 OpenStdio ¶
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 ¶
Open just the stdout pipe. Equivalent to a StdioPipe with no input buffer.
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 ¶
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 ¶
Read the next line from the output buffer. Blocks until a line is available.