testcli

package module
v0.0.0-...-6283090 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2016 License: MIT Imports: 8 Imported by: 0

README

testcli

CLI testing package for the Go language.

Developing a command line application? Wanna be able to test your app from the outside? If the answer is Yes to at least one of the questions, keep reading.

When using Ruby I use aruba for testing command line applications, in Go I still can use aruba, but it"s awkward to bring Ruby and it's artillery only to test my app.

testcli is a wrapper around os.exec to test CLI apps in Go lang, minimalistic, so you can do your tests with testing or any other testing framework.

Greetings app

main_test.go

// make sure to execute `go install` before tests
package main

import (
	"testing"

	"github.com/rendon/testcli"
)

func TestGreetings(t *testing.T) {
	// Using package functions
	testcli.Run("greetings")
	if !testcli.Success() {
		t.Fatalf("Expected to succeed, but failed: %s", testcli.Error())
	}

	if !testcli.StdoutContains("Hello?") {
		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "Hello?")
	}
}

func TestGreetingsWithName(t *testing.T) {
	// Using the struct version, if you want to test multiple commands
	c := testcli.Command("greetings", "--name", "John")
	c.Run()
	if !c.Success() {
		t.Fatalf("Expected to succeed, but failed with error: %s", c.Error())
	}

	if !c.StdoutContains("Hello John!") {
		t.Fatalf("Expected %q to contain %q", c.Stdout(), "Hello John!")
	}
}

main.go

package main

import (
	"fmt"
	"os"

	"github.com/codegangsta/cli"
)

func main() {
	app := cli.NewApp()
	app.Name = "cli"
	app.Usage = "CLI app"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:  "name",
			Usage: "User name",
		},
	}
	app.Action = func(c *cli.Context) {
		if c.String("name") != "" {
			fmt.Printf("Hello %s!\n", c.String("name"))
		} else {
			fmt.Printf("Hello? Anyone?\n")
		}
	}

	app.Run(os.Args)
}

Documentation

Overview

CLI testing package for the Go language.

Developing a command line application? Wanna be able to test your app from the outside? If the answer is Yes to at least one of the questions, keep reading.

`testcli` is a wrapper around os/exec to test CLI apps in Go lang, minimalistic, so you can do your tests with `testing` or any other testing framework.

Index

Constants

This section is empty.

Variables

View Source
var ErrUninitializedCmd = errors.New("You need to run this command first")

ErrUninitializedCmd is returned when members are accessed before a run, that can only be used after a command has been run.

Functions

func Error

func Error() error

Error is the command's error, if any.

func Failure

func Failure() bool

Failure is the inverse of Success().

func Run

func Run(name string, arg ...string)

Run runs a command with name and arguments. After this, package-level functions will return the data about the last command run.

func Stderr

func Stderr() string

Stderr stream for the command

func StderrContains

func StderrContains(str string) bool

StderrContains determines if command's STDERR contains `str`, this operation is case insensitive.

func StderrMatches

func StderrMatches(regex string) bool

StderrMatches compares a regex to the stderr produced by the command.

func Stdout

func Stdout() string

Stdout stream for the command

func StdoutContains

func StdoutContains(str string) bool

StdoutContains determines if command's STDOUT contains `str`, this operation is case insensitive.

func StdoutMatches

func StdoutMatches(regex string) bool

StdoutMatches compares a regex to the stdout produced by the command.

func Success

func Success() bool

Success is a boolean status which indicates if the program exited non-zero or not.

Types

type Cmd

type Cmd struct {
	// contains filtered or unexported fields
}

Cmd is typically constructed through the Command() call and provides state to the execution engine.

func Command

func Command(name string, arg ...string) *Cmd

Command constructs a *Cmd. It is passed the command name and arguments.

func (*Cmd) Error

func (c *Cmd) Error() error

Error is the command's error, if any.

func (*Cmd) Failure

func (c *Cmd) Failure() bool

Failure is the inverse of Success().

func (*Cmd) Run

func (c *Cmd) Run()

Run runs the command.

func (*Cmd) SetEnv

func (c *Cmd) SetEnv(env []string)

SetEnv overwrites the environment with the provided one. Otherwise, the parent environment will be supplied.

func (*Cmd) SetStdin

func (c *Cmd) SetStdin(stdin io.Reader)

SetStdin sets the stdin stream. It makes no attempt to determine if the command accepts anything over stdin.

func (*Cmd) Stderr

func (c *Cmd) Stderr() string

Stderr stream for the command

func (*Cmd) StderrContains

func (c *Cmd) StderrContains(str string) bool

StderrContains determines if command's STDERR contains `str`, this operation is case insensitive.

func (*Cmd) StderrMatches

func (c *Cmd) StderrMatches(regex string) bool

StderrMatches compares a regex to the stderr produced by the command.

func (*Cmd) Stdout

func (c *Cmd) Stdout() string

Stdout stream for the command

func (*Cmd) StdoutContains

func (c *Cmd) StdoutContains(str string) bool

StdoutContains determines if command's STDOUT contains `str`, this operation is case insensitive.

func (*Cmd) StdoutMatches

func (c *Cmd) StdoutMatches(regex string) bool

StdoutMatches compares a regex to the stdout produced by the command.

func (*Cmd) Success

func (c *Cmd) Success() bool

Success is a boolean status which indicates if the program exited non-zero or not.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL