-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_test.go
More file actions
269 lines (261 loc) · 6.66 KB
/
execute_test.go
File metadata and controls
269 lines (261 loc) · 6.66 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cobrax
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuildFlagArgs(t *testing.T) {
tests := []struct {
name string
flags map[string]any
expected []string
}{
{
name: "Empty flags",
flags: map[string]any{},
expected: []string{},
},
{
name: "Boolean flags",
flags: map[string]any{
"verbose": true,
"quiet": false,
"debug": true,
},
expected: []string{"--verbose", "--debug"},
},
{
name: "String flags",
flags: map[string]any{
"output": "result.txt",
"format": "json",
},
expected: []string{"--output", "result.txt", "--format", "json"},
},
{
name: "Integer flags",
flags: map[string]any{
"count": 10,
"timeout": 30,
},
expected: []string{"--count", "10", "--timeout", "30"},
},
{
name: "Float flags",
flags: map[string]any{
"ratio": 0.75,
"threshold": 1.5,
},
expected: []string{"--ratio", "0.75", "--threshold", "1.5"},
},
{
name: "Array flags",
flags: map[string]any{
"include": []any{"*.go", "*.md"},
"exclude": []any{"vendor"},
},
expected: []string{"--include", "*.go", "--include", "*.md", "--exclude", "vendor"},
},
{
name: "Mixed types",
flags: map[string]any{
"verbose": true,
"output": "result.txt",
"count": 5,
"tags": []any{"test", "debug"},
},
expected: []string{"--verbose", "--output", "result.txt", "--count", "5", "--tags", "test", "--tags", "debug"},
},
{
name: "Map flags (stringToString)",
flags: map[string]any{
"labels": map[string]any{
"env": "prod",
"team": "backend",
},
},
expected: []string{"--labels", "env=prod", "--labels", "team=backend"},
},
{
name: "Empty map",
flags: map[string]any{
"labels": map[string]any{},
},
expected: []string{},
},
{
name: "StringToInt map flags",
flags: map[string]any{
"ports": map[string]any{
"http": 8080,
"https": 8443,
},
},
expected: []string{"--ports", "http=8080", "--ports", "https=8443"},
},
{
name: "StringToInt64 map flags",
flags: map[string]any{
"sizes": map[string]any{
"small": int64(1024),
"medium": int64(2048),
"large": int64(4096),
},
},
expected: []string{"--sizes", "small=1024", "--sizes", "medium=2048", "--sizes", "large=4096"},
},
{
name: "Nil values",
flags: map[string]any{
"flag1": nil,
"flag2": "value",
"flag3": nil,
},
expected: []string{"--flag2", "value"},
},
{
name: "Empty flag name",
flags: map[string]any{
"": "value",
"valid": "value",
},
expected: []string{"--valid", "value"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := buildFlagArgs(tt.flags)
// Sort both slices for comparison since map iteration order is not guaranteed
assert.ElementsMatch(t, tt.expected, result)
})
}
}
// makeFlatInput is a helper to build a ToolInput from a flat map, flag set and
// ordered arg name list — mirrors what decodeToolInput produces at runtime.
func makeFlatInput(flat map[string]any, flagNames []string, argNames []string) ToolInput {
fn := make(map[string]struct{}, len(flagNames))
for _, n := range flagNames {
fn[n] = struct{}{}
}
return ToolInput{
FlatInput: flat,
FlagNames: fn,
ArgNames: argNames,
}
}
func TestBuildCommandArgs(t *testing.T) {
tests := []struct {
name string
commandName string
input ToolInput
expectedArgs []string
}{
{
name: "Simple command",
commandName: "root_test",
input: makeFlatInput(map[string]any{}, nil, nil),
expectedArgs: []string{"test"},
},
{
name: "Nested command",
commandName: "root_sub_command",
input: makeFlatInput(map[string]any{}, nil, nil),
expectedArgs: []string{"sub", "command"},
},
{
name: "Command with flags",
commandName: "root_test",
input: makeFlatInput(
map[string]any{"verbose": true, "output": "result.txt"},
[]string{"verbose", "output"},
nil,
),
expectedArgs: []string{"test", "--verbose", "--output", "result.txt"},
},
{
name: "Command with arguments",
commandName: "root_test",
// ArgNames determines order: a_file1 then b_file2
input: makeFlatInput(
map[string]any{"a_file1": "file1.txt", "b_file2": "file2.txt"},
nil,
[]string{"a_file1", "b_file2"},
),
expectedArgs: []string{"test", "file1.txt", "file2.txt"},
},
{
name: "Command with flags and arguments",
commandName: "root_deploy",
input: makeFlatInput(
map[string]any{
"namespace": "production",
"replicas": 3,
"wait": true,
"app": "my-app",
"version": "v1.2.3",
},
[]string{"namespace", "replicas", "wait"},
[]string{"app", "version"},
),
expectedArgs: []string{"deploy", "--namespace", "production", "--replicas", "3", "--wait", "my-app", "v1.2.3"},
},
{
name: "Complex nested command",
commandName: "root_cluster_node_list",
input: makeFlatInput(
map[string]any{
"output": "json",
"label": []any{"env=prod", "team=backend"},
},
[]string{"output", "label"},
nil,
),
expectedArgs: []string{"cluster", "node", "list", "--output", "json", "--label", "env=prod", "--label", "team=backend"},
},
{
name: "Command with map flags",
commandName: "root_deploy",
input: makeFlatInput(
map[string]any{
"labels": map[string]any{
"env": "production",
"version": "v1.2.3",
},
"wait": true,
"app": "my-app",
},
[]string{"labels", "wait"},
[]string{"app"},
),
expectedArgs: []string{"deploy", "--labels", "env=production", "--labels", "version=v1.2.3", "--wait", "my-app"},
},
{
name: "Command with quoted arguments",
commandName: "root_exec",
input: makeFlatInput(
map[string]any{
"a": "argument with spaces",
"b": "another quoted arg",
"c": "normal",
},
nil,
[]string{"a", "b", "c"},
),
expectedArgs: []string{"exec", "argument with spaces", "another quoted arg", "normal"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := buildCommandArgs(tt.commandName, tt.input)
// Extract command parts for comparison
commandParts := len(result) - len(tt.expectedArgs)
if commandParts >= 0 {
// Compare command parts
assert.Equal(t, tt.expectedArgs[:commandParts], result[:commandParts], "Command parts mismatch")
// Compare flags and args (order might vary for flags)
assert.ElementsMatch(t, tt.expectedArgs[commandParts:], result[commandParts:], "Flags/args mismatch")
} else {
assert.Equal(t, tt.expectedArgs, result)
}
})
}
}