-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs_test.go
More file actions
68 lines (64 loc) · 1.93 KB
/
docs_test.go
File metadata and controls
68 lines (64 loc) · 1.93 KB
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
package cli
import (
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// TestEverySubcommandIsDocumented asserts the §7.1 contract: every Cobra
// subcommand (including nested subcommands like `query consumers`) has Use,
// Short, Long, Example, and RunE populated; every flag has Usage text. A
// subcommand or flag that lacks docs fails the build.
func TestEverySubcommandIsDocumented(t *testing.T) {
root := NewRootCommand()
var walk func(parent string, cmd *cobra.Command)
walk = func(parent string, cmd *cobra.Command) {
// Skip Cobra auto-generated children (help / completion).
if cmd.Hidden || cmd.Name() == "help" || cmd.Name() == "completion" {
return
}
full := cmd.Name()
if parent != "" {
full = parent + " " + full
}
if cmd.Use == "" {
t.Errorf("%s: Use is empty", full)
}
if cmd.Short == "" {
t.Errorf("%s: Short is empty", full)
}
if cmd.Long == "" {
t.Errorf("%s: Long is empty", full)
}
if cmd.Example == "" {
t.Errorf("%s: Example is empty", full)
} else if lines := strings.Split(cmd.Example, "\n"); len(lines) < 3 {
t.Errorf("%s: Example must have >= 3 lines, got %d", full, len(lines))
}
if cmd.RunE == nil {
t.Errorf("%s: must use RunE (returns error), not Run", full)
}
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Usage == "" {
t.Errorf("%s --%s: Usage is empty", full, f.Name)
}
})
for _, child := range cmd.Commands() {
walk(full, child)
}
}
for _, cmd := range root.Commands() {
walk("", cmd)
}
}
// TestRootCommandPersistentFlagsDocumented ensures the global flags themselves
// are documented — they're inherited by every subcommand so a missing Usage
// there pollutes every help screen.
func TestRootCommandPersistentFlagsDocumented(t *testing.T) {
root := NewRootCommand()
root.PersistentFlags().VisitAll(func(f *pflag.Flag) {
if f.Usage == "" {
t.Errorf("persistent flag --%s: Usage is empty", f.Name)
}
})
}