Documentation ¶
Overview ¶
Package fakeexec provides utilities to test external program execution.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuxMain ¶
type AuxMain struct {
// contains filtered or unexported fields
}
AuxMain represents a auxiliary main function.
func NewAuxMain ¶
NewAuxMain registers a new auxiliary main function.
name identifies an auxiliary main function. It must be unique within the current executable; otherwise this function will panic.
f must be a function having a signature func(T) where T is a JSON serializable type.
NewAuxMain must be called in a top-level variable initialization like:
type fooParams struct { ... } var fooMain = fakeexec.NewAuxMain("foo", func(p fooParams) { // Another main function here... })
If the current process is executed for the auxiliary main, NewAuxMain immediately calls f and exits. Otherwise *AuxMain is returned, which you can use to start a subprocess running the auxiliary main.
p := fooMain.Params(fooParams{ ... }) cmd := exec.Command(p.Name()) cmd.Env = append(os.Environ(), p.Envs()...) if err := cmd.Run(); err != nil { ... }
Prefer Loopback if subprocesses don't need to call system calls. Loopback subprocesses virtually run within the current unit test process, which is usually more convenient than auxiliary main functions that run as separate processes.
func (*AuxMain) Params ¶
func (a *AuxMain) Params(v interface{}) (*AuxMainParams, error)
Params creates AuxMainParams that contains information necessary to execute the auxiliary main function. v should be an arbitrary JSON-serializable value. It is passed to the auxiliary main function.
type AuxMainParams ¶
type AuxMainParams struct {
// contains filtered or unexported fields
}
AuxMainParams contains information necessary to execute an auxiliary main function.
func (*AuxMainParams) Envs ¶
func (a *AuxMainParams) Envs() []string
Envs returns environment variables to be set to execute the auxiliary main function. Elements are in the form of "key=value" so that they can be appended to os/exec.Cmd.Env.
func (*AuxMainParams) Executable ¶
func (a *AuxMainParams) Executable() string
Executable returns a path to the current executable. It is similar to os.Executable, but it is precomputed and never fails.
func (*AuxMainParams) SetEnvs ¶
func (a *AuxMainParams) SetEnvs() (restore func())
SetEnvs modifies the current process' environment variables with os.Setenv so that the auxiliary main function is called on executing the self executable as a subprocess.
It returns a closure to restore the original environment variable. It panics if the environment variable is already set.
type Loopback ¶
type Loopback struct {
// contains filtered or unexported fields
}
Loopback represents a loopback executable file.
func CreateLoopback ¶
CreateLoopback creates a new file called a loopback executable.
When a loopback executable file is executed, its process connects to the current unit test process by gRPC to call proc remotely. The process behaves exactly as specified by proc. Since proc is called within the current unit test process, unit tests and subprocesses can interact easily with Go constructs, e.g. shared memory or channels.
A drawback is that proc can only emulate args, stdio and exit code. If you need to do anything else, e.g. catching signals, use NewAuxMain instead.
Once you're done with a loopback executable, call Loopback.Close to release associated resources.