-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurfavecliv3_test.go
More file actions
186 lines (169 loc) · 3.97 KB
/
urfavecliv3_test.go
File metadata and controls
186 lines (169 loc) · 3.97 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
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
package climate
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v3"
)
func TestInterpolatePathUrfaveCliV3(t *testing.T) {
hData := HandlerData{
Method: "get",
Path: "/path/{foo}/to/{bar}/with/{baz}/and/{quxx}/together/{foo}",
PathParams: []ParamMeta{
{Name: "foo", Type: String},
{Name: "bar", Type: Integer},
{Name: "baz", Type: Number},
{Name: "quxx", Type: Boolean},
},
}
cmd := cli.Command{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "foo",
Usage: "foo usage",
Value: "yes",
},
&cli.IntFlag{
Name: "bar",
Usage: "bar usage",
Value: 420,
},
&cli.Float64Flag{
Name: "baz",
Usage: "baz usage",
Value: 420.69,
},
&cli.BoolFlag{
Name: "quxx",
Usage: "quxx usage",
Value: false,
},
},
}
err := interpolatePathUrfaveCliV3(&cmd, &hData)
assert.NoError(t, err)
assert.Equal(t, hData.Path, "/path/yes/to/420/with/420.69/and/false/together/yes")
}
func assertCmdTreeUrfaveCliV3(t *testing.T, cmd *cli.Command, expected *cli.Command) {
t.Logf("Checking command %s", cmd.Name)
assert.Equal(t, expected.Name, cmd.Name)
assert.Equal(t, expected.Usage, cmd.Usage)
assert.Equal(t, expected.Aliases, cmd.Aliases)
cmd.InvalidFlagAccessHandler = func(_ context.Context, cmd *cli.Command, name string) {
t.Logf("Invalid flag accessed %s in command %v", name, cmd)
t.FailNow()
}
for _, flag := range expected.Flags {
switch flag.(type) {
case *cli.StringFlag:
cmd.String(flag.Names()[0])
case *cli.IntFlag:
cmd.Int(flag.Names()[0])
case *cli.Float64Flag:
cmd.Float64(flag.Names()[0])
case *cli.BoolFlag:
cmd.Bool(flag.Names()[0])
}
}
for _, subCmd := range cmd.Commands {
assertCmdTreeUrfaveCliV3(t, subCmd, expected.Command(subCmd.Name))
}
}
func TestBootstrapV3UrfaveCliV3(t *testing.T) {
model, err := LoadFileV3("api.yaml")
assert.NoError(t, err)
handler := func(opts *cli.Command, args []string, data HandlerData) error {
assert.Equal(t, data.PathParams, []ParamMeta{{Name: "p1", Type: Integer}})
assert.Equal(t, data.QueryParams, []ParamMeta{{Name: "p2", Type: String}})
assert.Equal(t, data.HeaderParams, []ParamMeta{{Name: "p3", Type: Number}})
assert.Equal(t, data.CookieParams, []ParamMeta{{Name: "p4", Type: Boolean}})
assert.Equal(t, data.RequestBodyParam, &ParamMeta{Name: "req-body", Type: String})
return nil
}
rootCmd := &cli.Command{
Name: "calc",
Usage: "My Calc",
}
handlers := map[string]HandlerUrfaveCliV3{
"AddGet": handler,
"AddPost": handler,
"HealthCheck": handler,
"GetInfo": handler,
}
err = BootstrapV3UrfaveCliV3(rootCmd, *model, handlers)
assert.NoError(t, err)
var noAlias []string
expectedCmd := &cli.Command{
Name: "calc",
Usage: "My Calc",
Aliases: noAlias,
Commands: []*cli.Command{
{
Name: "info",
Usage: "Operations on info",
Aliases: noAlias,
Commands: []*cli.Command{
{
Name: "GetInfo",
Usage: "Returns info",
Aliases: noAlias,
},
},
},
{
Name: "ops",
Usage: "Operations on ops",
Aliases: noAlias,
Commands: []*cli.Command{
{
Name: "add-get",
Usage: "Adds two numbers",
Aliases: []string{"ag"},
Flags: []cli.Flag{
&cli.IntFlag{
Name: "n1",
},
&cli.IntFlag{
Name: "n2",
},
},
},
{
Name: "add-post",
Usage: "Adds two numbers via POST",
Aliases: []string{"ap"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "nmap",
},
},
},
},
},
{
Name: "ping",
Usage: "Returns Ok if all is well",
Aliases: noAlias,
},
},
}
assertCmdTreeUrfaveCliV3(t, rootCmd, expectedCmd)
assert.NoError(t, rootCmd.Run(
context.Background(),
[]string{
"calc",
"info",
"GetInfo",
"--p1",
"420",
"--p2",
"yes",
"--p3",
"420.69",
"--p4",
"true",
"--req-body",
"the string body",
},
))
}