-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_test.go
104 lines (100 loc) · 1.92 KB
/
cmd_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bytes"
"flag"
"os"
"strings"
"testing"
)
func TestFlags(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
tests := []struct {
cmd string
stdin string
args []string
wantExit int
wantOutput []string
}{
{
"gen",
"",
[]string{"-h"},
0,
[]string{"Usage:"},
},
{
"gen",
"",
[]string{},
1,
[]string{"Usage:"},
},
{
"gen",
"",
[]string{"-s", "ten names of flowers"},
1,
[]string{"Usage:"},
},
{
"gen",
"",
[]string{"-c"},
1,
[]string{"Usage:"},
},
{
"gen",
"you speak like a Valley girl",
[]string{"-f", "-", "-s"},
1,
[]string{"Usage:"},
},
{
"gen",
"",
[]string{"ten names of flowers"},
0,
[]string{"rose", "lily", "tulip"},
},
{
"gen",
"you understand english but always reply in french",
[]string{"-s", "-f", "-", "ten names of flowers"},
0,
[]string{"rose", "tulipe", "marguerite"},
},
{
"gen",
"you understand english but always reply in french",
[]string{"ten names of flowers"},
1,
[]string{"Usage:"},
},
{
"gen",
"",
[]string{"-p", "DSN=postgres://steampipe:[email protected]:9193/steampipe", "list AWS services"},
0,
[]string{"connection refused"},
},
}
for _, test := range tests {
flag.CommandLine = flag.NewFlagSet(test.cmd, flag.ExitOnError)
os.Args = append([]string{test.cmd}, test.args...)
t.Log(os.Args)
var out bytes.Buffer
actualExit := emitGen(strings.NewReader(test.stdin), &out)
if test.wantExit != actualExit {
t.Errorf("Wrong exit code for args: %v, expected: %v, got: %v",
test.args, test.wantExit, actualExit)
continue
}
actualOutput := out.String()
if !anyMatches([]string{actualOutput}, test.wantOutput...) {
t.Errorf("Wrong output for args: %v, expected one of: %v, got: %v",
test.args, strings.Join(test.wantOutput, ","), actualOutput)
}
}
}