Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): disable check args exist valid for raw #788

Merged
merged 6 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions internal/core/cobra_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"reflect"
"testing"

"github.com/scaleway/scaleway-cli/internal/args"
)

type testType struct {
Expand Down Expand Up @@ -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
},
},
)
}

Expand Down Expand Up @@ -71,6 +88,25 @@ func Test_handleUnmarshalErrors(t *testing.T) {
}))
}

func Test_RawArgs(t *testing.T) {
Copy link
Contributor

@loicbourgois loicbourgois Mar 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoudn't we have a testcobraRun func that only test cobraRun ?

I find it strnage that we hav to test an entire command.

Shouldn't we only call cobraRun() in the test ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could 😄 I'll fix that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though it may be more complicated 🤔

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{
Expand Down
7 changes: 7 additions & 0 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
autocomplete
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
complete install script