exectest

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package exectest provides a means of mocking [os/exec.Cmd]s allowing injection of arbitrary behavior into an external executable from a test.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Actor

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

Actor defines a fake behavior (or act) for an external executable. Build these with Act.

func Act

func Act(t testing.TB, main func()) *Actor

Act builds an actor that acts as specified in the given function.

Because of how this operates:

  • call this near the top of a test function
  • avoid non-determistic code before this function is called
  • do not call this inside subtests

func (*Actor) CommandContext added in v0.5.0

func (c *Actor) CommandContext(ctx context.Context, args ...string) *exec.Cmd

CommandContext builds an exec.Cmd that will run this Actor as an external executable with the provided arguments.

This operates by re-running the test executable to run only the current test, and hijacking that test execution to run the main function.

actor := exectest.Act(t, func() { fmt.Println("hello") })
cmd := actor.CommandContext(ctx, args)
got, err := cmd.Output()
...
fmt.Println(string(got) == "hello\n") // true

Jump to

Keyboard shortcuts

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