github.com/tmlbl/deis@v1.0.2/deisctl/deisctl_test.go (about)

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