forked from deis/deis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(deisctl): add example unit tests for help and --version
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// commandOutput returns stdout for a deisctl command line as a string. | ||
func commandOutput(args []string) (output string) { | ||
old := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
Command(args) | ||
|
||
outC := make(chan string) | ||
go func() { | ||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
outC <- buf.String() | ||
}() | ||
|
||
w.Close() | ||
os.Stdout = old | ||
output = <-outC | ||
return | ||
} | ||
|
||
// TestHelp tests that deisctl is flexible when being asked to print built-in help. | ||
func TestHelp(t *testing.T) { | ||
allArgs := [][]string{{"-h"}, {"--help"}, {"help"}} | ||
out := "" | ||
for _, args := range allArgs { | ||
out = commandOutput(args) | ||
if !strings.Contains(out, "Usage: deisctl <command> [<args>...] [options]") || | ||
!strings.Contains(out, "Commands, use \"deisctl help <command>\" to learn more") { | ||
t.Error(out) | ||
} | ||
} | ||
} | ||
|
||
// TestUsage ensures that deisctl prints a short usage string when no arguments were provided. | ||
func TestUsage(t *testing.T) { | ||
out := commandOutput(nil) | ||
if out != "Usage: deisctl <command> [<args>...] [options]\n" { | ||
t.Error(out) | ||
} | ||
} | ||
|
||
// TestVersion verifies that "deisctl --version" prints the current version string. | ||
func TestVersion(t *testing.T) { | ||
args := []string{"--version"} | ||
out := commandOutput(args) | ||
if !strings.HasPrefix(out, Version) { | ||
t.Error(out) | ||
} | ||
} |