-
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 #18876
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
26239bf
refactor: wrap cmd.SerArgs to fix bugs for cmd.SetArgs
levisyin 1db5246
fix ci lint
levisyin d75ef08
Update testutil/cmd_test.go
levisyin 246a2e0
refactor: move to internal package
levisyin f388f4e
Merge branch 'main' into refactor/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
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,22 @@ | ||
package testutil | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// SetArgs sets arguments for the command. It is desired to replace the cmd.SetArgs in all test case, as cmd.SetArgs doesn't reset flag value as expected. | ||
// | ||
// see https://github.com/spf13/cobra/issues/2079#issuecomment-1867991505 for more detail info | ||
func SetArgs(cmd *cobra.Command, args []string) { | ||
if cmd.Flags().Parsed() { | ||
cmd.Flags().Visit(func(pf *pflag.Flag) { | ||
if err := pf.Value.Set(pf.DefValue); err != nil { | ||
panic(fmt.Errorf("reset argument[%s] value error %v", pf.Name, err)) | ||
} | ||
}) | ||
} | ||
cmd.SetArgs(args) | ||
} | ||
levisyin marked this conversation as resolved.
Show resolved
Hide resolved
|
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,119 @@ | ||
package testutil_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/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) { | ||
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() | ||
|
||
testutil.SetArgs(cmd, []string{ | ||
"testcmd", | ||
"--a=true", | ||
}) | ||
require.NoError(t, cmd.Execute()) | ||
|
||
testutil.SetArgs(cmd, []string{ | ||
"testcmd", | ||
"--b=true", | ||
}) | ||
require.True(t, cmd.Flags().Changed("a")) | ||
require.NoError(t, cmd.Execute()) | ||
|
||
testutil.SetArgs(cmd, []string{ | ||
"testcmd", | ||
"--c=true", | ||
}) | ||
require.NoError(t, cmd.Execute()) | ||
|
||
testutil.SetArgs(cmd, []string{ | ||
"testcmd", | ||
"--a=false", | ||
"--b=false", | ||
"--c=true", | ||
}) | ||
require.NoError(t, cmd.Execute()) | ||
} |
Oops, something went wrong.
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.
We should be mindful of new public api we add. Can we maybe have this in an internal package?
Concerning using it everywhere in the sdk, we should really really watch out that we don't increase the reliance on the sdk for already extracted modules.
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.
@julienrbrt Hi sir, I have moved it to
internal/testutil