-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
humacli_test.go
253 lines (211 loc) · 5.57 KB
/
humacli_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
package humacli_test
import (
"bytes"
"context"
"fmt"
"log"
"net/http"
"os"
"syscall"
"testing"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humachi"
"github.com/danielgtaylor/huma/v2/humacli"
"github.com/go-chi/chi/v5"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
func ExampleCLI() {
// First, define your input options.
type Options struct {
Debug bool `doc:"Enable debug logging"`
Host string `doc:"Hostname to listen on."`
Port int `doc:"Port to listen on." short:"p" default:"8888"`
}
// Then, create the CLI.
cli := humacli.New(func(hooks humacli.Hooks, opts *Options) {
fmt.Printf("Options are debug:%v host:%v port%v\n",
opts.Debug, opts.Host, opts.Port)
// Set up the router & API
router := chi.NewRouter()
api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0"))
huma.Register(api, huma.Operation{
OperationID: "hello",
Method: http.MethodGet,
Path: "/hello",
}, func(ctx context.Context, input *struct{}) (*struct{}, error) {
// TODO: implement handler
return nil, nil
})
srv := &http.Server{
Addr: fmt.Sprintf("%s:%d", opts.Host, opts.Port),
Handler: router,
// TODO: Set up timeouts!
}
hooks.OnStart(func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
})
hooks.OnStop(func() {
srv.Shutdown(context.Background())
})
})
// Run the thing!
cli.Run()
}
func TestCLIPlain(t *testing.T) {
type Options struct {
Debug bool
Host string
Port int
// ignore private fields, should not crash.
ignore bool
}
cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
assert.True(t, options.Debug)
assert.Equal(t, "localhost", options.Host)
assert.Equal(t, 8001, options.Port)
assert.False(t, options.ignore)
hooks.OnStart(func() {
// Do nothing
})
})
cli.Root().SetArgs([]string{"--debug", "--host", "localhost", "--port", "8001"})
cli.Run()
}
func TestCLIEnv(t *testing.T) {
type Options struct {
Debug bool
Host string
Port int
}
os.Setenv("SERVICE_DEBUG", "true")
os.Setenv("SERVICE_HOST", "localhost")
os.Setenv("SERVICE_PORT", "8001")
defer func() {
os.Unsetenv("SERVICE_DEBUG")
os.Unsetenv("SERVICE_HOST")
os.Unsetenv("SERVICE_PORT")
}()
cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
assert.True(t, options.Debug)
assert.Equal(t, "localhost", options.Host)
assert.Equal(t, 8001, options.Port)
hooks.OnStart(func() {
// Do nothing
})
})
cli.Root().SetArgs([]string{})
cli.Run()
}
func TestCLIAdvanced(t *testing.T) {
type DebugOption struct {
Debug bool `doc:"Enable debug mode." default:"false"`
}
type Options struct {
// Example of option composition via embedded type.
DebugOption
Host string `doc:"Hostname to listen on."`
Port *int `doc:"Port to listen on." short:"p" default:"8000"`
Timeout time.Duration `doc:"Request timeout." default:"5s"`
}
cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
assert.True(t, options.Debug)
assert.Equal(t, "localhost", options.Host)
assert.Equal(t, 8001, *options.Port)
assert.Equal(t, 10*time.Second, options.Timeout)
hooks.OnStart(func() {
// Do nothing
})
})
// A custom pre-run isn't overwritten and should still work!
customPreRun := false
cli.Root().PersistentPreRun = func(cmd *cobra.Command, args []string) {
customPreRun = true
}
cli.Root().SetArgs([]string{"--debug", "--host", "localhost", "--port", "8001", "--timeout", "10s"})
cli.Run()
assert.True(t, customPreRun)
}
func TestCLIHelp(t *testing.T) {
type Options struct {
Debug bool
Host string
Port int
}
cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
// Do nothing
})
cli.Root().Use = "myapp"
cli.Root().SetArgs([]string{"--help"})
buf := bytes.NewBuffer(nil)
cli.Root().SetOut(buf)
cli.Root().SetErr(buf)
cli.Run()
assert.Equal(t, "Usage:\n myapp [flags]\n\nFlags:\n --debug \n -h, --help help for myapp\n --host string \n --port int\n", buf.String())
}
func TestCLICommandWithOptions(t *testing.T) {
type Options struct {
Debug bool
}
cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
// Do nothing
})
wasSet := false
cli.Root().AddCommand(&cobra.Command{
Use: "custom",
Run: humacli.WithOptions(func(cmd *cobra.Command, args []string, options *Options) {
if options.Debug {
wasSet = true
}
}),
})
cli.Root().SetArgs([]string{"custom", "--debug"})
cli.Run()
assert.True(t, wasSet)
}
func TestCLIShutdown(t *testing.T) {
type Options struct{}
started := make(chan bool, 1)
stopping := make(chan bool, 1)
cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
hooks.OnStart(func() {
started <- true
<-stopping
})
hooks.OnStop(func() {
stopping <- true
})
})
go func() {
time.Sleep(10 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()
cli.Root().SetArgs([]string{})
cli.Run()
assert.True(t, <-started)
}
func TestCLIBadType(t *testing.T) {
type Options struct {
Debug []struct{}
}
assert.Panics(t, func() {
humacli.New(func(hooks humacli.Hooks, options *Options) {})
})
}
func TestCLIBadDefaults(t *testing.T) {
type OptionsBool struct {
Debug bool `default:"notabool"`
}
type OptionsInt struct {
Debug int `default:"notanint"`
}
assert.Panics(t, func() {
humacli.New(func(hooks humacli.Hooks, options *OptionsBool) {})
})
assert.Panics(t, func() {
humacli.New(func(hooks humacli.Hooks, options *OptionsInt) {})
})
}