fake

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: Apache-2.0 Imports: 6 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 ...

}

A Command is safe for concurrent use. Each call to Command returns an independent exec.Cmd, and the responses are consumed under a lock, so code under test may run commands from multiple goroutines. Note that responses are still matched in order by default: when the commands may be issued concurrently, the order they arrive in is not deterministic, so the corresponding responses must be marked OutOfOrder.

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 hands out exec.Cmd implementations that return predefined results when exec.Cmd.Run is called. The zero value is not useful; use Commands.

A Command is safe for concurrent use by multiple goroutines.

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 returns a new exec.Cmd that draws its result from c when run.

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 and all commands it handed out have finished running.

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