Skip to content

Commit 3fd25eb

Browse files
kindermoumouteLaure-di
authored andcommitted
feat(core): support positional args as dynamic args as well (scaleway#4621)
Signed-off-by: Olivier Cano <[email protected]>
1 parent b8b818f commit 3fd25eb

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

core/cobra_utils_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ type testType struct {
1717
Tag string
1818
}
1919

20+
type testTypeManyTags struct {
21+
NameID string
22+
Tags []string
23+
}
24+
2025
type testDate struct {
2126
Date *time.Time
2227
}
@@ -78,6 +83,45 @@ func testGetCommands() *core.Commands {
7883
return argsI, nil
7984
},
8085
},
86+
&core.Command{
87+
Namespace: "test",
88+
Resource: "many-positional",
89+
ArgSpecs: core.ArgSpecs{
90+
{
91+
Name: "name-id",
92+
Positional: true,
93+
},
94+
{
95+
Name: "tag",
96+
Positional: true,
97+
},
98+
},
99+
AllowAnonymousClient: true,
100+
ArgsType: reflect.TypeOf(testType{}),
101+
Run: func(_ context.Context, argsI interface{}) (i interface{}, e error) {
102+
return argsI, nil
103+
},
104+
},
105+
&core.Command{
106+
Namespace: "test",
107+
Resource: "many-multi-positional",
108+
ArgSpecs: core.ArgSpecs{
109+
{
110+
Name: "name-id",
111+
Positional: true,
112+
},
113+
{
114+
Name: "tags",
115+
Positional: true,
116+
},
117+
},
118+
AcceptMultiplePositionalArgs: true,
119+
AllowAnonymousClient: true,
120+
ArgsType: reflect.TypeOf(testTypeManyTags{}),
121+
Run: func(_ context.Context, argsI interface{}) (i interface{}, e error) {
122+
return argsI, nil
123+
},
124+
},
81125
&core.Command{
82126
Namespace: "test",
83127
Resource: "raw-args",
@@ -258,6 +302,41 @@ func Test_PositionalArg(t *testing.T) {
258302
core.TestCheckGolden(),
259303
),
260304
}))
305+
306+
t.Run("many positional", core.Test(&core.TestConfig{
307+
Commands: testGetCommands(),
308+
Cmd: "scw test many-positional tag1 name-id=plop",
309+
Check: core.TestCheckExitCode(0),
310+
}))
311+
312+
t.Run("many positional", core.Test(&core.TestConfig{
313+
Commands: testGetCommands(),
314+
Cmd: "scw test many-positional tag1 name-id=plop",
315+
Check: core.TestCheckCombine(
316+
core.TestCheckExitCode(0),
317+
func(t *testing.T, ctx *core.CheckFuncCtx) {
318+
t.Helper()
319+
res := ctx.Result.(*testType)
320+
assert.Equal(t, "plop", res.NameID)
321+
assert.Equal(t, "tag1", res.Tag)
322+
},
323+
),
324+
}))
325+
326+
t.Run("many multi-positional", core.Test(&core.TestConfig{
327+
Commands: testGetCommands(),
328+
Cmd: "scw test many-multi-positional pos1 pos2 name-id=plop",
329+
Check: core.TestCheckCombine(
330+
core.TestCheckExitCode(0),
331+
func(t *testing.T, ctx *core.CheckFuncCtx) {
332+
t.Helper()
333+
res := ctx.Result.(*testTypeManyTags)
334+
assert.Equal(t, "plop", res.NameID)
335+
assert.Equal(t, "pos1", res.Tags[0])
336+
assert.Equal(t, "pos2", res.Tags[1])
337+
},
338+
),
339+
}))
261340
}
262341

263342
func Test_MultiPositionalArg(t *testing.T) {

0 commit comments

Comments
 (0)