github.com/chasestarr/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/deisctl/deisctl_test.go (about)

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