iexec

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2017 License: MIT Imports: 8 Imported by: 2

README

iexec

When writing unit tests for a package that calls a specific system command, it can be helpful to assert that the call was made without actually calling the target binary (it may be long running or have side effects that are undesirable to have to deal with in a unit test). iexec.Exec.Command is a wrapper around exec.Command that makes this possible by using a provider pattern.

Installation

go get github.com/BooleanCat/igo/os/exec

Usage

Here the key features of iexec are shown, for a full example ginkgo test suite see the example/ folder.

Suppose we have some function, foo, that calls out to a system command.

func foo() {
    cmd := exec.Command("my-binary")
    cmd.Run()
}

It would be preferable, for the purposes of a unit test, to check that my-binary would have been called without actually calling it; enter iexec. If the function is rewritten as:

func foo(command iexec.CmdProvider) {
    cmd := command("my-binary")
    cmd.Run()
}

Since the CmdProvider has been used, we can substitute a fake instead of the real thing that can be used to make assertions. This is demonstrated using the ginkgo testing framework below.

...
Describe("foo", func(), {
    var (
        execFake    *iexec.ExecFake
        commandName string
    )

    BeforeEach(func() {
        execFake = new(iexec.ExecFake)
        foo(execFake.Command)
        commandName, _ = execFake.CommandArgsForCall(0)
    })

    It("calls out to my-binary", func() {
        Expect(commandName).To(Equal("my-binary"))
    })
})
...

Using new(iexec.Exec).Command as a provider to foo will behave exactly as exec.Commnd does.

In the example above, we haven't covered testing around whether or not our function called cmd.Run(). This can be easily achieved by using the helper iexec.NewPureFake():

...
Describe("foo", func(), {
    var execFakes *iexec.PureFake

    BeforeEach(func() {
        execFakes = iexec.NewPureFake()
        foo(execFakes.Exec.Command)
    })

    It("runs my-binary", func() {
        Expect(execFakes.Cmd.RunCallCount()).To(Equal(1))
    })
})
...

iexec.NewPureFake() will even set up a fake ios.Process (interfacing os.Process). In the below example, the killing of a process can be asserted.

func bar(command iexec.CmdProvider) {
    cmd := command("my-binary")
    cmd.Start()
    cmd.GetProcess().Kill()
}

...
Describe("foo", func(), {
    var execFakes *iexec.PureFake

    BeforeEach(func() {
        execFakes = iexec.NewPureFake()
        bar(execFakes.Exec.Command)
    })

    It("kills my-binary", func() {
        Expect(execFakes.Process.KillCallCount()).To(Equal(1))
    })
})
...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd interface {
	CombinedOutput() ([]byte, error)
	Output() ([]byte, error)
	Run() error
	Start() error
	StderrPipe() (io.ReadCloser, error)
	StdinPipe() (io.WriteCloser, error)
	StdoutPipe() (io.ReadCloser, error)
	Wait() error
	GetPath() string
	SetPath(string)
	GetArgs() []string
	SetArgs([]string)
	GetEnv() []string
	SetEnv([]string)
	GetDir() string
	SetDir(string)
	GetStdin() io.Reader
	SetStdin(io.Reader)
	GetStdout() io.Writer
	SetStdout(io.Writer)
	GetStderr() io.Writer
	SetStderr(io.Writer)
	GetExtraFiles() []*os.File
	SetExtraFiles([]*os.File)
	GetSysProcAttr() *syscall.SysProcAttr
	SetSysProcAttr(*syscall.SysProcAttr)
	GetProcess() ios.Process
	SetProcess(*os.Process)
	GetProcessState() *os.ProcessState
	SetProcessState(*os.ProcessState)
}

Cmd is an interface around exec.Cmd

type CmdFake

