cmdtesting

package
v0.0.0-...-8e43f3f Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: LGPL-3.0 Imports: 9 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context

func Context(c *gc.C) *cmd.Context

Context creates a simple command execution context with the current dir set to a newly created directory within the test directory.

func ContextForDir

func ContextForDir(c *gc.C, dir string) *cmd.Context

ContextForDir creates a simple command execution context with the current dir set to the specified directory.

func HelpText

func HelpText(command cmd.Command, name string) string

HelpText returns a command's formatted help text.

func InitCommand

func InitCommand(c cmd.Command, args []string) error

InitCommand will create a new flag set, and call the Command's SetFlags and Init methods with the appropriate args.

func NewFlagSet

func NewFlagSet() *gnuflag.FlagSet

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

func RunCommand(c *gc.C, com cmd.Command, args ...string) (*cmd.Context, error)

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

func RunCommandInDir(c *gc.C, com cmd.Command, args []string, dir string) (*cmd.Context, error)

RunCommandInDir works like RunCommand, but runs with a context that uses dir.

func Stderr

func Stderr(ctx *cmd.Context) string

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.

func Stdout

func Stdout(ctx *cmd.Context) string

Stdout takes a command Context that we assume has been created in this package, and gets the content of the Stdout buffer as a string.

func TestInit

func TestInit(c *gc.C, com cmd.Command, args []string, errPat string)

TestInit checks that a command initialises correctly with the given set of arguments.

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

func NewPrompter(prompt func(string) (string, error)) *Prompter

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

func (p *Prompter) HasUnread() bool

HasUnread reports whether any input from the last prompt remains unread.

func (*Prompter) Read

func (p *Prompter) Read(buf []byte) (int, error)

Read implements io.Reader.

func (*Prompter) String

func (p *Prompter) String() string

String returns all the text that has been written to the prompter since it was created.

func (*Prompter) Tail

func (p *Prompter) Tail() string

Tail returns all the text written since the last prompt.

func (*Prompter) Write

func (p *Prompter) Write(buf []byte) (int, error)

Write implements io.Writer.

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.

Jump to

Keyboard shortcuts

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