Documentation
¶
Overview ¶
Package icmd executes binaries and provides convenient assertions for testing the results.
Index ¶
Examples ¶
Constants ¶
const None = "[NOTHING]"
None is a token to inform Result.Assert that the output should be empty
Variables ¶
var Success = Expected{}
Success is the default expected result. A Success result is one with a 0 ExitCode.
Functions ¶
This section is empty.
Types ¶
type Cmd ¶
type Cmd struct {
Command []string
Timeout time.Duration
Stdin io.Reader
Stdout io.Writer
Dir string
Env []string
ExtraFiles []*os.File
}
Cmd contains the arguments and options for a process to run as part of a test suite.
type CmdOp ¶
type CmdOp func(*Cmd)
CmdOp is an operation which modified a Cmd structure used to execute commands
func WithEnv ¶
WithEnv sets the environment variable of the command. Each arguments are in the form of KEY=VALUE
func WithExtraFile ¶
WithExtraFile adds a file descriptor to the command
func WithTimeout ¶
WithTimeout sets the timeout duration of the command
type Expected ¶
Expected is the expected output from a Command. This struct is compared to a Result struct by Result.Assert().
type Result ¶
type Result struct {
Cmd *exec.Cmd
ExitCode int
Error error
// Timeout is true if the command was killed because it ran for too long
Timeout bool
// contains filtered or unexported fields
}
Result stores the result of running a command
func RunCmd ¶
RunCmd runs a command and returns a Result
Example ¶
package main
import (
"testing"
"github.com/DominicLavery/gotest.tools/v3/icmd"
)
var t = &testing.T{}
func main() {
result := icmd.RunCmd(icmd.Command("cat", "/does/not/exist"))
result.Assert(t, icmd.Expected{
ExitCode: 1,
Err: "cat: /does/not/exist: No such file or directory",
})
}
func RunCommand ¶
RunCommand runs a command with default options, and returns a result
Example ¶
package main
import (
"testing"
"github.com/DominicLavery/gotest.tools/v3/icmd"
)
var t = &testing.T{}
func main() {
result := icmd.RunCommand("bash", "-c", "echo all good")
result.Assert(t, icmd.Success)
}
func WaitOnCmd ¶
WaitOnCmd waits for a command to complete. If timeout is non-nil then only wait until the timeout.
func (*Result) Assert ¶
Assert compares the Result against the Expected struct, and fails the test if any of the expectations are not met.
This function is equivalent to assert.Assert(t, result.Equal(exp)).
func (*Result) Equal ¶
func (r *Result) Equal(exp Expected) cmp.Comparison
Equal compares the result to Expected. If the result doesn't match expected returns a formatted failure message with the command, stdout, stderr, exit code, and any failed expectations.