-
Notifications
You must be signed in to change notification settings - Fork 2
/
docs_test.go
446 lines (352 loc) · 9.91 KB
/
docs_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package docs
import (
"bytes"
"embed"
"io"
"io/fs"
"net/mail"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v3"
)
var (
//go:embed testdata
testdata embed.FS
)
func expectFileContent(t *testing.T, file, got string) {
data, err := testdata.ReadFile(file)
r := require.New(t)
r.NoError(err)
r.Equal(
string(normalizeNewlines([]byte(got))),
string(normalizeNewlines(data)),
)
}
func normalizeNewlines(d []byte) []byte {
return bytes.ReplaceAll(
bytes.ReplaceAll(
d,
[]byte("\r\n"), []byte("\n"),
),
[]byte("\r"), []byte("\n"),
)
}
func buildExtendedTestCommand() *cli.Command {
return &cli.Command{
Writer: io.Discard,
Name: "greet",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "socket",
Aliases: []string{"s"},
Usage: "some 'usage' text",
Value: "value",
TakesFile: true,
},
&cli.StringFlag{Name: "flag", Aliases: []string{"fl", "f"}},
&cli.BoolFlag{
Name: "another-flag",
Aliases: []string{"b"},
Usage: "another usage text",
Sources: cli.EnvVars("EXAMPLE_VARIABLE_NAME"),
},
&cli.BoolFlag{
Name: "hidden-flag",
Hidden: true,
},
},
Commands: []*cli.Command{{
Aliases: []string{"c"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "flag",
Aliases: []string{"fl", "f"},
TakesFile: true,
},
&cli.BoolFlag{
Name: "another-flag",
Aliases: []string{"b"},
Usage: "another usage text",
},
},
Name: "config",
Usage: "another usage test",
Commands: []*cli.Command{{
Aliases: []string{"s", "ss"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "sub-flag", Aliases: []string{"sub-fl", "s"}},
&cli.BoolFlag{
Name: "sub-command-flag",
Aliases: []string{"s"},
Usage: "some usage text",
},
},
Name: "sub-config",
Usage: "another usage test",
}},
}, {
Aliases: []string{"i", "in"},
Name: "info",
Usage: "retrieve generic information",
}, {
Name: "some-command",
}, {
Name: "hidden-command",
Hidden: true,
}, {
Aliases: []string{"u"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "flag",
Aliases: []string{"fl", "f"},
TakesFile: true,
},
&cli.BoolFlag{
Name: "another-flag",
Aliases: []string{"b"},
Usage: "another usage text",
},
},
Name: "usage",
Usage: "standard usage text",
UsageText: `
Usage for the usage text
- formatted: Based on the specified ConfigMap and summon secrets.yml
- list: Inspect the environment for a specific process running on a Pod
- for_effect: Compare 'namespace' environment with 'local'
` + "```" + `
func() { ... }
` + "```" + `
Should be a part of the same code block
`,
Commands: []*cli.Command{{
Aliases: []string{"su"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "sub-command-flag",
Aliases: []string{"s"},
Usage: "some usage text",
},
},
Name: "sub-usage",
Usage: "standard usage text",
UsageText: "Single line of UsageText",
}},
}},
UsageText: "app [first_arg] [second_arg]",
Description: `Description of the application.`,
Usage: "Some app",
Authors: []any{
"Harrison <[email protected]>",
&mail.Address{Name: "Oliver Allen", Address: "[email protected]"},
},
}
}
func TestToMarkdownFull(t *testing.T) {
cmd := buildExtendedTestCommand()
res, err := ToMarkdown(cmd)
require.NoError(t, err)
expectFileContent(t, "testdata/expected-doc-full.md", res)
}
func TestToTabularMarkdown(t *testing.T) {
app := buildExtendedTestCommand()
t.Run("full", func(t *testing.T) {
res, err := ToTabularMarkdown(app, "app")
require.NoError(t, err)
expectFileContent(t, "testdata/expected-tabular-markdown-full.md", res)
})
t.Run("with empty path", func(t *testing.T) {
res, err := ToTabularMarkdown(app, "")
require.NoError(t, err)
expectFileContent(t, "testdata/expected-tabular-markdown-full.md", res)
})
t.Run("with custom app path", func(t *testing.T) {
res, err := ToTabularMarkdown(app, "/usr/local/bin")
require.NoError(t, err)
expectFileContent(t, "testdata/expected-tabular-markdown-custom-app-path.md", res)
})
}
func TestToTabularMarkdownFailed(t *testing.T) {
tpl := MarkdownTabularDocTemplate
t.Cleanup(func() { MarkdownTabularDocTemplate = tpl })
MarkdownTabularDocTemplate = "{{ .Foo }}"
app := buildExtendedTestCommand()
res, err := ToTabularMarkdown(app, "")
r := require.New(t)
r.Error(err)
r.Equal("", res)
}
func TestToTabularToFileBetweenTags(t *testing.T) {
expectedDocs, fErr := testdata.ReadFile("testdata/expected-tabular-markdown-full.md")
r := require.New(t)
r.NoError(fErr)
t.Run("default tags", func(t *testing.T) {
tmpFile, err := os.CreateTemp("", "")
r := require.New(t)
r.NoError(err)
t.Cleanup(func() { _ = os.Remove(tmpFile.Name()) })
_, err = tmpFile.WriteString(`# App readme file
Some description
<!--GENERATED:CLI_DOCS-->
<!--/GENERATED:CLI_DOCS-->
Some other text`)
r.NoError(err)
_ = tmpFile.Close()
r.NoError(ToTabularToFileBetweenTags(buildExtendedTestCommand(), "app", tmpFile.Name()))
content, err := os.ReadFile(tmpFile.Name())
r.NoError(err)
content = normalizeNewlines(content)
expected := normalizeNewlines([]byte(`# App readme file
Some description
<!--GENERATED:CLI_DOCS-->
<!-- Documentation inside this block generated by github.com/urfave/cli-docs/v3; DO NOT EDIT -->
` + string(expectedDocs) + `
<!--/GENERATED:CLI_DOCS-->
Some other text`))
r.Equal(string(expected), string(content))
})
t.Run("custom tags", func(t *testing.T) {
r := require.New(t)
tmpFile, err := os.CreateTemp("", "")
r.NoError(err)
t.Cleanup(func() { _ = os.Remove(tmpFile.Name()) }) // cleanup
_, err = tmpFile.WriteString(`# App readme file
Some description
foo_BAR|baz
lorem+ipsum
Some other text`)
r.NoError(err)
_ = tmpFile.Close()
r.NoError(ToTabularToFileBetweenTags(buildExtendedTestCommand(), "app", tmpFile.Name(), "foo_BAR|baz", "lorem+ipsum"))
content, err := os.ReadFile(tmpFile.Name())
r.NoError(err)
content = normalizeNewlines(content)
expected := normalizeNewlines([]byte(`# App readme file
Some description
foo_BAR|baz
<!-- Documentation inside this block generated by github.com/urfave/cli-docs/v3; DO NOT EDIT -->
` + string(expectedDocs) + `
lorem+ipsum
Some other text`))
r.Equal(string(expected), string(content))
})
t.Run("missing file", func(t *testing.T) {
r := require.New(t)
tmpFile, err := os.CreateTemp("", "")
r.NoError(err)
_ = tmpFile.Close()
r.NoError(os.Remove(tmpFile.Name()))
err = ToTabularToFileBetweenTags(buildExtendedTestCommand(), "app", tmpFile.Name())
r.ErrorIs(err, fs.ErrNotExist)
})
}
func TestToMarkdown(t *testing.T) {
t.Run("no flags", func(t *testing.T) {
app := buildExtendedTestCommand()
app.Flags = nil
res, err := ToMarkdown(app)
require.NoError(t, err)
expectFileContent(t, "testdata/expected-doc-no-flags.md", res)
})
t.Run("no commands", func(t *testing.T) {
app := buildExtendedTestCommand()
app.Commands = nil
res, err := ToMarkdown(app)
require.NoError(t, err)
expectFileContent(t, "testdata/expected-doc-no-commands.md", res)
})
t.Run("no authors", func(t *testing.T) {
app := buildExtendedTestCommand()
app.Authors = []any{}
res, err := ToMarkdown(app)
require.NoError(t, err)
expectFileContent(t, "testdata/expected-doc-no-authors.md", res)
})
t.Run("no usage text", func(t *testing.T) {
app := buildExtendedTestCommand()
app.UsageText = ""
res, err := ToMarkdown(app)
require.NoError(t, err)
expectFileContent(t, "testdata/expected-doc-no-usagetext.md", res)
})
}
func TestToMan(t *testing.T) {
app := buildExtendedTestCommand()
res, err := ToMan(app)
require.NoError(t, err)
expectFileContent(t, "testdata/expected-doc-full.man", res)
}
func TestToManParseError(t *testing.T) {
app := buildExtendedTestCommand()
tmp := MarkdownDocTemplate
t.Cleanup(func() { MarkdownDocTemplate = tmp })
MarkdownDocTemplate = "{{ .App.Name"
_, err := ToMan(app)
require.ErrorContains(t, err, "template: cli:1: unclosed action")
}
func TestToManWithSection(t *testing.T) {
cmd := buildExtendedTestCommand()
res, err := ToManWithSection(cmd, 8)
require.NoError(t, err)
expectFileContent(t, "testdata/expected-doc-full.man", res)
}
func Test_prepareUsageText(t *testing.T) {
t.Run("no UsageText provided", func(t *testing.T) {
cmd := &cli.Command{}
res := prepareUsageText(cmd)
require.Equal(t, "", res)
})
t.Run("single line UsageText", func(t *testing.T) {
cmd := &cli.Command{UsageText: "Single line usage text"}
res := prepareUsageText(cmd)
require.Equal(t, ">Single line usage text\n", res)
})
t.Run("multiline UsageText", func(t *testing.T) {
cmd := &cli.Command{
UsageText: `
Usage for the usage text
- Should be a part of the same code block
`,
}
res := prepareUsageText(cmd)
require.Equal(t, ` Usage for the usage text
- Should be a part of the same code block
`, res)
})
t.Run("multiline UsageText has formatted embedded markdown", func(t *testing.T) {
cmd := &cli.Command{
UsageText: `
Usage for the usage text
` + "```" + `
func() { ... }
` + "```" + `
Should be a part of the same code block
`,
}
res := prepareUsageText(cmd)
require.Equal(t, ` Usage for the usage text
`+"```"+`
func() { ... }
`+"```"+`
Should be a part of the same code block
`, res)
})
}
func Test_prepareUsage(t *testing.T) {
t.Run("no Usage provided", func(t *testing.T) {
cmd := &cli.Command{}
res := prepareUsage(cmd, "")
require.Equal(t, "", res)
})
t.Run("simple Usage", func(t *testing.T) {
cmd := &cli.Command{Usage: "simple usage text"}
res := prepareUsage(cmd, "")
require.Equal(t, cmd.Usage+"\n", res)
})
t.Run("simple Usage with UsageText", func(t *testing.T) {
cmd := &cli.Command{Usage: "simple usage text"}
res := prepareUsage(cmd, "a non-empty string")
require.Equal(t, cmd.Usage+"\n\n", res)
})
}