executil

package
v0.0.0-...-f6cb899 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package executil provides a mostly transparent way to make os/exec testable. It is inspired by https://npf.io/2015/06/testing-exec-command/ (which was inspired by the standard library's tests of os/exec). Basically, the helpers in this package replace a call to an arbitrary executable (and arguments) with a call to the underlying test binary, with a flag to run exactly one test. This test can then be a fake implementation of the binary, do assertions on the arguments, etc.

See executil_test.go for example usages.

Index

Constants

View Source
const (
	// OverrideEnvironmentVariable is the environment variable that will be set if a test has been
	// invoked via CommandContext below and it should behave as if it is faking a call to an
	// executable. The value it is set to should be considered arbitrary and not relied upon.
	OverrideEnvironmentVariable = "SKIA_INFRA_OVERRIDE_TEST"
)

Variables

This section is empty.

Functions

func CommandContext

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

CommandContext looks for a special value on the provided context.Context (see WithFakeTests). If that value exists, it will use the next fake test value and return a faked *exec.Cmd. It panics if there are not enough fake tests that were provided to the original context. If the special value does not exist, it is a passthrough to os/exec.CommandContext.

func FakeCommandsReturned

func FakeCommandsReturned(ctx context.Context) int

FakeCommandsReturned returns the count of how many times CommandContext was called using the given context. This is a proxy for the number of fake commands run.

func FakeTestsContext

func FakeTestsContext(fakeTestNames ...string) context.Context

FakeTestsContext is a convenient wrapper around WithFakeTests using context.Background().

func IsCallingFakeCommand

func IsCallingFakeCommand() bool

IsCallingFakeCommand returns whether the current process is a test process that's running a mocked-out CLI invocation. This should be called at the beginning of each Test_FakeExe_... test and trigger an early return if false.

func OriginalArgs

func OriginalArgs() []string

OriginalArgs returns the original arguments passed into a test function. Concretely, it looks at the osArgs and strips off the first 3 (the test binary, the test to run, and "--")

func WithFakeTests

func WithFakeTests(parent context.Context, fakeTestNames ...string) context.Context

WithFakeTests returns a context.Context loaded with a special Value containing the given test names. When this Context is passed into this package's CommandContext, faked *exec.Cmd objects will be returned using the given test names. The first call to CommandContext will be faked with the first value of fakeTestNames, the second call to CommandContext will use the second value of fakeTestNames and so on. This panics if the provided context was one that already has fake tests associated with it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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