testprocess

package
v1.13.8 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dispatch

func Dispatch()

Dispatch can be used to launch multiple subprocesses as part of a go test. If you want to throw up in your mouth a little, then read the implementation. Don't worry, this is apparently a "blessed" hack for doing this sort of thing.

You always want to call testprocess.Dispatch() at the beginning of your TestMain function since confusing things will happen if you call it later on or have complex logic surrounding calls to it. For each subprocess you want, use testprocess.Make to create an *exec.Cmd variable and save it in a global (it must be global for this to work). The resulting commands that are returned can be started/stopped at any point later in your test, e.g.:

func TestMain(m *testing.M) {
    testprocess.Dispatch()
    os.Exit(m.Run())
}

var fooCmd = testprocess.Make(func() { doFoo(); })
var barCmd = testprocess.Make(func() { doBar(); })
var bazcmd = testprocess.Make(func() { doBaz(); })

func TestSomething(t *testing.T) {
    ...
    err := fooCmd.Run()
    ...
}

It is permissible, but not required, to call flag.Parse before calling testprocess.Dispatch. If flag.Parse has not been called, then testprocess.Dispatch will call it.

func Make

func Make(f func()) *exec.Cmd

Make returns an *exec.Cmd that will execute the supplied function in a subprocess. For this to work, testprocess.Dispatch must be invoked by the TestMain of any test suite using this, and the call to Make must be *before* the call to testprocess.Dispatch; possibly from a global variable initializer, e.g.:

var myCmd = testprocess.Make(func() { doSomething(); })

func MakeSudo

func MakeSudo(f func()) *exec.Cmd

MakeSudo does the same thing as testprocess.Make with exactly the same limitations, except the subprocess will run as root.

Types

This section is empty.

Jump to

Keyboard shortcuts

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