-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_help_ext_test.go
183 lines (167 loc) · 3.68 KB
/
app_help_ext_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
package warg_test
// Run WARG_TEST_UPDATE_GOLDEN=1 go test ./... to update golden files
import (
"os"
"testing"
"go.bbkane.com/warg"
"go.bbkane.com/warg/command"
"go.bbkane.com/warg/flag"
"go.bbkane.com/warg/section"
"go.bbkane.com/warg/value/scalar"
)
// A grabbitSection is a simple section to test help
func grabbitSection() section.SectionT {
rootFooter := `Examples:
# Grab without config
grabbit grab
# Edit config, then grab
grabbit config edit
grabbit grab
`
configEditFooter := `Examples:
# Use defaults
grabbit config edit
# Override defaults
grabbit config edit --config-path /path/to/config --editor code
`
sec := section.New(
"grab those images!",
section.Command(
"grab",
"do the grabbity grabbity",
command.DoNothing,
),
section.Command(
"command2",
"another command",
command.DoNothing,
),
section.Command(
"command3",
"another command",
command.DoNothing,
),
section.Section(
"config",
"Change grabbit's config",
section.Footer(rootFooter),
section.Command(
"edit",
"Edit the config. A default config will be created if it doesn't exist",
command.DoNothing,
command.Footer(configEditFooter),
command.Flag(
"--editor",
"path to editor",
scalar.String(
scalar.Default("vi"),
),
flag.ConfigPath("editor"),
flag.EnvVars("EDITOR"),
flag.Required(),
),
),
),
section.Section(
"section2",
"another section",
section.Command("com", "Dummy command to pass validation", command.DoNothing),
),
section.Section(
"section3",
"another section",
section.Command("com", "Dummy command to pass validation", command.DoNothing),
),
)
return sec
}
func TestAppHelp(t *testing.T) {
updateGolden := os.Getenv("WARG_TEST_UPDATE_GOLDEN") != ""
tests := []struct {
name string
app warg.App
args []string
lookup warg.LookupFunc
}{
// toplevel just a toplevel help!
{
name: "toplevel",
app: warg.New(
"grabbit",
grabbitSection(),
warg.SkipValidation(),
),
args: []string{"grabbit", "-h", "outline"},
lookup: warg.LookupMap(nil),
},
// allcommands (no command help)
{
name: "allcommandsSection",
app: warg.New(
"grabbit",
grabbitSection(),
warg.SkipValidation(),
),
args: []string{"grabbit", "config", "--help"},
lookup: warg.LookupMap(nil),
},
// detailed
{
name: "detailedCommand",
app: warg.New(
"newAppName",
grabbitSection(),
warg.SkipValidation(),
),
args: []string{"grabbit", "config", "edit", "--help"},
lookup: warg.LookupMap(map[string]string{"EDITOR": "emacs"}),
},
{
name: "detailedSection",
app: warg.New(
"newAppName",
grabbitSection(),
warg.SkipValidation(),
),
args: []string{"grabbit", "--help", "detailed"},
lookup: warg.LookupMap(nil),
},
// outline
{
// TODO: make this print global flags!
name: "outlineCommand",
app: warg.New(
"grabbit",
grabbitSection(),
warg.SkipValidation(),
),
args: []string{"grabbit", "config", "edit", "--help", "outline"},
lookup: warg.LookupMap(map[string]string{"EDITOR": "emacs"}),
},
{
// TODO: make this print global flags!
name: "outlineSection",
app: warg.New(
"grabbit",
grabbitSection(),
warg.SkipValidation(),
),
args: []string{"grabbit", "--help", "outline"},
lookup: warg.LookupMap(nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
warg.GoldenTest(
t,
warg.GoldenTestArgs{
App: &tt.app,
UpdateGolden: updateGolden,
ExpectActionErr: false,
},
warg.OverrideArgs(tt.args),
warg.OverrideLookupFunc(tt.lookup),
)
})
}
}