Categorygithub.com/jaredallard/cmdexec
modulepackage
1.2.1
Repository: https://github.com/jaredallard/cmdexec.git
Documentation: pkg.go.dev

# README

cmdexec

Latest Version License GitHub Workflow Status Codecov

Go library for mocking exec.Command and exec.CommandContext calls. Aims to provide an as close as possible [exec.Cmd]-like interface for drop-in support.

Differences

  • Instead of setting cmd.Stdin (or in/err), use SetStdin() functions instead. This is due to those being fields on a struct, which cannot be on an interface.

Usage

See our Go docs.

Asserting Output

Normally, you shouldn't need to assert anything as your function that executes a command should give you testing signal (is it working or not :wink:). However, you can assert certain fields with this library. Currently, this is limited to Stdin checking.

If you set [MockCommand.Stdin] and call SetStdin in the function executing a command, Stdin will be checked to ensure it is equal. This is to allow greater testing if required.

License

LGPL-3.0

# Functions

Command returns a new Cmd that will call the given command with the given arguments.
CommandContext returns a new Cmd that will call the given command with the given arguments and the given context.
NewMockExecutor returns a new MockExecutor with the given commands.
UseMockExecutor replaces the executor used by cmdexec with a mock executor that can be used to control the output of all commands created after this function is called.

# Structs

MockCommand is a command that can be executed by the MockExecutor.
MockExecutor provides an executor that returns mock data.

# Interfaces

Cmd is an interface to be used instead of [exec.Cmd] for mocking purposes.