fakeexec

package
v0.0.0-...-b5d9cbe Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: BSD-3-Clause Imports: 12 Imported by: 0

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

func NewAuxMain(name string, f interface{}) *AuxMain

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

func CreateLoopback(path string, proc ProcFunc) (lo *Loopback, retErr error)

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.

func (*Loopback) Close

func (s *Loopback) Close() error

Close removes the loopback executable file and releases its associated resources.

type ProcFunc

type ProcFunc func(args []string, stdin io.Reader, stdout, stderr io.WriteCloser) int

ProcFunc is a callback passed to CreateLoopback to fully control the behavior of a loopback process.

func (ProcFunc) Exec

Exec implements LoopbackExecService.

Jump to

Keyboard shortcuts

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