package module
Version: v0.0.0-...-e1d28f4 Latest Latest

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

Go to latest
Published: Sep 9, 2016 License: BSD-3-Clause Imports: 12 Imported by: 0



Build Status

commandmocker is a simple utility for tests in Go. It adds command with expected output to the path.

For example, if you want to mock the command "ssh", you can write a test that looks like this:

import (

func TestScreamsIfSSHFail(t *testing.T) {
    message := "ssh: Could not resolve hostname myhost: nodename nor servname provided, or not known"
    path, err := commandmocker.Error("ssh", message, 65)
    if err != nil {
    defer commandmocker.Remove(path)

    // write your test and expectations



Package commandmocker is a simple utility for tests in Go. It adds command with expected output/failure to the path.




This section is empty.


This section is empty.


func Add

func Add(name, output string) (string, error)

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.


This example demonstrates the mocking of the SSH command.

package main

import (


func main() {
	msg := "HELP!"
	path, err := commandmocker.Error("ssh", msg, 1)
	if err != nil {
	defer commandmocker.Remove(path)
	cmd := exec.Command("ssh", "-l", "root", "")
	out, err := cmd.CombinedOutput()
	fmt.Printf("%s\n", out)

exit status 1
[-l root]

func AddStderr

func AddStderr(name, stdout, stderr string) (string, error)

AddStderr works like Add, but it allow callers to specify the output for both the stdout and stderr streams.

func Envs

func Envs(tempdir string) string

Envs returns the environment variables available to the previously added command execution.

func Error

func Error(name, output string, status int) (string, 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 Output

func Output(tempdir string) string

Output returns the output generated by the previously added command execution.

func Parameters

func Parameters(tempdir string) []string

Parameters returns a slice containing all positional parameters given to the command mocked in tempdir in its last execution.

func Ran

func Ran(tempdir string) bool

Ran indicates whether the mocked executable was called or not.

It just checks if the given tempdir contains a .ran file.

func Remove

func Remove(tempdir string) error

Remove removes the tempdir from $PATH and from file system.

This function is intended only to undo what Add does. It returns error if the given tempdir is not a temporary directory.


This section is empty.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL