Documentation
¶
Index ¶
- func Args(args ...string) []string
- func ArgsS(args string) []string
- func TempConfigFileWithBytes(bytes []byte) *os.File
- func TempConfigFileWithObj(obj interface{}) *os.File
- func TempConfigFileWithString(str string) *os.File
- type CommandExecutor
- type InteractiveCommandExecutor
- func (ice *InteractiveCommandExecutor) Command() *cobra.Command
- func (ice *InteractiveCommandExecutor) Execute(args []string, interactionFunc InteractiveUserFunc) (*InteractiveExecutionContext, error)
- func (ice *InteractiveCommandExecutor) ExecuteS(argsStr string, interactionFunc InteractiveUserFunc) (*InteractiveExecutionContext, error)
- func (ice *InteractiveCommandExecutor) SetTimeout(timeout time.Duration)
- func (ice *InteractiveCommandExecutor) Timeout() time.Duration
- type InteractiveCommandTester
- type InteractiveExecutionContext
- func (ice *InteractiveExecutionContext) Console() *expect.Console
- func (ice *InteractiveExecutionContext) OutputBuffer() *bytes.Buffer
- func (ice *InteractiveExecutionContext) PassthroughTty() *PassthroughPipeFile
- func (ice *InteractiveExecutionContext) ReadTimeout() time.Duration
- func (ice *InteractiveExecutionContext) Stderr() io.Writer
- func (ice *InteractiveExecutionContext) Stdin() io.Reader
- func (ice *InteractiveExecutionContext) Stdout() io.Writer
- func (ice *InteractiveExecutionContext) TerminalState() *vt10x.State
- func (ice *InteractiveExecutionContext) Tty() *os.File
- type InteractiveProcessFunc
- type InteractiveTestContext
- func (ict *InteractiveTestContext) Assert() *assert.Assertions
- func (ict *InteractiveTestContext) Console() *expect.Console
- func (ict *InteractiveTestContext) ExpectEOF() (string, error)
- func (ict *InteractiveTestContext) ExpectMatch(opts ...expect.ExpectOpt) (string, error)
- func (ict *InteractiveTestContext) ExpectMatches(opts ...expect.ExpectOpt) (string, error)
- func (ict *InteractiveTestContext) ExpectString(s string) (string, error)
- func (ict *InteractiveTestContext) ExpectStringf(format string, args ...interface{}) (string, error)
- func (ict *InteractiveTestContext) Require() *require.Assertions
- func (ict *InteractiveTestContext) RequireEOF() (string, error)
- func (ict *InteractiveTestContext) RequireMatch(opts ...expect.ExpectOpt) (string, error)
- func (ict *InteractiveTestContext) RequireMatches(opts ...expect.ExpectOpt) (string, error)
- func (ict *InteractiveTestContext) RequireString(s string) (string, error)
- func (ict *InteractiveTestContext) RequireStringf(format string, args ...interface{}) (string, error)
- func (ict *InteractiveTestContext) SendLine(s string) (int, error)
- func (ict *InteractiveTestContext) T() *testing.T
- type InteractiveUserFunc
- type PassthroughPipeFile
- type Suite
- func (h *Suite) AddDefaultArg(arg string)
- func (h *Suite) AddDefaultArgs(args ...string)
- func (h *Suite) Command() *cobra.Command
- func (h Suite) DefaultArgs() []string
- func (h *Suite) Execute(args ...string) (string, error)
- func (h *Suite) ExecuteArgs(args []string) (string, error)
- func (h *Suite) ExecuteC(args ...string) (*cobra.Command, string, error)
- func (h *Suite) ExecuteInteractively(args []string, interactionFunc InteractiveUserFunc) (*InteractiveExecutionContext, error)
- func (h *Suite) ExecuteString(argsStr string) (string, error)
- func (h *Suite) ExecuteStringInteractively(argsStr string, interactionFunc InteractiveUserFunc) (*InteractiveExecutionContext, error)
- func (h *Suite) ExecuteTestInteractively(args []string, testFunc func(*InteractiveTestContext) error) (*InteractiveExecutionContext, error)
- func (h *Suite) ExecuteTestStringInteractively(argsStr string, testFunc func(*InteractiveTestContext) error) (*InteractiveExecutionContext, error)
- func (h *Suite) InteractiveCommandExecutor() *InteractiveCommandExecutor
- func (h *Suite) InteractiveCommandTester() *InteractiveCommandTester
- func (h Suite) InteractiveExecutionTimeout() time.Duration
- func (h *Suite) SetCobraCommand(cmd *cobra.Command)
- func (h *Suite) SetCommand(cmd *command.BaseCommand)
- func (h *Suite) SetDefaultArgs(args []string)
- func (h *Suite) SetInteractiveExecutionTimeout(timeout time.Duration)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArgsS ¶ added in v0.2.0
ArgsS is a convenience function that converts a space delimited string into an array of args
func TempConfigFileWithBytes ¶
TempConfigFileWithBytes returns a temporary YAML config file with the given byte array content
func TempConfigFileWithObj ¶
TempConfigFileWithObj returns a temporary YAML config file with the given object serialized to YAML
func TempConfigFileWithString ¶
TempConfigFileWithString returns a temporary YAML config file with the given string content
Types ¶
type CommandExecutor ¶
type CommandExecutor struct {
// contains filtered or unexported fields
}
CommandExecutor provides an interface for executing Cobra commands in tests
func NewCommandExecutor ¶
func NewCommandExecutor(rootCmd *cobra.Command) *CommandExecutor
NewCommandExecutor returns an executor for testing Cobra commands
type InteractiveCommandExecutor ¶
type InteractiveCommandExecutor struct { PreExecutionFunc InteractiveProcessFunc PostExecutionFunc InteractiveProcessFunc // contains filtered or unexported fields }
InteractiveCommandExecutor executes a Cobra command interactively within a virtual terminal The command executor orchestrates an underlying expect.Console and vt10x.VT virtual terminal and executes the target command within the environment. This allows for interaction with a command that is performing readline operations, emitting ANSI output, and otherwise exposing an interactive user experience.
func NewInteractiveCommandExecutor ¶
func NewInteractiveCommandExecutor(command *cobra.Command, consoleOpts ...expect.ConsoleOpt) *InteractiveCommandExecutor
NewInteractiveCommandExecutor returns a new command executor for working with interactive terminal commands
func (*InteractiveCommandExecutor) Command ¶
func (ice *InteractiveCommandExecutor) Command() *cobra.Command
Command returns the Cobra command the executor was initialized with This is typically the root command in a Cobra application
func (*InteractiveCommandExecutor) Execute ¶
func (ice *InteractiveCommandExecutor) Execute(args []string, interactionFunc InteractiveUserFunc) (*InteractiveExecutionContext, error)
Execute runs the specified command interactively and returns an execution context object upon completion
func (*InteractiveCommandExecutor) ExecuteS ¶
func (ice *InteractiveCommandExecutor) ExecuteS(argsStr string, interactionFunc InteractiveUserFunc) (*InteractiveExecutionContext, error)
ExecuteS executes the target command by splitting the args string at space boundaries This is a convenience interface suitable only for simple arguments that do not contain quoted values or literals If you need something more advanced please use the Execute() and Args() method to compose from a variadic list of arguments
func (*InteractiveCommandExecutor) SetTimeout ¶
func (ice *InteractiveCommandExecutor) SetTimeout(timeout time.Duration)
SetTimeout sets the timeout for command execution
func (*InteractiveCommandExecutor) Timeout ¶
func (ice *InteractiveCommandExecutor) Timeout() time.Duration
Timeout return the timeout for command execution A zero value indicates that no timeout is in effect
type InteractiveCommandTester ¶
type InteractiveCommandTester struct {
// contains filtered or unexported fields
}
InteractiveCommandTester instances are yielded while executing a command via ExecuteCommandInteractively and provide information about the virtual terminal execution environment
func NewInteractiveCommandTester ¶
func NewInteractiveCommandTester(command *cobra.Command, executor *InteractiveCommandExecutor, consoleOpts ...expect.ConsoleOpt) *InteractiveCommandTester
NewInteractiveCommandTester returns a new command executor for working with interactive terminal commands
func (*InteractiveCommandTester) Execute ¶
func (ict *InteractiveCommandTester) Execute( t *testing.T, args []string, testFunc func(*InteractiveTestContext) error) (*InteractiveExecutionContext, error)
Execute runs a Cobra command interactively and yields control to a func for testing the command through a console decorated with testify assertions
func (*InteractiveCommandTester) Executor ¶
func (ict *InteractiveCommandTester) Executor() *InteractiveCommandExecutor
Executor returns the command executor for this command tester
func (*InteractiveCommandTester) T ¶
func (ict *InteractiveCommandTester) T() *testing.T
T returns the test instance for this command tester
type InteractiveExecutionContext ¶
type InteractiveExecutionContext struct {
// contains filtered or unexported fields
}
InteractiveExecutionContext describes the state of an interactive terminal execution
func ExecuteInInteractiveConsole ¶
func ExecuteInInteractiveConsole( processFunc InteractiveProcessFunc, userFunc InteractiveUserFunc, consoleOpts ...expect.ConsoleOpt) (*InteractiveExecutionContext, error)
ExecuteInInteractiveConsole runs a pair of functions connected in an interactive virtual terminal environment
func ExecuteInInteractiveConsoleT ¶
func ExecuteInInteractiveConsoleT(t *testing.T, codeUnderTestFunc InteractiveProcessFunc, testFunc InteractiveUserFunc, consoleOpts ...expect.ConsoleOpt) (*InteractiveExecutionContext, error)
ExecuteInInteractiveConsoleT runs a test within an interactive console environment Execution requires a standard test instance, a pair of functions that execute the code under test and the test code, and any desired options for configuring the virtual terminal environment The raw ouytput and the terminal state are logged in the event of a test failure
func (*InteractiveExecutionContext) Console ¶
func (ice *InteractiveExecutionContext) Console() *expect.Console
Console returns the console for interacting with the terminal
func (*InteractiveExecutionContext) OutputBuffer ¶
func (ice *InteractiveExecutionContext) OutputBuffer() *bytes.Buffer
OutputBuffer returns the output buffer read from the tty
func (*InteractiveExecutionContext) PassthroughTty ¶
func (ice *InteractiveExecutionContext) PassthroughTty() *PassthroughPipeFile
PassthroughTty returns a wrapper for the Tty that supports deadline based timeouts The Std* family of methods are all aliases for the passthrough tty
func (*InteractiveExecutionContext) ReadTimeout ¶
func (ice *InteractiveExecutionContext) ReadTimeout() time.Duration
ReadTimeout returns the read time for the process side of an interactive execution expect.Console takes care of establishing a proxy pipe on the master side of the Tty but in a unit testing situation we have read failures on the slave side where the process may be waiting for input from the user
func (*InteractiveExecutionContext) Stderr ¶
func (ice *InteractiveExecutionContext) Stderr() io.Writer
Stderr returns the io.Writer to be used as stdout during execution
func (*InteractiveExecutionContext) Stdin ¶
func (ice *InteractiveExecutionContext) Stdin() io.Reader
Stdin returns the io.Reader to be used as stdin during execution
func (*InteractiveExecutionContext) Stdout ¶
func (ice *InteractiveExecutionContext) Stdout() io.Writer
Stdout returns the io.Writer to be used as stdout during execution
func (*InteractiveExecutionContext) TerminalState ¶
func (ice *InteractiveExecutionContext) TerminalState() *vt10x.State
TerminalState returns the state if the terminal
func (*InteractiveExecutionContext) Tty ¶
func (ice *InteractiveExecutionContext) Tty() *os.File
Tty returns the Tty of the underlying expect.Console instance You probably want to interact with the PassthroughTty which supports deadline based timeouts
type InteractiveProcessFunc ¶
type InteractiveProcessFunc func(*InteractiveExecutionContext) error
InteractiveProcessFunc instances are functions that represent the process side of an interactive terminal
type InteractiveTestContext ¶
type InteractiveTestContext struct { *assert.Assertions // contains filtered or unexported fields }
InteractiveTestContext instances are vended when an interactive test is run
func (*InteractiveTestContext) Assert ¶
func (ict *InteractiveTestContext) Assert() *assert.Assertions
Assert returns an assert context for suite. Normally, you can call `suite.NoError(expected, actual)`, but for situations where the embedded methods are overridden (for example, you might want to override assert.Assertions with require.Assertions), this method is provided so you can call `suite.Assert().NoError()`.
func (*InteractiveTestContext) Console ¶
func (ict *InteractiveTestContext) Console() *expect.Console
Console returns the the underlying terminal for direct interaction
func (*InteractiveTestContext) ExpectEOF ¶
func (ict *InteractiveTestContext) ExpectEOF() (string, error)
ExpectEOF waits for an EOF or an error to be emitted on the console
func (*InteractiveTestContext) ExpectMatch ¶
func (ict *InteractiveTestContext) ExpectMatch(opts ...expect.ExpectOpt) (string, error)
ExpectMatch waits for a matcher to evaluate to true against content on the console
func (*InteractiveTestContext) ExpectMatches ¶
func (ict *InteractiveTestContext) ExpectMatches(opts ...expect.ExpectOpt) (string, error)
ExpectMatches waits for a series of matchers to evaluate to true against content on the console
func (*InteractiveTestContext) ExpectString ¶
func (ict *InteractiveTestContext) ExpectString(s string) (string, error)
ExpectString waits for a string of text to ebe written to the console
func (*InteractiveTestContext) ExpectStringf ¶
func (ict *InteractiveTestContext) ExpectStringf(format string, args ...interface{}) (string, error)
ExpectStringf waits for a string of formatted text to ebe written to the console
func (*InteractiveTestContext) Require ¶
func (ict *InteractiveTestContext) Require() *require.Assertions
Require returns a require context for suite.
func (*InteractiveTestContext) RequireEOF ¶
func (ict *InteractiveTestContext) RequireEOF() (string, error)
RequireEOF waits for an EOF to be written to the console and terminates the test on timeout
func (*InteractiveTestContext) RequireMatch ¶
func (ict *InteractiveTestContext) RequireMatch(opts ...expect.ExpectOpt) (string, error)
RequireMatch waits for a matcher to evaluate truthfully to be written to the console and terminates the test on timeout
func (*InteractiveTestContext) RequireMatches ¶
func (ict *InteractiveTestContext) RequireMatches(opts ...expect.ExpectOpt) (string, error)
RequireMatches waits for a series if matcher to evaluate truthfully to be written to the console and terminates the test on timeout
func (*InteractiveTestContext) RequireString ¶
func (ict *InteractiveTestContext) RequireString(s string) (string, error)
RequireString waits for a string of text to be written to the console and terminates the test on timeout
func (*InteractiveTestContext) RequireStringf ¶
func (ict *InteractiveTestContext) RequireStringf(format string, args ...interface{}) (string, error)
RequireStringf waits for a formatted string of text to be written to the console and terminates the test on timeout
func (*InteractiveTestContext) SendLine ¶
func (ict *InteractiveTestContext) SendLine(s string) (int, error)
SendLine sends a line of text to the console
func (*InteractiveTestContext) T ¶
func (ict *InteractiveTestContext) T() *testing.T
T retrieves the current *testing.T context.
type InteractiveUserFunc ¶
type InteractiveUserFunc func(*InteractiveExecutionContext, *expect.Console) error
InteractiveUserFunc iinstances are functions that represent the user side of an interactive terminal
type PassthroughPipeFile ¶
type PassthroughPipeFile struct { *expect.PassthroughPipe // contains filtered or unexported fields }
PassthroughPipeFile wraps a file with a PassthroughPipe to add read deadline support
func NewPassthroughPipeFile ¶
func NewPassthroughPipeFile(inFile *os.File) (*PassthroughPipeFile, error)
NewPassthroughPipeFile returns a new PassthroughPipeFile that wraps the input file to enable read deadline support
func (*PassthroughPipeFile) Fd ¶
func (s *PassthroughPipeFile) Fd() uintptr
Fd proxies the file descriptor of the underyling file This is necessary because the survey library treats the stdio descriptors as concrete os.File instances instead of reader/writer interfaces
type Suite ¶
Suite provides a single struct for importing all testing functionality with sensible defaults with disambiguated names designed to read clearly in test cases.
func (*Suite) AddDefaultArg ¶
AddDefaultArg adds a default argument
func (*Suite) AddDefaultArgs ¶
AddDefaultArgs adds a series of default arguments
func (Suite) DefaultArgs ¶
DefaultArgs returns the default arguments
func (*Suite) Execute ¶
Execute runs a Cobra command with the given arguments and returns the output captured Default arguments are prepended before execution
func (*Suite) ExecuteArgs ¶
ExecuteArgs runs a Cobra command with the given arguments and returns the output captured Default arguments are prepended before execution
func (*Suite) ExecuteC ¶
ExecuteC runs a Cobra command with the given arguments and returns the Cobra command invoked and the output captured Default arguments are prepended before execution
func (*Suite) ExecuteInteractively ¶
func (h *Suite) ExecuteInteractively(args []string, interactionFunc InteractiveUserFunc) (*InteractiveExecutionContext, error)
ExecuteInteractively executes a command using the given arguments interactively Default arguments are prepended before execution
func (*Suite) ExecuteString ¶
ExecuteString executes the target command by splitting the args string at space boundaries This is a convenience interface suitable only for simple arguments that do not contain quoted values or literals If you need something more advanced please use the Execute() and Args() method to compose from a variadic list of arguments Default arguments are prepended before execution
func (*Suite) ExecuteStringInteractively ¶
func (h *Suite) ExecuteStringInteractively(argsStr string, interactionFunc InteractiveUserFunc) (*InteractiveExecutionContext, error)
ExecuteStringInteractively splits the input string at space boundaires and executes the resulting arguments interactively Default arguments are prepended before execution
func (*Suite) ExecuteTestInteractively ¶
func (h *Suite) ExecuteTestInteractively(args []string, testFunc func(*InteractiveTestContext) error) (*InteractiveExecutionContext, error)
ExecuteTestInteractively executes the given arguments in an interactive test context Default arguments are prepended before execution
func (*Suite) ExecuteTestStringInteractively ¶
func (h *Suite) ExecuteTestStringInteractively(argsStr string, testFunc func(*InteractiveTestContext) error) (*InteractiveExecutionContext, error)
ExecuteTestStringInteractively splits the input string at space boundaires and executes the resulting arguments in an interactive test context Default arguments are prepended before execution
func (*Suite) InteractiveCommandExecutor ¶
func (h *Suite) InteractiveCommandExecutor() *InteractiveCommandExecutor
InteractiveCommandExecutor returns the interactive command executor
func (*Suite) InteractiveCommandTester ¶
func (h *Suite) InteractiveCommandTester() *InteractiveCommandTester
InteractiveCommandTester returns the interactive command tester
func (Suite) InteractiveExecutionTimeout ¶
InteractiveExecutionTimeout returns the timeout for interactive command executions and tests
func (*Suite) SetCobraCommand ¶
SetCobraCommand sets the Cobra command under test Changing the command will reset the associated command executor and tester instances
func (*Suite) SetCommand ¶
func (h *Suite) SetCommand(cmd *command.BaseCommand)
SetCommand sets the Opsani command under test
func (*Suite) SetDefaultArgs ¶
SetDefaultArgs sets the default arguments
func (*Suite) SetInteractiveExecutionTimeout ¶
SetInteractiveExecutionTimeout sets the timeout for interactive command executions and tests