-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.go
67 lines (56 loc) · 1.48 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cli
import (
"bytes"
"os/exec"
"regexp"
"testing"
)
func ExpectSuccess(t *testing.T, err error) {
if err != nil {
_, ok := err.(*exec.ExitError)
if !ok {
t.Fatalf("Command failed to execute")
}
t.Errorf("Expected zero exit status; received %s", err)
}
}
func ExpectError(t *testing.T, err error) {
if err == nil {
t.Error("Expected nonzero exit status")
}
}
func ExpectOutput(t *testing.T, stdout bytes.Buffer) {
if len(stdout.Bytes()) < 1 {
t.Errorf("Expected command to write output to STDOUT")
}
}
func ExpectMatch(t *testing.T, stdout bytes.Buffer, pattern string) {
matched, err := regexp.Match(pattern, stdout.Bytes())
if err != nil {
t.Fatalf("Unable to parse, bad regular expression: %s", pattern)
}
if !matched {
t.Errorf("Output does not match pattern\nPattern: %s\nOutput:\n%s",
pattern, stdout.String())
}
}
func ExpectErrorOutput(t *testing.T, stderr bytes.Buffer, err error) {
if len(stderr.Bytes()) < 1 {
t.Errorf("Expected log output on STDERR with nonzero exit status")
}
}
func ExpectHelp(t *testing.T, stderr bytes.Buffer, cmd Command) {
var subcommands CLI
if b, ok := (interface{})(cmd).(HasSubcommands); ok {
subcommands = b.Subcommands()
}
for subcommand, _ := range subcommands {
matched, err := regexp.Match(subcommand, stderr.Bytes())
if err != nil {
t.Fatalf("Unable to parse, bad regular expression: %s", subcommand)
}
if !matched {
t.Errorf("Help text doesn't include subcommand: %s", subcommand)
}
}
}