Documentation
¶
Index ¶
- Constants
- Variables
- func AnyArguments() func(arg ...string) ArgumentsMatchResult
- func ExpectEnv(t *testing.T, environ []string, expect ...string) error
- func FormatInterfaces(a []interface{}) string
- func FormatStrings(a []string) string
- func GetEnv(key string, environ []string) (string, bool)
- func StopServer() error
- type Arguments
- type ArgumentsMatchResult
- type Call
- type Client
- type Expectation
- func (e *Expectation) AndCallFunc(f func(*Call)) *Expectation
- func (e *Expectation) AndExitWith(code int) *Expectation
- func (e *Expectation) AndPassthroughToLocalCommand(path string) *Expectation
- func (e *Expectation) AndWriteToStderr(s string) *Expectation
- func (e *Expectation) AndWriteToStdout(s string) *Expectation
- func (e *Expectation) AtLeastOnce() *Expectation
- func (e *Expectation) Check(t TestingT) bool
- func (e *Expectation) Exactly(expect int) *Expectation
- func (e *Expectation) Max(expect int) *Expectation
- func (e *Expectation) Min(expect int) *Expectation
- func (e *Expectation) NotCalled() *Expectation
- func (e *Expectation) Once() *Expectation
- func (e *Expectation) Optionally() *Expectation
- func (e *Expectation) String() string
- func (e *Expectation) WithAnyArguments() *Expectation
- func (e *Expectation) WithMatcherFunc(f func(arg ...string) ArgumentsMatchResult) *Expectation
- func (e *Expectation) WithStdin(match interface{}) *Expectation
- type ExpectationResult
- type ExpectationResultSet
- type ExpectationSet
- type Invocation
- type Matcher
- type MatcherFunc
- type Mock
- func (m *Mock) Before(f func(i Invocation) error) *Mock
- func (m *Mock) Check(t TestingT) bool
- func (m *Mock) CheckAndClose(t TestingT) error
- func (m *Mock) Close() error
- func (m *Mock) Expect(args ...interface{}) *Expectation
- func (m *Mock) ExpectAll(argSlices [][]interface{})
- func (m *Mock) IgnoreUnexpectedInvocations() *Mock
- func (m *Mock) PassthroughToLocalCommand() *Mock
- type Proxy
- type Server
- type TestingT
Examples ¶
Constants ¶
const (
InfiniteTimes = -1
)
const (
ServerEnvVar = `BINTEST_PROXY_SERVER`
)
Variables ¶
var (
Debug bool
)
var ErrNoExpectationsMatch = errors.New("No expectations match")
Functions ¶
func AnyArguments ¶
func AnyArguments() func(arg ...string) ArgumentsMatchResult
AnyArguments is a helper function for matching any argument set in WithMatcherFunc
func ExpectEnv ¶
ExpectEnv asserts that certain environment vars/values exist, otherwise an error is reported to T and a matching error is returned (for Before)
func FormatInterfaces ¶
func FormatInterfaces(a []interface{}) string
FormatInterfaces formats a slice of interface{} as quoted comma-separated arguments
func FormatStrings ¶
FormatStrings formats a slice of strings as quoted comma-separated arguments
Types ¶
type Arguments ¶
type Arguments []interface{}
func ArgumentsFromStrings ¶
func (Arguments) Match ¶
func (a Arguments) Match(x ...string) (result ArgumentsMatchResult)
type ArgumentsMatchResult ¶
type Call ¶
type Call struct { PID int Name string Args []string Env []string Dir string // Stdout is the output writer to send stdout to in the proxied binary Stdout io.WriteCloser `json:"-"` // Stderr is the output writer to send stdout to in the proxied binary Stderr io.WriteCloser `json:"-"` // Stdin is the input reader for stdin from the proxied binary Stdin io.ReadCloser `json:"-"` // contains filtered or unexported fields }
Call is created for every call to the proxied binary
func (*Call) Fatal ¶
Fatal exits the call and returns the passed error. If it's a exec.ExitError the exit code is used
func (*Call) Passthrough ¶
Passthrough invokes another local binary and returns the results
type Client ¶
type Client struct { Debug bool URL string Args []string Dir string Env []string PID int Stdin io.ReadCloser Stdout io.WriteCloser Stderr io.WriteCloser }
func NewClientFromEnv ¶
func NewClientFromEnv() *Client
type Expectation ¶
Expectation is used for setting expectations
func (*Expectation) AndCallFunc ¶
func (e *Expectation) AndCallFunc(f func(*Call)) *Expectation
AndCallFunc causes a middleware function to be called before invocation
func (*Expectation) AndExitWith ¶
func (e *Expectation) AndExitWith(code int) *Expectation
AndExitWith causes the invoker to finish with an exit code of code
func (*Expectation) AndPassthroughToLocalCommand ¶
func (e *Expectation) AndPassthroughToLocalCommand(path string) *Expectation
AndPassthroughToLocalCommand causes the invoker to defer to a local command
func (*Expectation) AndWriteToStderr ¶
func (e *Expectation) AndWriteToStderr(s string) *Expectation
AndWriteToStderr causes the invoker to output s to stderr. This resets any passthrough path set
func (*Expectation) AndWriteToStdout ¶
func (e *Expectation) AndWriteToStdout(s string) *Expectation
AndWriteToStdout causes the invoker to output s to stdout. This resets any passthrough path set
func (*Expectation) AtLeastOnce ¶
func (e *Expectation) AtLeastOnce() *Expectation
AtLeastOnce expects a minimum invocations of 0 and a max of InfinityTimes
func (*Expectation) Check ¶
func (e *Expectation) Check(t TestingT) bool
Check evaluates the expectation and outputs failures to the provided testing.T object
func (*Expectation) Exactly ¶
func (e *Expectation) Exactly(expect int) *Expectation
Exactly expects exactly n invocations of this expectation
func (*Expectation) Max ¶
func (e *Expectation) Max(expect int) *Expectation
Max expects a maximum of n invocations of this expectation, defaults to 1
func (*Expectation) Min ¶
func (e *Expectation) Min(expect int) *Expectation
Min expects a minimum of n invocations of this expectation
func (*Expectation) NotCalled ¶
func (e *Expectation) NotCalled() *Expectation
NotCalled is a shortcut for Exactly(0)
func (*Expectation) Once ¶
func (e *Expectation) Once() *Expectation
Once is a shortcut for Exactly(1)
func (*Expectation) Optionally ¶
func (e *Expectation) Optionally() *Expectation
Optionally is a shortcut for Min(0)
func (*Expectation) String ¶
func (e *Expectation) String() string
func (*Expectation) WithAnyArguments ¶
func (e *Expectation) WithAnyArguments() *Expectation
WithAnyArguments causes the expectation to accept any arguments via a MatcherFunc
func (*Expectation) WithMatcherFunc ¶
func (e *Expectation) WithMatcherFunc(f func(arg ...string) ArgumentsMatchResult) *Expectation
WithMatcherFunc provides a custom matcher for argument sets, for instance matching variable amounts of arguments
func (*Expectation) WithStdin ¶
func (e *Expectation) WithStdin(match interface{}) *Expectation
WithStdin sets an expectation on the stdin received by the command.
type ExpectationResult ¶
type ExpectationResult struct { Arguments []string Expectation *Expectation ArgumentsMatchResult ArgumentsMatchResult CallCountMatch bool }
ExpectationResult is the result of a set of Arguments applied to an Expectation
func (ExpectationResult) Explain ¶
func (r ExpectationResult) Explain() string
Explain returns an explanation of why the Expectation didn't match
type ExpectationResultSet ¶
type ExpectationResultSet []ExpectationResult
ExpectationResultSet is a collection of ExpectationResult
func (ExpectationResultSet) ClosestMatch ¶
func (r ExpectationResultSet) ClosestMatch() ExpectationResult
ClosestMatch returns the ExpectationResult that was the closest match (if not the exact) This is used for suggesting what the user might have meant
func (ExpectationResultSet) Match ¶
func (r ExpectationResultSet) Match() (*Expectation, error)
Match returns the first Expectation that matches exactly, or ErrNoExpectationsMatch if none match.
type ExpectationSet ¶
type ExpectationSet []*Expectation
ExpectationSet is a set of expectations
func (ExpectationSet) ForArguments ¶
func (exp ExpectationSet) ForArguments(args ...string) (result ExpectationResultSet)
ForArguments applies arguments to the expectations and returns the results
type Invocation ¶
type Invocation struct { Args []string Env []string Dir string Expectation *Expectation }
Invocation is a call to the binary
type Matcher ¶
func MatchPattern ¶
type MatcherFunc ¶
type MatcherFunc struct {
// contains filtered or unexported fields
}
func (MatcherFunc) String ¶
func (mf MatcherFunc) String() string
type Mock ¶
type Mock struct { sync.Mutex // Name of the binary Name string // Path to the bintest binary Path string // contains filtered or unexported fields }
Mock provides a wrapper around a Proxy for testing
func NewMockFromTestMain ¶
func (*Mock) Before ¶
func (m *Mock) Before(f func(i Invocation) error) *Mock
Before adds a middleware that is run before the Invocation is dispatched
func (*Mock) Check ¶
Check that all assertions are met and that there aren't invocations that don't match expectations
func (*Mock) CheckAndClose ¶
func (*Mock) Expect ¶
func (m *Mock) Expect(args ...interface{}) *Expectation
Expect creates an expectation that the mock will be called with the provided args
func (*Mock) ExpectAll ¶
func (m *Mock) ExpectAll(argSlices [][]interface{})
ExpectAll is a shortcut for adding lots of expectations
func (*Mock) IgnoreUnexpectedInvocations ¶
IgnoreUnexpectedInvocations allows for invocations without matching call expectations to just silently return 0 and no output
func (*Mock) PassthroughToLocalCommand ¶
PassthroughToLocalCommand executes the mock name as a local command (looked up in PATH) and then passes the result as the result of the mock. Useful for assertions that commands happen, but where you want the command to actually be executed.
type Proxy ¶
type Proxy struct { // Ch is the channel of calls Ch chan *Call // Path is the full path to the compiled binproxy file Path string // The server that the proxy uses to communicate with the binary Server *Server // A count of how many calls have been made CallCount int64 // contains filtered or unexported fields }
Proxy provides a way to programatically respond to invocations of a binary
func CompileProxy ¶
CompileProxy generates a mock binary at the provided path. If just a filename is provided a temp directory is created.
Example ¶
// create a proxy for the git command that echos some debug p, err := bintest.CompileProxy("git") if err != nil { log.Fatal(err) } // call the proxy like a normal binary in the background cmd := exec.Command(p.Path, "rev-parse") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr // windows needs all the environment variables cmd.Env = append(os.Environ(), `MY_MESSAGE=Llama party! 🎉`) if err := cmd.Start(); err != nil { _ = p.Close() log.Fatal(err) } // handle invocations of the proxy binary call := <-p.Ch fmt.Fprintln(call.Stdout, call.GetEnv(`MY_MESSAGE`)) call.Exit(0) // wait for the command to finish _ = cmd.Wait() if err := p.Close(); err != nil { log.Fatal(err) }
Output: Llama party! 🎉
func LinkTestBinaryAsProxy ¶
LinkTestBinaryAsProxy uses the current binary as a Proxy rather than compiling one directly This speeds things up considerably, but requires some code to be injected in TestMain
Example ¶
// create a proxy for the git command that echos some debug p, err := bintest.LinkTestBinaryAsProxy("git") if err != nil { log.Fatal(err) } // call the proxy like a normal binary in the background cmd := exec.Command(p.Path, "rev-parse") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr // windows needs all the environment variables cmd.Env = os.Environ() cmd.Env = append(cmd.Env, p.Environ()...) cmd.Env = append(cmd.Env, `MY_MESSAGE=Llama party! 🎉`) if err := cmd.Start(); err != nil { log.Fatal(err) } // handle invocations of the proxy binary call := <-p.Ch fmt.Fprintln(call.Stdout, call.GetEnv(`MY_MESSAGE`)) call.Exit(0) // wait for the command to finish if err := cmd.Wait(); err != nil { log.Fatal(err) } // cleanup the proxy if err := p.Close(); err != nil { log.Fatal(err) }
Output: Llama party! 🎉
type Server ¶
func StartServer ¶
StartServer starts an instance of a proxy server