type CmdFake struct {
	CombinedOutputStub func() ([]byte, error)

	OutputStub func() ([]byte, error)

	RunStub func() error

	StartStub func() error

	StderrPipeStub func() (io.ReadCloser, error)

	StdinPipeStub func() (io.WriteCloser, error)

	StdoutPipeStub func() (io.ReadCloser, error)

	WaitStub func() error

	GetPathStub func() string

	SetPathStub func(string)

	GetArgsStub func() []string

	SetArgsStub func([]string)

	GetEnvStub func() []string

	SetEnvStub func([]string)

	GetDirStub func() string

	SetDirStub func(string)

	GetStdinStub func() io.Reader

	SetStdinStub func(io.Reader)

	GetStdoutStub func() io.Writer

	SetStdoutStub func(io.Writer)

	GetStderrStub func() io.Writer

	SetStderrStub func(io.Writer)

	GetExtraFilesStub func() []*os.File

	SetExtraFilesStub func([]*os.File)

	GetSysProcAttrStub func() *syscall.SysProcAttr

	SetSysProcAttrStub func(*syscall.SysProcAttr)

	GetProcessStub func() ios.Process

	SetProcessStub func(*os.Process)

	GetProcessStateStub func() *os.ProcessState

	SetProcessStateStub func(*os.ProcessState)
	// contains filtered or unexported fields
}

CmdFake ...

func NewCmdFake

func NewCmdFake() *CmdFake

NewCmdFake is the preferred way to initialise a CmdFake

func (*CmdFake) CombinedOutput

func (fake *CmdFake) CombinedOutput() ([]byte, error)

CombinedOutput ...

func (*CmdFake) CombinedOutputCallCount

func (fake *CmdFake) CombinedOutputCallCount() int

CombinedOutputCallCount ...

func (*CmdFake) CombinedOutputReturns

func (fake *CmdFake) CombinedOutputReturns(result1 []byte, result2 error)

CombinedOutputReturns ...

func (*CmdFake) GetArgs

func (fake *CmdFake) GetArgs() []string

GetArgs ...

func (*CmdFake) GetArgsCallCount

func (fake *CmdFake) GetArgsCallCount() int

GetArgsCallCount ...

func (*CmdFake) GetArgsReturns

func (fake *CmdFake) GetArgsReturns(result1 []string)

GetArgsReturns ...

func (*CmdFake) GetDir

func (fake *CmdFake) GetDir() string

GetDir ...

func (*CmdFake) GetDirCallCount

func (fake *CmdFake) GetDirCallCount() int

GetDirCallCount ...

func (*CmdFake) GetDirReturns

func (fake *CmdFake) GetDirReturns(result1 string)

GetDirReturns ...

func (*CmdFake) GetEnv

func (fake *CmdFake) GetEnv() []string

GetEnv ...

func (*CmdFake) GetEnvCallCount

func (fake *CmdFake) GetEnvCallCount() int

GetEnvCallCount ...

func (*CmdFake) GetEnvReturns

func (fake *CmdFake) GetEnvReturns(result1 []string)

GetEnvReturns ...

func (*CmdFake) GetExtraFiles

func (fake *CmdFake) GetExtraFiles() []*os.File

GetExtraFiles ...

func (*CmdFake) GetExtraFilesCallCount

func (fake *CmdFake) GetExtraFilesCallCount() int

GetExtraFilesCallCount ...

func (*CmdFake) GetExtraFilesReturns

func (fake *CmdFake) GetExtraFilesReturns(result1 []*os.File)

GetExtraFilesReturns ...

func (*CmdFake) GetPath

func (fake *CmdFake) GetPath() string

GetPath ...

func (*CmdFake) GetPathCallCount

func (fake *CmdFake) GetPathCallCount() int

GetPathCallCount ...

func (*CmdFake) GetPathReturns

func (fake *CmdFake) GetPathReturns(result1 string)

GetPathReturns ...

func (*CmdFake) GetProcess

func (fake *CmdFake) GetProcess() ios.Process

GetProcess ...

func (*CmdFake) GetProcessCallCount

func (fake *CmdFake) GetProcessCallCount() int

GetProcessCallCount ...

func (*CmdFake) GetProcessReturns

func (fake *CmdFake) GetProcessReturns(result1 ios.Process)

GetProcessReturns ...

func (*CmdFake) GetProcessState

func (fake *CmdFake) GetProcessState() *os.ProcessState

GetProcessState ...

func (*CmdFake) GetProcessStateCallCount

func (fake *CmdFake) GetProcessStateCallCount() int

GetProcessStateCallCount ...

func (*CmdFake) GetProcessStateReturns

func (fake *CmdFake) GetProcessStateReturns(result1 *os.ProcessState)

GetProcessStateReturns ...

func (*CmdFake) GetStderr

func (fake *CmdFake) GetStderr() io.Writer

GetStderr ...

func (*CmdFake) GetStderrCallCount

func (fake *CmdFake) GetStderrCallCount() int

GetStderrCallCount ...

func (*CmdFake) GetStderrReturns

func (fake *CmdFake) GetStderrReturns(result1 io.Writer)

GetStderrReturns ...

func (*CmdFake) GetStdin

func (fake *CmdFake) GetStdin() io.Reader

GetStdin ...

func (*CmdFake) GetStdinCallCount

func (fake *CmdFake) GetStdinCallCount() int

GetStdinCallCount ...

func (*CmdFake) GetStdinReturns

func (fake *CmdFake) GetStdinReturns(result1 io.Reader)

GetStdinReturns ...

func (*CmdFake) GetStdout

func (fake *CmdFake) GetStdout() io.Writer

GetStdout ...

func (*CmdFake) GetStdoutCallCount

func (fake *CmdFake) GetStdoutCallCount() int

GetStdoutCallCount ...

func (*CmdFake) GetStdoutReturns

func (fake *CmdFake) GetStdoutReturns(result1 io.Writer)

GetStdoutReturns ...

func (*CmdFake) GetSysProcAttr

func (fake *CmdFake) GetSysProcAttr() *syscall.SysProcAttr

GetSysProcAttr ...

func (*CmdFake) GetSysProcAttrCallCount

func (fake *CmdFake) GetSysProcAttrCallCount() int

GetSysProcAttrCallCount ...

func (*CmdFake) GetSysProcAttrReturns

func (fake *CmdFake) GetSysProcAttrReturns(result1 *syscall.SysProcAttr)

GetSysProcAttrReturns ...

func (*CmdFake) Invocations

func (fake *CmdFake) Invocations() map[string][][]interface{}

Invocations ...

func (*CmdFake) Output

func (fake *CmdFake) Output() ([]byte, error)

Output ...

func (*CmdFake) OutputCallCount

func (fake *CmdFake) OutputCallCount() int

OutputCallCount ...

func (*CmdFake) OutputReturns

func (fake *CmdFake) OutputReturns(result1 []byte, result2 error)

OutputReturns ...

func (*CmdFake) Run

func (fake *CmdFake) Run() error

Run ...

func (*CmdFake) RunCallCount

func (fake *CmdFake) RunCallCount() int

RunCallCount ...

func (*CmdFake) RunReturns

func (fake *CmdFake) RunReturns(result1 error)

RunReturns ...

func (*CmdFake) SetArgs

func (fake *CmdFake) SetArgs(arg1 []string)

SetArgs ...

func (*CmdFake) SetArgsArgsForCall

func (fake *CmdFake) SetArgsArgsForCall(i int) []string

SetArgsArgsForCall ...

func (*CmdFake) SetArgsCallCount

func (fake *CmdFake) SetArgsCallCount() int

SetArgsCallCount ...

func (*CmdFake) SetDir

func (fake *CmdFake) SetDir(arg1 string)

SetDir ...

func (*CmdFake) SetDirArgsForCall

func (fake *CmdFake) SetDirArgsForCall(i int) string

SetDirArgsForCall ...

func (*CmdFake) SetDirCallCount

func (fake *CmdFake) SetDirCallCount() int

SetDirCallCount ...

func (*CmdFake) SetEnv

func (fake *CmdFake) SetEnv(arg1 []string)

SetEnv ...

func (*CmdFake) SetEnvArgsForCall

func (fake *CmdFake) SetEnvArgsForCall(i int) []string

SetEnvArgsForCall ...

func (*CmdFake) SetEnvCallCount

func (fake *CmdFake) SetEnvCallCount() int

SetEnvCallCount ...

func (*CmdFake) SetExtraFiles

func (fake *CmdFake) SetExtraFiles(arg1 []*os.File)

SetExtraFiles ...

func (*CmdFake) SetExtraFilesArgsForCall

func (fake *CmdFake) SetExtraFilesArgsForCall(i int) []*os.File

SetExtraFilesArgsForCall ...

