-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandle_args_test.go
213 lines (188 loc) · 4.7 KB
/
handle_args_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package donothing
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
// DefaultCLI.Usage should return the usage string.
func TestDefaultCLI_Usage(t *testing.T) {
t.Parallel()
assert := assert.New(t)
pcd := NewProcedure()
pcd.Short("Procedure's short description")
type testCase struct {
DefaultStep string
Exp string
}
testCases := []testCase{
// With default step specified
testCase{
DefaultStep: "root/stepName",
Exp: `USAGE: foo [options] [STEP_NAME]
Procedure's short description
OPTIONS:
--markdown Instead of executing the procedure, print its Markdown documentation to stdout
--help Print usage message`,
},
// Without default step
testCase{
DefaultStep: "",
Exp: `USAGE: foo [options] STEP_NAME
Procedure's short description
OPTIONS:
--markdown Instead of executing the procedure, print its Markdown documentation to stdout
--help Print usage message`,
},
}
for i, tc := range testCases {
t.Logf("test case %d", i)
cli, err := NewDefaultCLI("foo", pcd, tc.DefaultStep)
assert.Nil(err)
assert.Equal(tc.Exp, cli.Usage())
}
}
// DefaultCLI should print usage when --help is passed or the args are wrong.
func TestDefaultCLI_PrintUsage(t *testing.T) {
t.Parallel()
assert := assert.New(t)
pcd := NewProcedure()
pcd.Short("Procedure's short description")
type testCase struct {
// os.Args
Args []string
// The CLI's default step
DefaultStep string
// Whether an error is expected
ErrorExp bool
}
testCases := []testCase{
testCase{
Args: []string{"foo", "--help"},
ErrorExp: false,
},
testCase{
Args: []string{"foo", "-h"},
ErrorExp: false,
},
testCase{
Args: []string{"foo"},
DefaultStep: "",
ErrorExp: true,
},
testCase{
Args: []string{"foo", "--markdown"},
DefaultStep: "",
ErrorExp: true,
},
testCase{
Args: []string{"foo", "--nonexistent-flag"},
ErrorExp: true,
},
testCase{
Args: []string{"foo", "too", "many", "args"},
ErrorExp: true,
},
}
for i, tc := range testCases {
t.Logf("test case %d", i)
cli, err := NewDefaultCLI("foo", pcd, tc.DefaultStep)
assert.Nil(err)
var buf bytes.Buffer
cli.out = &buf
err = cli.Run(tc.Args)
if tc.ErrorExp != (err != nil) {
if err != nil {
t.Logf("cli.Run returned unexpected error '%s'", err.Error())
t.Fail()
} else {
t.Logf("cli.Run should have returned an error but didn't")
}
}
assert.Contains(buf.String(), "USAGE:")
}
}
// DefaultCLI should render a step when --markdown is passed
func TestDefaultCLI_Render(t *testing.T) {
t.Parallel()
assert := assert.New(t)
pcd := NewProcedure()
pcd.Short("Procedure's short description")
pcd.AddStep(func(step *Step) {
step.Name("blahBlah")
step.Short("the blahBlah step")
})
type testCase struct {
// os.Args
Args []string
// The CLI's default step
DefaultStep string
// A function that makes assertions about the output of cli.Run
Match func(s string)
// Whether an error is expected from cli.Run
ErrorExp bool
}
testCases := []testCase{
testCase{
Args: []string{"foo", "--markdown"},
DefaultStep: "root",
Match: func(s string) {
assert.Contains(s, "Procedure's short description")
assert.Contains(s, "the blahBlah step")
},
ErrorExp: false,
},
testCase{
Args: []string{"foo", "--markdown"},
DefaultStep: "",
Match: func(s string) {},
ErrorExp: true,
},
testCase{
Args: []string{"foo", "--markdown", "root.blahBlah"},
DefaultStep: "",
Match: func(s string) {
assert.NotContains(s, "Procedure's short description")
assert.Contains(s, "the blahBlah step")
},
ErrorExp: false,
},
testCase{
Args: []string{"foo", "--markdown"},
DefaultStep: "root.blahBlah",
Match: func(s string) {
assert.NotContains(s, "Procedure's short description")
assert.Contains(s, "the blahBlah step")
},
ErrorExp: false,
},
testCase{
Args: []string{"foo", "--markdown", "nonexistentStep"},
DefaultStep: "",
Match: func(s string) {},
ErrorExp: true,
},
testCase{
Args: []string{"foo", "--markdown"},
DefaultStep: "nonexistentStep",
Match: func(s string) {},
ErrorExp: true,
},
}
for i, tc := range testCases {
t.Logf("test case %d", i)
cli, err := NewDefaultCLI("foo", pcd, tc.DefaultStep)
assert.Nil(err)
var buf bytes.Buffer
cli.out = &buf
err = cli.Run(tc.Args)
if tc.ErrorExp != (err != nil) {
if err != nil {
t.Logf("cli.Run returned unexpected error '%s'", err.Error())
t.FailNow()
} else {
t.Logf("cli.Run should have returned an error but didn't")
}
}
tc.Match(buf.String())
}
}