-
Notifications
You must be signed in to change notification settings - Fork 6
/
cobra2snooty_test.go
284 lines (252 loc) · 8.6 KB
/
cobra2snooty_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2022 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra2snooty
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/spf13/cobra"
)
func emptyRun(*cobra.Command, []string) {}
var rootCmd *cobra.Command
var echoCmd *cobra.Command
func Root() *cobra.Command {
if rootCmd != nil {
return rootCmd
}
rootCmd = &cobra.Command{
Use: "root",
Short: "Root short description",
Long: "Root long description",
Run: emptyRun,
}
rootCmd.PersistentFlags().StringP("rootflag", "r", "two", "")
rootCmd.PersistentFlags().StringP("strtwo", "t", "two", "help message for parent flag strtwo")
printCmd := &cobra.Command{
Use: "print [string to print]",
Short: "Print anything to the screen",
Long: `an absolutely utterly useless command for testing.`,
}
printCmd.PersistentFlags().StringP("strthree", "s", "three", "help message for flag strthree")
printCmd.Flags().IntP("intthree", "i", 345, "help message for flag intthree")
printCmd.Flags().BoolP("boolthree", "b", true, "help message for flag boolthree")
dummyCmd := &cobra.Command{
Use: "dummy [action]",
Short: "Performs a dummy action",
}
rootCmd.AddCommand(printCmd, Echo(), dummyCmd)
return rootCmd
}
func Echo() *cobra.Command {
if echoCmd != nil {
return echoCmd
}
echoCmd = &cobra.Command{
Use: "echo <string to print> [test param]",
Aliases: []string{"say"},
Short: "Echo anything to the screen",
Long: "an utterly useless command for testing",
Example: "# Example with intro text\n atlas command no intro text\n",
Annotations: map[string]string{
"string to printDesc": "A string to print",
"test paramDesc": "just for testing",
},
}
echoCmd.PersistentFlags().StringP("strone", "s", "one", "help message for flag strone")
echoCmd.PersistentFlags().BoolP("persistentbool", "p", false, "help message for flag persistentbool")
echoCmd.Flags().IntP("intone", "i", 123, "help message for flag intone")
echoCmd.Flags().BoolP("boolone", "b", true, "help message for flag boolone")
echoCmd.Flags().StringToStringP("stringtostring", "x", nil, "help message for flag stringtostring")
timesCmd := &cobra.Command{
Use: "times [# times] [string to echo]",
SuggestFor: []string{"counts"},
Short: "Echo anything to the screen more times",
Long: `a slightly useless command for testing.`,
Run: emptyRun,
}
timesCmd.PersistentFlags().StringP("strtwo", "t", "2", "help message for child flag strtwo")
timesCmd.Flags().IntP("inttwo", "j", 234, "help message for flag inttwo")
timesCmd.Flags().BoolP("booltwo", "c", false, "help message for flag booltwo")
echoCmd.AddCommand(timesCmd, EchoSubCmd(), deprecatedCmd)
return echoCmd
}
var echoSubCmd *cobra.Command
func EchoSubCmd() *cobra.Command {
if echoSubCmd != nil {
return echoSubCmd
}
echoSubCmd = &cobra.Command{
Use: "echosub [string to print]",
Short: "second sub command for echo",
Long: "an absolutely utterly useless command for testing gendocs!.",
Run: emptyRun,
}
return echoSubCmd
}
var deprecatedCmd = &cobra.Command{
Use: "deprecated [can't do anything here]",
Short: "A command which is deprecated",
Long: `an absolutely utterly useless command for testing deprecation!.`,
Deprecated: "Please use echo instead",
}
func TestGenDocs(t *testing.T) {
// We generate on a subcommand, so we have both subcommands and parents
buf := new(bytes.Buffer)
Root() // init root
if err := GenDocs(Echo(), buf); err != nil {
t.Fatal(err)
}
output := buf.String()
checkStringContains(t, output, Echo().Long)
checkStringContains(t, output, `# Example with intro text
atlas command no intro text
`)
checkStringContains(t, output, "boolone")
checkStringContains(t, output, "rootflag")
//
checkStringContains(t, output, fmt.Sprintf(" * - string to print\n - string\n - true\n - %s\n", Echo().Annotations["string to printDesc"]))
checkStringContains(t, output, fmt.Sprintf(" * - test param\n - string\n - false\n - %s\n", Echo().Annotations["test paramDesc"]))
checkStringOmits(t, output, Root().Short)
checkStringContains(t, output, EchoSubCmd().Short)
checkStringOmits(t, output, deprecatedCmd.Short)
// Verify that the text "This value defaults to" is not printed when the default value is provided to StringToStringP
checkStringContains(t, output, "* - -x, --stringtostring\n - key=value\n - false\n - help message for flag stringtostring\n *")
}
func TestGenDocsNoHiddenParents(t *testing.T) {
// We generate on a subcommand so we have both subcommands and parents
for _, name := range []string{"rootflag", "strtwo"} {
f := Root().PersistentFlags().Lookup(name)
f.Hidden = true
t.Cleanup(func() { f.Hidden = false })
}
buf := new(bytes.Buffer)
if err := GenDocs(Echo(), buf); err != nil {
t.Fatal(err)
}
output := buf.String()
checkStringContains(t, output, Echo().Long)
checkStringContains(t, output, `# Example with intro text
atlas command no intro text
`)
checkStringContains(t, output, "boolone")
checkStringOmits(t, output, "rootflag")
checkStringOmits(t, output, Root().Short)
checkStringContains(t, output, Echo().Short)
checkStringOmits(t, output, deprecatedCmd.Short)
checkStringOmits(t, output, "Options inherited from parent commands")
}
func TestGenDocsNoTag(t *testing.T) {
Root().DisableAutoGenTag = true
defer func() { Root().DisableAutoGenTag = false }()
buf := new(bytes.Buffer)
if err := GenDocs(Root(), buf); err != nil {
t.Fatal(err)
}
output := buf.String()
unexpected := "Auto generated"
checkStringOmits(t, output, unexpected)
}
func TestGenTreeDocs(t *testing.T) {
c := &cobra.Command{
Use: "do <arg1> [arg2]",
Annotations: map[string]string{
"arg1Desc": "desc",
"arg2Desc": "desc",
},
}
tmpdir, err := os.MkdirTemp("", "test-gen-rst-tree")
if err != nil {
t.Fatalf("Failed to create tmpdir: %s", err.Error())
}
defer os.RemoveAll(tmpdir)
if err := GenTreeDocs(c, tmpdir); err != nil {
t.Fatalf("GenTreeDocs failed: %s", err.Error())
}
if _, err := os.Stat(filepath.Join(tmpdir, "do.txt")); err != nil {
t.Fatalf("Expected file 'do.txt' to exist")
}
}
func BenchmarkGenDocsToFile(b *testing.B) {
file, err := os.CreateTemp("", "")
if err != nil {
b.Fatal(err)
}
defer os.Remove(file.Name())
defer file.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := GenDocs(Root(), file); err != nil {
b.Fatal(err)
}
}
}
func checkStringContains(t *testing.T, got, expected string) {
t.Helper()
if !strings.Contains(got, expected) {
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)
}
}
func checkStringOmits(t *testing.T, got, expected string) {
t.Helper()
if strings.Contains(got, expected) {
t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)
}
}
func TestArgsRegex(t *testing.T) {
t.Run("simple", func(t *testing.T) {
result := argsRegex.FindAllString("<arg1> [arg2]", -1)
expected := []string{"<arg1>", "[arg2]"}
for i := range result {
if result[i] != expected[i] {
t.Fatalf("expected: %s, got: %s\n", expected[i], result[i])
}
}
})
t.Run("with spaces", func(t *testing.T) {
result := argsRegex.FindAllString("<this arg1> [that arg2]", -1)
expected := []string{"<this arg1>", "[that arg2]"}
for i := range result {
if result[i] != expected[i] {
t.Fatalf("expected: %s, got: %s\n", expected[i], result[i])
}
}
})
t.Run("repeating", func(t *testing.T) {
result := argsRegex.FindAllString("<arg1>... [arg2]...", -1)
expected := []string{"<arg1>", "[arg2]"}
for i := range result {
if result[i] != expected[i] {
t.Fatalf("expected: %s, got: %s\n", expected[i], result[i])
}
}
})
t.Run("empty", func(t *testing.T) {
result := argsRegex.FindAllString("<> []", -1)
if len(result) != 0 {
t.Fatalf("expected no matches\n")
}
})
t.Run("complex", func(t *testing.T) {
result := argsRegex.FindAllString("<this arg1> <that arg2> [optional] [long option]", -1)
expected := []string{"<this arg1>", "<that arg2>", "[optional]", "[long option]"}
for i := range result {
if result[i] != expected[i] {
t.Fatalf("expected: %s, got: %s\n", expected[i], result[i])
}
}
})
}