func (*CmdFake) SetExtraFilesCallCount

func (fake *CmdFake) SetExtraFilesCallCount() int

SetExtraFilesCallCount ...

func (*CmdFake) SetPath

func (fake *CmdFake) SetPath(arg1 string)

SetPath ...

func (*CmdFake) SetPathArgsForCall

func (fake *CmdFake) SetPathArgsForCall(i int) string

SetPathArgsForCall ...

func (*CmdFake) SetPathCallCount

func (fake *CmdFake) SetPathCallCount() int

SetPathCallCount ...

func (*CmdFake) SetProcess

func (fake *CmdFake) SetProcess(arg1 *os.Process)

SetProcess ...

func (*CmdFake) SetProcessArgsForCall

func (fake *CmdFake) SetProcessArgsForCall(i int) *os.Process

SetProcessArgsForCall ...

func (*CmdFake) SetProcessCallCount

func (fake *CmdFake) SetProcessCallCount() int

SetProcessCallCount ...

func (*CmdFake) SetProcessState

func (fake *CmdFake) SetProcessState(arg1 *os.ProcessState)

SetProcessState ...

func (*CmdFake) SetProcessStateArgsForCall

func (fake *CmdFake) SetProcessStateArgsForCall(i int) *os.ProcessState

SetProcessStateArgsForCall ...

func (*CmdFake) SetProcessStateCallCount

func (fake *CmdFake) SetProcessStateCallCount() int

SetProcessStateCallCount ...

func (*CmdFake) SetStderr

func (fake *CmdFake) SetStderr(arg1 io.Writer)

SetStderr ...

func (*CmdFake) SetStderrArgsForCall

func (fake *CmdFake) SetStderrArgsForCall(i int) io.Writer

SetStderrArgsForCall ...

func (*CmdFake) SetStderrCallCount

func (fake *CmdFake) SetStderrCallCount() int

SetStderrCallCount ...

func (*CmdFake) SetStdin

func (fake *CmdFake) SetStdin(arg1 io.Reader)

SetStdin ...

func (*CmdFake) SetStdinArgsForCall

func (fake *CmdFake) SetStdinArgsForCall(i int) io.Reader

SetStdinArgsForCall ...

func (*CmdFake) SetStdinCallCount

func (fake *CmdFake) SetStdinCallCount() int

SetStdinCallCount ...

func (*CmdFake) SetStdout

func (fake *CmdFake) SetStdout(arg1 io.Writer)

SetStdout ...

func (*CmdFake) SetStdoutArgsForCall

func (fake *CmdFake) SetStdoutArgsForCall(i int) io.Writer

SetStdoutArgsForCall ...

func (*CmdFake) SetStdoutCallCount

func (fake *CmdFake) SetStdoutCallCount() int

SetStdoutCallCount ...

func (*CmdFake) SetSysProcAttr

func (fake *CmdFake) SetSysProcAttr(arg1 *syscall.SysProcAttr)

SetSysProcAttr ...

func (*CmdFake) SetSysProcAttrArgsForCall

func (fake *CmdFake) SetSysProcAttrArgsForCall(i int) *syscall.SysProcAttr

SetSysProcAttrArgsForCall ...

func (*CmdFake) SetSysProcAttrCallCount

func (fake *CmdFake) SetSysProcAttrCallCount() int

SetSysProcAttrCallCount ...

func (*CmdFake) Start

func (fake *CmdFake) Start() error

Start ...

func (*CmdFake) StartCallCount

func (fake *CmdFake) StartCallCount() int

StartCallCount ...

func (*CmdFake) StartReturns

func (fake *CmdFake) StartReturns(result1 error)

StartReturns ...

func (*CmdFake) StderrPipe

func (fake *CmdFake) StderrPipe() (io.ReadCloser, error)

StderrPipe ...

func (*CmdFake) StderrPipeCallCount

func (fake *CmdFake) StderrPipeCallCount() int

StderrPipeCallCount ...

func (*CmdFake) StderrPipeReturns

func (fake *CmdFake) StderrPipeReturns(result1 io.ReadCloser, result2 error)

StderrPipeReturns ...

func (*CmdFake) StdinPipe

