diff --git a/internal/core/cobra_utils.go b/internal/core/cobra_utils.go index ebc1222889..d5f0f70028 100644 --- a/internal/core/cobra_utils.go +++ b/internal/core/cobra_utils.go @@ -33,17 +33,19 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string) // Apply default values on missing args. rawArgs = ApplyDefaultValues(cmd.ArgSpecs, rawArgs) - // Check args exist valid - argsSlice := args.SplitRawNoError(rawArgs) - for _, arguments := range argsSlice { - // TODO: handle args such as tags.index - if cmd.ArgSpecs.GetByName(arguments[0]) == nil && - cmd.ArgSpecs.GetByName(arguments[0]+".{index}") == nil && - !strings.Contains(arguments[0], ".") { - return handleUnmarshalErrors(cmd, &args.UnmarshalArgError{ - Err: &args.UnknownArgError{}, - ArgName: arguments[0], - }) + // Check args exist valid if ArgsType is not args.RawArgs + if cmd.ArgsType != reflect.TypeOf(args.RawArgs{}) { + argsSlice := args.SplitRawNoError(rawArgs) + for _, arguments := range argsSlice { + // TODO: handle args such as tags.index + if cmd.ArgSpecs.GetByName(arguments[0]) == nil && + cmd.ArgSpecs.GetByName(arguments[0]+".{index}") == nil && + !strings.Contains(arguments[0], ".") { + return handleUnmarshalErrors(cmd, &args.UnmarshalArgError{ + Err: &args.UnknownArgError{}, + ArgName: arguments[0], + }) + } } } diff --git a/internal/core/cobra_utils_test.go b/internal/core/cobra_utils_test.go index 2e54fb1048..75274f5b2e 100644 --- a/internal/core/cobra_utils_test.go +++ b/internal/core/cobra_utils_test.go @@ -5,6 +5,8 @@ import ( "fmt" "reflect" "testing" + + "github.com/scaleway/scaleway-cli/internal/args" ) type testType struct { @@ -42,6 +44,21 @@ func testGetCommands() *Commands { return "", nil }, }, + &Command{ + Namespace: "test-raw-args", + ArgsType: reflect.TypeOf(args.RawArgs{}), + Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) { + res := "" + rawArgs := *argsI.(*args.RawArgs) + for i, arg := range rawArgs { + res += arg + if i != len(rawArgs)-1 { + res += " " + } + } + return res, nil + }, + }, ) } @@ -71,6 +88,25 @@ func Test_handleUnmarshalErrors(t *testing.T) { })) } +func Test_RawArgs(t *testing.T) { + t.Run("Simple", Test(&TestConfig{ + Commands: testGetCommands(), + Cmd: "scw test-raw-args -- blabla", + Check: TestCheckCombine( + TestCheckExitCode(0), + TestCheckStdout("blabla\n"), + ), + })) + t.Run("Multiple", Test(&TestConfig{ + Commands: testGetCommands(), + Cmd: "scw test-raw-args -- blabla foo bar", + Check: TestCheckCombine( + TestCheckExitCode(0), + TestCheckStdout("blabla foo bar\n"), + ), + })) +} + func Test_PositionalArg(t *testing.T) { t.Run("Error", func(t *testing.T) { t.Run("Missing1", Test(&TestConfig{ diff --git a/internal/core/testing.go b/internal/core/testing.go index 19ae578c50..303129ba9c 100644 --- a/internal/core/testing.go +++ b/internal/core/testing.go @@ -373,6 +373,13 @@ func TestCheckError(err error) TestCheck { } } +// TestCheckStdout asserts stdout using string +func TestCheckStdout(stdout string) TestCheck { + return func(t *testing.T, ctx *CheckFuncCtx) { + assert.Equal(t, stdout, string(ctx.Stdout), "Invalid stdout") + } +} + func testGolden(t *testing.T, goldenPath string, actual []byte) { actualIsEmpty := len(actual) == 0