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
- func CommandContext(ctx context.Context, cmd string, args ...string) *exec.Cmd
- func FakeCommandsReturned(ctx context.Context) int
- func FakeTestsContext(fakeTestNames ...string) context.Context
- func IsCallingFakeCommand() bool
- func OriginalArgs() []string
- func WithFakeTests(parent context.Context, fakeTestNames ...string) context.Context
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.