-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli_test.go
204 lines (196 loc) · 5.65 KB
/
cli_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
package sdkclient_test
import (
"bytes"
"context"
"encoding/json"
"testing"
sdkclient "github.com/fujiwara/awslim"
)
type PagingOutput struct {
Next string `json:"Next,omitempty"`
}
func init() {
sdkclient.SetClientMethod("foo", "List", func(_ context.Context, _ *sdkclient.ClientMethodParam) (any, error) {
return []string{"a", "b", "c"}, nil
})
sdkclient.SetClientMethod("foo", "Get", func(_ context.Context, _ *sdkclient.ClientMethodParam) (any, error) {
return struct{ Name string }{Name: "foo"}, nil
})
sdkclient.SetClientMethod("bar", "List", func(_ context.Context, _ *sdkclient.ClientMethodParam) (any, error) {
return []string{"x", "y", "z"}, nil
})
sdkclient.SetClientMethod("bar", "Get", func(_ context.Context, _ *sdkclient.ClientMethodParam) (any, error) {
return struct{ Name string }{Name: "bar"}, nil
})
sdkclient.SetClientMethod("baz", "Echo", func(_ context.Context, p *sdkclient.ClientMethodParam) (any, error) {
var v any
err := json.Unmarshal(p.InputBytes, &v)
return v, err
})
sdkclient.SetClientMethod("baz", "Paging", func(_ context.Context, p *sdkclient.ClientMethodParam) (any, error) {
var v map[string]string
json.Unmarshal(p.InputBytes, &v)
switch v["Start"] {
case "":
return PagingOutput{Next: "1"}, nil
case "1":
return PagingOutput{Next: "2"}, nil
case "2":
return PagingOutput{Next: "3"}, nil
}
return PagingOutput{}, nil
})
}
type TestCase struct {
Name string
Args []string
Expect string
IsError bool
Env map[string]string
}
var TestCases = []TestCase{
{
Name: "no args (list services)",
Args: []string{},
Expect: "bar\nbaz\nfoo\n",
},
{
Name: "list methods of foo",
Args: []string{"foo"},
Expect: "Get\nList\n",
},
{
Name: "list methods of bar",
Args: []string{"bar"},
Expect: "Get\nList\n",
},
{
Name: "list methods of baz",
Args: []string{"baz"},
Expect: "Echo\nPaging\n",
},
{
Name: "call foo#Client.List",
Args: []string{"foo", "List"},
Expect: "[\n \"a\",\n \"b\",\n \"c\"\n]\n",
},
{
Name: "call foo#Client.Get",
Args: []string{"foo", "Get"},
Expect: "{\n \"Name\": \"foo\"\n}\n",
},
{
Name: "call foo#Client.List",
Args: []string{"foo", "List", "help"},
Expect: "See https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/foo#Client.List\n",
},
{
Name: "call foo#Client.List -c",
Args: []string{"foo", "List", "-c"},
Expect: `["a","b","c"]`,
},
{
Name: "call bar#Client.Get --compact",
Args: []string{"bar", "Get", "--compact"},
Expect: `{"Name":"bar"}`,
},
{
Name: "call baz#Client.Echo",
Args: []string{"baz", "Echo", `{"Example": "value"}`},
Expect: "{\n \"Example\": \"value\"\n}\n",
},
{
Name: "call baz#Client.Echo Jsonnet",
Args: []string{"baz", "Echo", `{Example: std.extVar("value")}`, "--ext-str", "value=foo"},
Expect: "{\n \"Example\": \"foo\"\n}\n",
},
{
Name: "call baz#Client.Echo Jsonnet file",
Args: []string{"baz", "Echo", "tests/echo.jsonnet", "--ext-code", "a=1;b=2", "-c"},
Expect: `{"Sum":3}`,
},
{
Name: "call baz#Client.Echo JMESPath",
Args: []string{"baz", "Echo", `{"Example": ["a","b","c"]}`, "--query", "Example[0]", "-c"},
Expect: `"a"`,
},
{
Name: "call baz#Client.Echo raw string",
Args: []string{"baz", "Echo", `{"Example": "value"}`, "-q", "Example", "--raw-output"},
Expect: "value\n",
},
{
Name: "call baz#Client.Echo raw object",
Args: []string{"baz", "Echo", `{"Example": "value"}`, "-r"},
Expect: "{\n \"Example\": \"value\"\n}\n",
},
{
Name: "call baz#Client.Paging",
Args: []string{"baz", "Paging", `{}`, "--follow-next", "Next=Start", "-c"},
Expect: `{"Next":"1"}{"Next":"2"}{"Next":"3"}{}`,
},
{
Name: "call baz#Client.Echo with args",
Args: []string{"baz", "Echo", `{"Examples":[_(0), _(1), std.parseInt(_(2))]}`, "example1", "example2", "3", "-c"},
Expect: `{"Examples":["example1","example2",3]}`,
},
{
Name: "call baz#Client.Echo with env",
Args: []string{"baz", "Echo", `{"Examples":[env('MYENV','default'),env('NOENV','default')]}`, "-c"},
Expect: `{"Examples":["example1","default"]}`,
Env: map[string]string{"MYENV": "example1"}, // NOENV is not set
},
{
Name: "call baz#Client.Echo with must_env",
Args: []string{"baz", "Echo", `{"Example":must_env('MYENV')}`, "-c"},
Expect: `{"Example":"example1"}`,
Env: map[string]string{"MYENV": "example1"},
},
{
Name: "call baz#Client.Echo with must_env is not set",
Args: []string{"baz", "Echo", `{"Example":must_env('MYENV')}`, "-c"},
Env: map[string]string{},
IsError: true,
},
{
Name: "call baz#Client.Echo with dynamic flags",
Args: []string{"baz", "Echo", "--foo-Foo", "FOO", `{Baz:"baz"}`, "-c", "--bar=BAR"},
Expect: `{"Bar":"BAR","Baz":"baz","FooFoo":"FOO"}`,
},
}
func TestRun(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", "testdata") // don't use real config
for _, tc := range TestCases {
ctx := context.Background()
t.Run(tc.Name, func(t *testing.T) {
for k, v := range tc.Env {
t.Setenv(k, v)
}
buf := &bytes.Buffer{}
c, err := newCLI(ctx, tc.Args, buf)
if err != nil {
t.Fatal(err)
}
err = c.Dispatch(ctx)
if err != nil {
t.Log(err)
}
if tc.IsError && err == nil {
t.Fatal("expected error, but got nil")
} else if !tc.IsError && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got := buf.String(); got != tc.Expect {
t.Errorf("unexpected output: got %q, expect %q", got, tc.Expect)
}
})
}
}
func newCLI(ctx context.Context, args []string, out *bytes.Buffer) (*sdkclient.CLI, error) {
c, err := sdkclient.NewCLI(ctx, args)
if err != nil {
return nil, err
}
c.SetWriter(out)
return c, nil
}