commandmocker

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

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

README

#commandmocker

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 (
    "github.com/tsuru/commandmocker"
    "testing"
)

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 {
        t.Fatal(err)
    }
    defer commandmocker.Remove(path)

    // write your test and expectations
}

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

Example

This example demonstrates the mocking of the SSH command.

package main

import (
	"fmt"
	"os/exec"

	"github.com/tsuru/commandmocker"
)

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

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.

Types

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