func (fake *CmdFake) StdinPipe() (io.WriteCloser, error)

StdinPipe ...

func (*CmdFake) StdinPipeCallCount

func (fake *CmdFake) StdinPipeCallCount() int

StdinPipeCallCount ...

func (*CmdFake) StdinPipeReturns

func (fake *CmdFake) StdinPipeReturns(result1 io.WriteCloser, result2 error)

StdinPipeReturns ...

func (*CmdFake) StdoutPipe

func (fake *CmdFake) StdoutPipe() (io.ReadCloser, error)

StdoutPipe ...

func (*CmdFake) StdoutPipeCallCount

func (fake *CmdFake) StdoutPipeCallCount() int

StdoutPipeCallCount ...

func (*CmdFake) StdoutPipeReturns

func (fake *CmdFake) StdoutPipeReturns(result1 io.ReadCloser, result2 error)

StdoutPipeReturns ...

func (*CmdFake) Wait

func (fake *CmdFake) Wait() error

Wait ...

func (*CmdFake) WaitCallCount

func (fake *CmdFake) WaitCallCount() int

WaitCallCount ...

func (*CmdFake) WaitReturns

func (fake *CmdFake) WaitReturns(result1 error)

WaitReturns ...

type CmdProvider

type CmdProvider func(name string, args ...string) Cmd

CmdProvider is a type alias for exec.Command

type CmdReal

type CmdReal struct {
	// contains filtered or unexported fields
}

CmdReal is a wrapper around exec.Cmd that implements iexec.Cmd

func NewCmd

func NewCmd(cmd ...*exec.Cmd) *CmdReal

NewCmd creates a struct that behaves like exec.Cmd

func (*CmdReal) CombinedOutput

func (c *CmdReal) CombinedOutput() ([]byte, error)

CombinedOutput is a wrapper around exec.Cmd.CombinedOutput()

func (*CmdReal) GetArgs

func (c *CmdReal) GetArgs() []string

GetArgs is a wrapper around getting exec.Cmd.Args

func (*CmdReal) GetDir

func (c *CmdReal) GetDir() string

GetDir is a wrapper around getting exec.Cmd.Dir

func (*CmdReal) GetEnv

func (c *CmdReal) GetEnv() []string

GetEnv is a wrapper around getting exec.Cmd.Env

func (*CmdReal) GetExtraFiles

func (c *CmdReal) GetExtraFiles() []*os.File

GetExtraFiles is a wrapper around getting exec.Cmd.ExtraFiles

func (*CmdReal) GetPath

func (c *CmdReal) GetPath() string

GetPath is a wrapper around getting exec.Cmd.Path

func (*CmdReal) GetProcess

func (c *CmdReal) GetProcess() ios.Process

GetProcess is a wrapper around getting exec.Cmd.Process as an ios.Process

func (*CmdReal) GetProcessState

func (c *CmdReal) GetProcessState() *os.ProcessState

GetProcessState is a wrapper around getting exec.Cmd.ProcessState

func (*CmdReal) GetStderr

func (c *CmdReal) GetStderr() io.Writer

GetStderr is a wrapper around getting exec.Cmd.Stderr

func (*CmdReal) GetStdin

func (c *CmdReal) GetStdin() io.Reader

GetStdin is a wrapper around getting exec.Cmd.Stdin

func (*CmdReal) GetStdout

func (c *CmdReal) GetStdout() io.Writer

GetStdout is a wrapper around getting exec.Cmd.Stdout

func (*CmdReal) GetSysProcAttr

func (c *CmdReal) GetSysProcAttr() *syscall.SysProcAttr

GetSysProcAttr is a wrapper around getting exec.Cmd.SysProcAttr

func (*CmdReal) Output

func (c *CmdReal) Output() ([]byte, error)

Output is a wrapper around exec.Cmd.Output()

func (*CmdReal) Run

func (c *CmdReal) Run() error

Run is a wrapper around exec.Cmd.Run()

func (*CmdReal) SetArgs

func (c *CmdReal) SetArgs(args []string)

SetArgs is a wrapper around setting exec.Cmd.Args

func (*CmdReal) SetDir

func (c *CmdReal) SetDir(dir string)

SetDir is a wrapper around setting exec.Cmd.Dir

func (*CmdReal) SetEnv

func (c *CmdReal) SetEnv(env []string)

SetEnv is a wrapper around setting exec.Cmd.Env

func (*CmdReal) SetExtraFiles

func (c *CmdReal) SetExtraFiles(files []*os.File)

SetExtraFiles is a wrapper around setting exec.Cmd.ExtraFiles

func (*CmdReal) SetPath

func (c *CmdReal) SetPath(path string)

SetPath is a wrapper around setting exec.Cmd.Path

func (*CmdReal) SetProcess

func (c *CmdReal) SetProcess(process *os.Process)

SetProcess is a wrapper around setting exec.Cmd.Process

func (*CmdReal) SetProcessState

func (c *CmdReal) SetProcessState(state *os.ProcessState)

SetProcessState is a wrapper around setting exec.Cmd.ProcessState

func (*CmdReal) SetStderr

func (c *CmdReal) SetStderr(stderr io.Writer)

SetStderr is a wrapper around setting exec.Cmd.Stderr

func (*CmdReal) SetStdin

func (c *CmdReal) SetStdin(stdin io.Reader)

SetStdin is a wrapper around setting exec.Cmd.Stdin

func (*CmdReal) SetStdout

func (c *CmdReal) SetStdout(stdout io.Writer)

SetStdout is a wrapper around setting exec.Cmd.Stdout

func (*CmdReal) SetSysProcAttr

func (c *CmdReal) SetSysProcAttr(attr *syscall.SysProcAttr)

SetSysProcAttr is a wrapper around setting exec.Cmd.SysProcAttr

func (*CmdReal) Start

func (c *CmdReal) Start() error

Start is a wrapper around exec.Cmd.Start()

func (*CmdReal) StderrPipe

func (c *CmdReal) StderrPipe() (io.ReadCloser, error)

StderrPipe is a wrapper around exec.Cmd.StderrPipe()

func (*CmdReal) StdinPipe

func (c *CmdReal) StdinPipe() (io.WriteCloser, error)

StdinPipe is a wrapper around exec.Cmd.StdinPipe()

func (*CmdReal) StdoutPipe

func (c *CmdReal) StdoutPipe() (io.ReadCloser, error)

StdoutPipe is a wrapper around exec.Cmd.StdoutPipe()

func (*CmdReal) Wait

func (c *CmdReal) Wait() error

Wait is a wrapper around exec.Cmd.Wait()

type Exec

type Exec interface {
	Command(string, ...string) Cmd
}

Exec is an interface around os/exec

type Fake

type Fake struct {
	CommandStub func(string, ...string) Cmd
	// contains filtered or unexported fields
}

Fake ...

func NewFake

func NewFake() *Fake

NewFake is the preferred way to initialise a Fake

func (*Fake) Command

func (fake *Fake) Command(arg1 string, arg2 ...string) Cmd

Command ...

func (*Fake) CommandArgsForCall

func (fake *Fake) CommandArgsForCall(i int) (string, []string)

CommandArgsForCall ...

func (*Fake) CommandCallCount

func (fake *Fake) CommandCallCount() int

CommandCallCount ...

func (*Fake) CommandReturns

func (fake *Fake) CommandReturns(result1 Cmd)

CommandReturns ...

func (*Fake) Invocations

func (fake *Fake) Invocations() map[string][][]interface{}

Invocations ...

type NestedCommandFake

type NestedCommandFake struct {
	Exec    *Fake
	Cmd     *CmdFake
	Process *ios.ProcessFake
}

NestedCommandFake is a struct containing a fake Exec with nested initialised fake members.

The following Fakes are available: - Cmd: a FakeCmd returned by Exec.Command() - Process: a FakeProcess returned by Cmd.GetProcess()

func NewNestedCommandFake

func NewNestedCommandFake() *NestedCommandFake

NewNestedCommandFake returns a fake Exec with nested new fakes within

type Real

type Real struct{}

Real is a wrapper around exec that implements iexec.Exec

func New

func New() *Real

New creates a struct that behaves like the exec package

func (*Real) Command

func (*Real) Command(name string, args ...string) Cmd

Command is a wrapper around exec.Command()

Jump to

Keyboard shortcuts

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