fake

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package fake is used to fake calls github.com/openconfig/kne/exec.

Typical Usage:

import "github.com/openconfig/kne/exec"

{
	responses := []fake.Response{...}
	cmds := fake.Commands(responses)
	oCommand := kexec.Command
	defer func() {
		kexec.Command = oCommand
		if err := cmds.Done(); err != nil {
			// handle the error
		}
	}()
	kexec.Command = cmds.Command

	... test code ...

}

Index

Constants

This section is empty.

Variables

View Source
var LogCommand = func(string) {}

LogCommand is called with the string representation of the command that is running. The test program can optionally set this to their own function.

Functions

This section is empty.

Types

type Command

type Command struct {
	Name string // if set it is included in errors
	// contains filtered or unexported fields
}

A Command is an implementation of exec.Cmd that is used to return predefined results when exec.Cmd.Run is called.

func Commands

func Commands(resp []Response) *Command

Commands returns a Command that is primed with the provided responses. It's Command method can be used to override exec.Command.

func (*Command) Command

func (c *Command) Command(cmd string, args ...string) exec.Cmd

Command resets the command associated with c.

func (*Command) Done

func (c *Command) Done() error

Done returns an error if there were any unexpected commands called on c or if there are any non-optional responses left.

Done should be called once the test has finished calling c.Command.

func (*Command) Run

func (c *Command) Run() error

Run runs the command. It expects there to be a corresponding Response to the command. If the command does not match the first remaining response Run searches for a matching Response that has OutOfOrder set to true. It also calls LogCommand with a string representation of a Response that matches this command.

Run returns nil if no matching response is found. Use c.Done to detect these errors.

func (*Command) SetStderr

func (c *Command) SetStderr(w io.Writer)

Stderr sets standard err to w.

func (*Command) SetStdout

func (c *Command) SetStdout(w io.Writer)

Stdout sets standard out to w.

type DoneError

type DoneError struct {
	Source     string
	Unexpected []Response // Unexpected calls
	Unused     []Response // Unused calls
}

A DoneError is returned when the calls to a Command do not match the Responses.

func (*DoneError) Error

func (e *DoneError) Error() string

type Response

type Response struct {
	Cmd        string
	Args       []string
	Err        interface{}
	Stdout     string
	Stderr     string
	OutOfOrder bool // This response can be out of order
	Optional   bool // This response might not be used
}

A Response indicates how Command should respond to Run.

func (Response) String

func (r Response) String() string

Jump to

Keyboard shortcuts

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