prompt

package
v0.10.0-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package prompt allows testing of "prompting" that simulates asking the user for input using stdin and stdout.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DialogTest

func DialogTest(ctx context.Context, tb testing.TB, steps []DialogStep, cmd cli.Command, runArgs []string) error

DialogTest is a helper for running tests against a CLI command that involve communicating over stdin and stdout. The expected conversation is defined as a sequence of DialogSteps.

If the observed dialog doesn't match the expected dialog, or if the test times out, then tb.Fatalf() will be called. In either of these cases, a goroutine could be leaked, but we consider that OK, because this is just a test.

cmd.Run() will be called with runArgs. If Run() returns an error, that error will be returned from this function. That is the only error that will ever be returned by this function.

If *both* (1) cmd.Run() returns error and (2) the observed dialog doesn't match the expected dialog, then tb.Fatalf() will be called (so no error will be returned. This allows the dialog to be verified even for cases that return error.

func DialogTestFunc

func DialogTestFunc(ctx context.Context, tb testing.TB, steps []DialogStep, f func(input.Prompter))

DialogTestFunc is like DialogTest except it doesn't depend on having a cli.Command. A background goroutine is started that will execute the back-and-forth conversation defined in the given steps, simulating the user. Your provided callback should prompt the user for some input using the provided Prompter.

func ReadWithTimeout

func ReadWithTimeout(tb testing.TB, r io.Reader, wantSubstr string)

ReadWithTimeout does a single read from the given reader. It calls Fatal if that read fails or the returned string doesn't contain wantSubStr. May leak a goroutine on timeout.

func WriteWithTimeout

func WriteWithTimeout(tb testing.TB, w io.Writer, msg string)

WriteWithTimeout does a single write to the given writer. It calls Fatal if that read doesn't contain wantSubStr. May leak a goroutine on timeout.

Types

type DialogStep

type DialogStep struct {
	WaitForPrompt string
	ThenRespond   string // should end with newline
}

DialogStep describes the prompt and respond msg.

type FakePrompter

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

func (*FakePrompter) IsTestFake

func (f *FakePrompter) IsTestFake()

This function doesn't do anything and is never called. It just satisfies an interface so a type assertion can check for a fake prompter.

func (*FakePrompter) Prompt

func (f *FakePrompter) Prompt(ctx context.Context, msg string, args ...any) (string, error)

func (*FakePrompter) Stdin

func (f *FakePrompter) Stdin() io.Reader

Jump to

Keyboard shortcuts

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