Documentation ¶
Overview ¶
Package commandmocker is a simple utility for tests in Go. It adds command with expected output/failure to the path.
Index ¶
- func Add(name, output string) (string, error)
- func AddStderr(name, stdout, stderr string) (string, error)
- func Envs(tempdir string) string
- func Error(name, output string, status int) (string, error)
- func Output(tempdir string) string
- func Parameters(tempdir string) []string
- func Ran(tempdir string) bool
- func Remove(tempdir string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
Add creates a temporary directory containing an executable file named "name" that prints "output" when executed. It also adds the temporary directory to the first position of $PATH.
It returns the temporary directory path (for future removing, using the Remove function) and an error if any happen.
Example ¶
This example demonstrates the mocking of the SSH command.
package main import ( "fmt" "github.com/tsuru/commandmocker" "os/exec" ) func main() { msg := "HELP!" path, err := commandmocker.Error("ssh", msg, 1) if err != nil { panic(err) } defer commandmocker.Remove(path) cmd := exec.Command("ssh", "-l", "root", "127.0.0.1") out, err := cmd.CombinedOutput() fmt.Println(err) fmt.Printf("%s\n", out) fmt.Println(commandmocker.Parameters(path)) fmt.Println(commandmocker.Ran(path)) }
Output: exit status 1 HELP! [-l root 127.0.0.1] true
func AddStderr ¶
AddStderr works like Add, but it allow callers to specify the output for both the stdout and stderr streams.
func Envs ¶
Envs returns the environment variables available to the previously added command execution.
func Error ¶
Error works like Add, but the created executable returns a non-zero status code (an error). The returned status code will be the value provided by status.
func Parameters ¶
Parameters returns a slice containing all positional parameters given to the command mocked in tempdir in its last execution.
Types ¶
This section is empty.