forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(client): wrap cmd.SetArgs to fix bugs for cmd.SetArgs (cosmos#18876
- Loading branch information
1 parent
936f488
commit ae19acc
Showing
3 changed files
with
155 additions
and
17 deletions.
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) | ||
} |
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/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) { | ||
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()) | ||
} |