Documentation
¶
Index ¶
- func Context(c *gc.C) *cmd.Context
- func ContextForDir(c *gc.C, dir string) *cmd.Context
- func HelpText(command cmd.Command, name string) string
- func InitCommand(c cmd.Command, args []string) error
- func NewFlagSet() *gnuflag.FlagSet
- func RunCommand(c *gc.C, com cmd.Command, args ...string) (*cmd.Context, error)
- func RunCommandInDir(c *gc.C, com cmd.Command, args []string, dir string) (*cmd.Context, error)
- func RunCommandWithContext(ctx *cmd.Context, com cmd.Command, args ...string) chan error
- func Stderr(ctx *cmd.Context) string
- func Stdout(ctx *cmd.Context) string
- func TestInit(c *gc.C, com cmd.Command, args []string, errPat string)
- type Prompter
- type SeqPrompter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Context ¶
Context creates a simple command execution context with the current dir set to a newly created directory within the test directory.
func ContextForDir ¶
ContextForDir creates a simple command execution context with the current dir set to the specified directory.
func InitCommand ¶
InitCommand will create a new flag set, and call the Command's SetFlags and Init methods with the appropriate args.
func NewFlagSet ¶
NewFlagSet creates a new flag set using the standard options, particularly the option to stop the gnuflag methods from writing to StdErr or StdOut.
func RunCommand ¶
RunCommand runs a command with the specified args. The returned error may come from either the parsing of the args, the command initialisation, or the actual running of the command. Access to the resulting output streams is provided through the returned context instance.
func RunCommandInDir ¶
RunCommandInDir works like RunCommand, but runs with a context that uses dir.
func RunCommandWithContext ¶
RunCommandWithContext runs the command asynchronously with the specified context and returns a channel which providers the command's errors.
func Stderr ¶
Stderr takes a command Context that we assume has been created in this package, and gets the content of the Stderr buffer as a string.
Types ¶
type Prompter ¶
type Prompter struct {
// contains filtered or unexported fields
}
Prompter is designed to be used in a cmd.Context to check interactive request-response sequences using stdin and stdout.
func NewPrompter ¶
NewPrompter returns an io.ReadWriter implementation that calls the given function every time Read is called after some text has been written or if all the previously returned text has been read. The function's argument contains all the text printed since the last input. The function should return the text that the user is expected to type, or an error to return from Read. If it returns an empty string, and no error, it will return io.EOF instead.
func (*Prompter) HasUnread ¶
HasUnread reports whether any input from the last prompt remains unread.
func (*Prompter) String ¶
String returns all the text that has been written to the prompter since it was created.
type SeqPrompter ¶
type SeqPrompter struct { *Prompter // contains filtered or unexported fields }
func NewSeqPrompter ¶
func NewSeqPrompter(c *gc.C, userInputMarker, text string) *SeqPrompter
NewSeqPrompter returns a prompter that can be used to check a sequence of IO interactions. Expected input from the user is marked with the given user input marker (for example a distinctive unicode character that will not occur in the rest of the text) and runs to the end of a line.
All output text in between user input is treated as regular expressions.
As a special case, if an input marker is followed only by a single input marker on that line, the checker will cause io.EOF to be returned for that prompt.
The returned SeqPrompter wraps a Prompter and checks that each read and write corresponds to the expected action in the sequence.
After all interaction is done, CheckDone or AssertDone should be called to check that no more interactions are expected.
Any failures will result in the test failing.
For example given the prompter created with:
checker := NewSeqPrompter(c, "»", `What is your name: »Bob And your age: »148 You're .* old, Bob! `)
The following code will pass the checker:
fmt.Fprintf(checker, "What is your name: ") buf := make([]byte, 100) n, _ := checker.Read(buf) name := strings.TrimSpace(string(buf[0:n])) fmt.Fprintf(checker, "And your age: ") n, _ = checker.Read(buf) age, err := strconv.Atoi(strings.TrimSpace(string(buf[0:n]))) c.Assert(err, gc.IsNil) if age > 90 { fmt.Fprintf(checker, "You're very old, %s!\n", name) } checker.CheckDone()
func (*SeqPrompter) AssertDone ¶
func (p *SeqPrompter) AssertDone()
AssertDone is like CheckDone but aborts the test if the check fails.
func (*SeqPrompter) CheckDone ¶
func (p *SeqPrompter) CheckDone() bool
CheckDone asserts that all the expected prompts have been printed and all the replies read, and reports whether the check succeeded.