-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
test(client): wrap cmd.SetArgs to fix bugs for cmd.SetArgs #18960
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
fa6ff57
refactor: wrap cmd.SetArgs to fix bugs for cmd.SetArgs
levisyin 4ddd859
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 4914c8c
make ci lint happy
levisyin 9e33798
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 1c93feb
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 7d462b8
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 7875553
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 21cb771
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 5668e4c
Merge branch 'main' into feat/wrap-cmd-setargs
facundomedica 62c6f60
refactor: rename to ResetArgs
levisyin a392ecb
refactor: ResetArgs should only do the resetting/clearing
levisyin b1d8255
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 68b709e
update cmd.go
levisyin 01404ae
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 34c11f3
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin bea81d1
Update internal/testutil/cmd.go
levisyin c02a2b9
Update internal/testutil/cmd.go
levisyin ec7fc1d
Update internal/testutil/cmd.go
levisyin 79e8515
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin ae114e5
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin d8b3f19
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 41ecd66
fix: ci lint
levisyin 151c6bd
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin 703b423
Merge branch 'main' into feat/wrap-cmd-setargs
levisyin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package testutil | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// ResetArgs resets arguments for the command. It is desired to be a helpful function for the cmd.SetArgs | ||
// in the case of calling multiple times in a unit test, as cmd.SetArgs doesn't | ||
// reset the flag value as expected. | ||
// | ||
// **Warning**: this is only compatible with following flag types: | ||
// 1. the implementations of pflag.Value | ||
// 2. the built-in implementations of pflag.SliceValue | ||
// 3. the custom implementations of pflag.SliceValue that are split by comma "," | ||
// | ||
// see https://github.com/spf13/cobra/issues/2079#issuecomment-1867991505 for more detail info | ||
func ResetArgs(cmd *cobra.Command) { | ||
// if flags haven't been parsed yet, there is no need to reset the args | ||
if !cmd.Flags().Parsed() { | ||
return | ||
} | ||
// If flags have already been parsed, we should reset the values of flags that haven't been set | ||
cmd.Flags().Visit(func(pf *pflag.Flag) { | ||
// if the flag hasn't been changed, there is no need to reset the args | ||
if !pf.Changed { | ||
return | ||
} | ||
// handle pflag.SliceValue | ||
if v, ok := pf.Value.(pflag.SliceValue); ok { | ||
defVal := strings.Trim(pf.DefValue, "[]") | ||
defSliceVal := make([]string, 0) | ||
if defVal != "" { | ||
defSliceVal = strings.Split(defVal, ",") | ||
} | ||
if err := v.Replace(defSliceVal); err != nil { | ||
panic(fmt.Errorf("error resetting argument <%s> with default value <%+v>: %v", pf.Name, defSliceVal, err)) | ||
} | ||
return | ||
} | ||
// handle pflag.Value | ||
if err := pf.Value.Set(pf.DefValue); err != nil { | ||
panic(fmt.Errorf("error resetting argument <%s> with default value <%s>: %v", pf.Name, pf.DefValue, err)) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package testutil_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/internal/testutil" | ||
) | ||
|
||
// TestSetArgsWithOriginalMethod is used to illustrate cobra.Command.SetArgs won't reset args as expected | ||
func TestSetArgsWithOriginalMethod(t *testing.T) { | ||
getCMD := func() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "testcmd", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
a, _ := cmd.Flags().GetBool("a") | ||
b, _ := cmd.Flags().GetBool("b") | ||
c, _ := cmd.Flags().GetBool("c") | ||
switch { | ||
case a && b, a && c, b && c: | ||
return fmt.Errorf("a,b,c only one could be true") | ||
} | ||
return nil | ||
}, | ||
} | ||
f := cmd.Flags() | ||
f.BoolP("a", "a", false, "a,b,c only one could be true") | ||
f.BoolP("b", "b", false, "a,b,c only one could be true") | ||
f.Bool("c", false, "a,b,c only one could be true") | ||
return cmd | ||
} | ||
|
||
cmd := getCMD() | ||
|
||
cmd.SetArgs([]string{ | ||
"testcmd", | ||
"--a=true", | ||
}) | ||
require.NoError(t, cmd.Execute()) | ||
|
||
// This call to cmd.SetArgs is expected to set only the 'b' flag. However, due to the bug, the 'a' flag remains set from the previous call to cmd.SetArgs, leading to an error. | ||
cmd.SetArgs([]string{ | ||
"testcmd", | ||
"--b=true", | ||
}) | ||
require.True(t, cmd.Flags().Changed("a")) | ||
require.Error(t, cmd.Execute()) | ||
|
||
// This call to cmd.SetArgs is expected to set only the 'c' flag. However, the 'a' and 'b' flags remain set from the previous calls, causing an unexpected error. | ||
cmd.SetArgs([]string{ | ||
"testcmd", | ||
"--c=true", | ||
}) | ||
require.Error(t, cmd.Execute()) | ||
|
||
// To work around the bug, we must explicitly reset the 'a' and 'b' flags to false, even though we only want to set the 'c' flag to true. | ||
cmd.SetArgs([]string{ | ||
"testcmd", | ||
"--a=false", | ||
"--b=false", | ||
"--c=true", | ||
}) | ||
require.NoError(t, cmd.Execute()) | ||
} | ||
|
||
func TestSetArgsWithWrappedMethod(t *testing.T) { | ||
var ( | ||
mockFlagWithCommaF = testutil.MockFlagsWithComma{Ary: []string{"g;m", "g;n"}} | ||
mockFlagWithCommaG testutil.MockFlagsWithComma | ||
) | ||
var ( | ||
mockFlagWithSemicolonH = testutil.MockFlagsWithSemicolon{Ary: []string{"g,m", "g,n"}} | ||
mockFlagWithSemicolonI testutil.MockFlagsWithSemicolon | ||
) | ||
getCMD := func() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "testcmd", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
}, | ||
} | ||
f := cmd.Flags() | ||
f.BoolP("a", "a", false, "check build-in pflag.Value") | ||
f.IntSlice("b", []int{1, 2}, "check build-in pflag.SliceValue with default value") | ||
f.IntSliceP("c", "c", nil, "check build-in pflag.SliceValue with nil default value") | ||
f.Var(&mockFlagWithCommaF, "d", "check custom implementation of pflag.SliceValue with splitting by comma and default value") | ||
f.VarP(&mockFlagWithCommaG, "e", "e", "check custom implementation of pflag.SliceValue with splitting by comma and nil default value") | ||
f.Var(&mockFlagWithSemicolonH, "f", "check custom implementation of pflag.SliceValue with splitting by semicolon and default value") | ||
f.VarP(&mockFlagWithSemicolonI, "g", "g", "check custom implementation of pflag.SliceValue with splitting by semicolon and nil default value") | ||
return cmd | ||
} | ||
|
||
cmd := getCMD() | ||
|
||
checkFlagsValue := func(cmd *cobra.Command, notDefaultFlags map[string]string) bool { | ||
require.NoError(t, cmd.Execute()) | ||
for _, k := range []string{"a", "b", "c", "d", "e", "f", "g"} { | ||
curVal := cmd.Flag(k).Value | ||
curDefVal := cmd.Flag(k).DefValue | ||
if v, ok := notDefaultFlags[k]; ok { | ||
require.NotEqual(t, curVal.String(), curDefVal, fmt.Sprintf("flag: %s, cmp_to: %v", k, curVal)) | ||
require.Equal(t, curVal.String(), v, fmt.Sprintf("flag: %s, cmp_to: %v", k, curVal)) | ||
} else { | ||
require.Equal(t, curVal.String(), curDefVal, fmt.Sprintf("flag: %s, cmp_to: %v", k, curVal)) | ||
} | ||
} | ||
return true | ||
} | ||
|
||
resetAndSetNewArgs := func(cmd *cobra.Command, args []string) { | ||
testutil.ResetArgs(cmd) | ||
cmd.SetArgs(args) | ||
} | ||
|
||
resetAndSetNewArgs(cmd, []string{ | ||
"testcmd", | ||
}) | ||
checkFlagsValue(cmd, nil) | ||
|
||
resetAndSetNewArgs(cmd, []string{ | ||
"testcmd", | ||
"--a=true", | ||
}) | ||
checkFlagsValue(cmd, map[string]string{"a": "true"}) | ||
|
||
resetAndSetNewArgs(cmd, []string{ | ||
"testcmd", | ||
"--b=3,4", | ||
}) | ||
checkFlagsValue(cmd, map[string]string{"b": "[3,4]"}) | ||
|
||
resetAndSetNewArgs(cmd, []string{ | ||
"testcmd", | ||
"--c=3,4", | ||
}) | ||
checkFlagsValue(cmd, map[string]string{"c": "[3,4]"}) | ||
|
||
resetAndSetNewArgs(cmd, []string{ | ||
"testcmd", | ||
"--d=g;n,g;m", | ||
}) | ||
checkFlagsValue(cmd, map[string]string{"d": "g;n,g;m"}) | ||
|
||
resetAndSetNewArgs(cmd, []string{ | ||
"testcmd", | ||
"--e=g;n,g;m", | ||
}) | ||
checkFlagsValue(cmd, map[string]string{"e": "g;n,g;m"}) | ||
|
||
resetAndSetNewArgs(cmd, []string{ | ||
"testcmd", | ||
"--f=g,n;g,m", | ||
}) | ||
checkFlagsValue(cmd, map[string]string{"f": "g,n;g,m"}) | ||
|
||
resetAndSetNewArgs(cmd, []string{ | ||
"testcmd", | ||
"--g=g,n;g,m", | ||
}) | ||
// custom implementation of pflag.SliceValue with splitting by semicolon is not compatible with testutil.SetArgs. | ||
// So `f` is changed to "g;m;g;n"(split to ["g", "m;g", "n"], and then join with ";"), not default value "g,m;g,n" | ||
checkFlagsValue(cmd, map[string]string{"f": "g;m;g;n", "g": "g,n;g,m"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package testutil | ||
|
||
import "strings" | ||
|
||
type MockFlagsWithComma struct { | ||
Ary []string | ||
changed bool | ||
} | ||
|
||
func (m *MockFlagsWithComma) String() string { | ||
return strings.Join(m.Ary, ",") | ||
} | ||
|
||
func (m *MockFlagsWithComma) Set(value string) error { | ||
if m.changed { | ||
m.Ary = append(m.Ary, strings.Split(value, ",")...) | ||
} else { | ||
m.Ary = strings.Split(value, ",") | ||
m.changed = true | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MockFlagsWithComma) Type() string { | ||
return "mock_flags" | ||
} | ||
|
||
func (m *MockFlagsWithComma) Replace(value []string) error { | ||
m.Ary = value | ||
return nil | ||
} | ||
|
||
func (m *MockFlagsWithComma) Append(value string) error { | ||
m.Ary = append(m.Ary, value) | ||
return nil | ||
} | ||
|
||
func (m *MockFlagsWithComma) GetSlice() []string { | ||
return m.Ary | ||
} | ||
|
||
type MockFlagsWithSemicolon struct { | ||
Ary []string | ||
changed bool | ||
} | ||
|
||
func (m *MockFlagsWithSemicolon) String() string { | ||
return strings.Join(m.Ary, ";") | ||
} | ||
|
||
func (m *MockFlagsWithSemicolon) Set(value string) error { | ||
if m.changed { | ||
m.Ary = append(m.Ary, strings.Split(value, ";")...) | ||
} else { | ||
m.Ary = strings.Split(value, ";") | ||
m.changed = true | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MockFlagsWithSemicolon) Type() string { | ||
return "mock_flags" | ||
} | ||
|
||
func (m *MockFlagsWithSemicolon) Replace(value []string) error { | ||
m.Ary = value | ||
return nil | ||
} | ||
|
||
func (m *MockFlagsWithSemicolon) Append(value string) error { | ||
m.Ary = append(m.Ary, value) | ||
return nil | ||
} | ||
|
||
func (m *MockFlagsWithSemicolon) GetSlice() []string { | ||
return m.Ary | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or
Args
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it means 'Array', not 'arg' or 'args', just for illustrate slice type field