generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
climan_test.go
264 lines (244 loc) · 6.45 KB
/
climan_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package climan_test
import (
"context"
"errors"
"flag"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"moul.io/climan"
"moul.io/u"
)
func TestCliman(t *testing.T) {
cases := []struct {
name string
root *climan.Command
args []string
output string
parseShouldErrFlag bool
parseShouldOtherFail bool
runShouldErrFlag bool
runShouldOtherFail bool
}{
{
name: "empty",
root: &climan.Command{},
args: []string{},
output: `
USAGE
climan.test
`[1:],
runShouldErrFlag: true,
}, {
name: "example-no-args",
root: exampleCommand(),
args: []string{},
output: "debug false\nargs []\n",
}, {
name: "example-flags",
root: exampleCommand(),
args: []string{"-debug"},
output: "debug true\nargs []\n",
}, {
name: "example-args",
root: exampleCommand(),
args: []string{"foo", "bar"},
output: "debug false\nargs [foo bar]\n",
}, {
name: "example-flags-and-args",
root: exampleCommand(),
args: []string{"-debug", "foo", "bar"},
output: "debug true\nargs [foo bar]\n",
}, {
name: "example-help",
root: exampleCommand(),
args: []string{"-h"},
output: `
USAGE
example
FLAGS
-debug false debug mode
`[1:],
parseShouldErrFlag: true,
}, {
name: "example-sub-no-args",
root: exampleCommandWithSubcommands(),
args: []string{},
output: "debug false\nfoo \nbar 0\nargs []\nfoo called false\nbar called false\n",
}, {
name: "example-sub-flags",
root: exampleCommandWithSubcommands(),
args: []string{"-debug"},
output: "debug true\nfoo \nbar 0\nargs []\nfoo called false\nbar called false\n",
}, {
name: "example-sub-args",
root: exampleCommandWithSubcommands(),
args: []string{"blah", "blih"},
output: "debug false\nfoo \nbar 0\nargs [blah blih]\nfoo called false\nbar called false\n",
}, {
name: "example-sub-flags-and-args",
root: exampleCommandWithSubcommands(),
args: []string{"-debug", "blah", "blih"},
output: "debug true\nfoo \nbar 0\nargs [blah blih]\nfoo called false\nbar called false\n",
}, {
name: "example-sub-foo-no-args",
root: exampleCommandWithSubcommands(),
args: []string{"foo"},
output: "debug false\nfoo \nbar 0\nargs []\nfoo called true\nbar called false\n",
}, {
name: "example-sub-foo-pre-flags",
root: exampleCommandWithSubcommands(),
args: []string{"-debug", "foo", "-flag=baz"},
output: "debug true\nfoo baz\nbar 0\nargs []\nfoo called true\nbar called false\n",
}, {
name: "example-sub-foo-post-flags",
root: exampleCommandWithSubcommands(),
args: []string{"foo", "-debug", "-flag=baz"},
output: "debug true\nfoo baz\nbar 0\nargs []\nfoo called true\nbar called false\n",
}, {
name: "example-sub-foo-cancel-flags",
root: exampleCommandWithSubcommands(),
args: []string{"-debug", "foo", "-debug=false"},
output: "debug false\nfoo \nbar 0\nargs []\nfoo called true\nbar called false\n",
}, {
name: "example-sub-foo-args",
root: exampleCommandWithSubcommands(),
args: []string{"foo", "bar", "baz"},
output: "debug false\nfoo \nbar 0\nargs [bar baz]\nfoo called true\nbar called false\n",
}, {
name: "example-sub-foo-args-and-flags",
root: exampleCommandWithSubcommands(),
args: []string{"-debug", "foo", "-debug", "-flag=baz", "bar", "baz"},
output: "debug true\nfoo baz\nbar 0\nargs [bar baz]\nfoo called true\nbar called false\n",
}, {
name: "example-sub-help",
root: exampleCommandWithSubcommands(),
args: []string{"-h"},
output: `
USAGE
example
SUBCOMMANDS
foo dolor sit amet
bar dolor sit amet2
FLAGS
-debug false debug mode
`[1:],
parseShouldErrFlag: true,
}, {
name: "example-foo-help",
root: exampleCommandWithSubcommands(),
args: []string{"-debug", "foo", "-h"},
output: `
USAGE
lorem ipsum
FLAGS
-debug true debug mode
-flag ... foo's flag
`[1:],
parseShouldErrFlag: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
root := tc.root
// parse
{
done, err := u.CaptureStdoutAndStderr()
require.NoError(t, err)
err = root.Parse(tc.args)
out := done()
if tc.parseShouldOtherFail {
require.Error(t, err)
return
}
if tc.parseShouldErrFlag {
require.True(t, errors.Is(err, flag.ErrHelp))
require.Equal(t, out, tc.output)
return
}
require.NoError(t, err)
}
// run
{
done, err := u.CaptureStdoutAndStderr()
require.NoError(t, err)
err = root.Run(context.Background())
out := done()
if tc.runShouldOtherFail {
require.Error(t, err)
return
}
if tc.runShouldErrFlag {
require.True(t, errors.Is(err, flag.ErrHelp))
} else {
require.NoError(t, err)
}
require.Equal(t, out, tc.output)
}
})
}
}
func exampleCommand() *climan.Command {
var opts struct {
Debug bool
}
return &climan.Command{
Name: "example",
FlagSetBuilder: func(fs *flag.FlagSet) {
fs.BoolVar(&opts.Debug, "debug", opts.Debug, "debug mode")
},
Exec: func(ctx context.Context, args []string) error {
fmt.Println("debug", opts.Debug)
fmt.Println("args", args)
return nil
},
}
}
func exampleCommandWithSubcommands() *climan.Command {
var opts struct {
Debug bool
FooFlag string
BarFlag int
FooCalled bool
BarCalled bool
}
exec := func(ctx context.Context, args []string) error {
fmt.Println("debug", opts.Debug)
fmt.Println("foo", opts.FooFlag)
fmt.Println("bar", opts.BarFlag)
fmt.Println("args", args)
fmt.Println("foo called", opts.FooCalled)
fmt.Println("bar called", opts.BarCalled)
return nil
}
commonFlagsSetup := func(fs *flag.FlagSet) {
fs.BoolVar(&opts.Debug, "debug", opts.Debug, "debug mode")
}
return &climan.Command{
Name: "example",
FlagSetBuilder: commonFlagsSetup,
Exec: exec,
Subcommands: []*climan.Command{
{
Name: "foo",
ShortUsage: "lorem ipsum",
ShortHelp: "dolor sit amet",
FlagSetBuilder: func(fs *flag.FlagSet) {
commonFlagsSetup(fs)
opts.FooCalled = true
fs.StringVar(&opts.FooFlag, "flag", opts.FooFlag, "foo's flag")
},
Exec: exec,
}, {
Name: "bar",
ShortUsage: "lorem ipsum2",
ShortHelp: "dolor sit amet2",
FlagSetBuilder: func(fs *flag.FlagSet) {
opts.BarCalled = true
fs.IntVar(&opts.BarFlag, "flag", opts.BarFlag, "bar's flag")
},
Exec: exec,
},
},
}
}