github.com/didip/deis@v1.4.1/deisctl/deisctl_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/deis/deis/version"
    11  )
    12  
    13  // commandOutput returns stdout for a deisctl command line as a string.
    14  func commandOutput(args []string) (output string) {
    15  	old := os.Stdout
    16  	r, w, _ := os.Pipe()
    17  	os.Stdout = w
    18  
    19  	Command(args)
    20  
    21  	outC := make(chan string)
    22  	go func() {
    23  		var buf bytes.Buffer
    24  		io.Copy(&buf, r)
    25  		outC <- buf.String()
    26  	}()
    27  
    28  	w.Close()
    29  	os.Stdout = old
    30  	output = <-outC
    31  	return
    32  }
    33  
    34  // TestHelp tests that deisctl is flexible when being asked to print built-in help.
    35  func TestHelp(t *testing.T) {
    36  	allArgs := [][]string{{"-h"}, {"--help"}, {"help"}}
    37  	out := ""
    38  	for _, args := range allArgs {
    39  		out = commandOutput(args)
    40  		if !strings.Contains(out, "Usage: deisctl [options] <command> [<args>...]") ||
    41  			!strings.Contains(out, "Commands, use \"deisctl help <command>\" to learn more") {
    42  			t.Error(out)
    43  		}
    44  	}
    45  }
    46  
    47  // TestUsage ensures that deisctl prints a short usage string when no arguments were provided.
    48  func TestUsage(t *testing.T) {
    49  	out := commandOutput(nil)
    50  	if out != "Usage: deisctl [options] <command> [<args>...]\n" {
    51  		t.Error(out)
    52  	}
    53  }
    54  
    55  // TestVersion verifies that "deisctl --version" prints the current version string.
    56  func TestVersion(t *testing.T) {
    57  	args := []string{"--version"}
    58  	out := commandOutput(args)
    59  	if !strings.HasPrefix(out, version.Version) {
    60  		t.Error(out)
    61  	}
    62  }