From 26239bff2c5410cd04a604027374ae2d439ad70d Mon Sep 17 00:00:00 2001 From: levisyin Date: Sat, 23 Dec 2023 12:11:50 +0800 Subject: [PATCH 1/4] refactor: wrap cmd.SerArgs to fix bugs for cmd.SetArgs --- client/keys/show_test.go | 30 +++++----- testutil/cmd.go | 22 ++++++++ testutil/cmd_test.go | 118 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 testutil/cmd.go create mode 100644 testutil/cmd_test.go diff --git a/client/keys/show_test.go b/client/keys/show_test.go index 5d7f9c033263..33235b095465 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -69,10 +69,10 @@ func Test_runShowCmd(t *testing.T) { ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) - cmd.SetArgs([]string{"invalid"}) + testutil.SetArgs(cmd, []string{"invalid"}) require.EqualError(t, cmd.ExecuteContext(ctx), "invalid is not a valid name or address: decoding bech32 failed: invalid bech32 string length 7") - cmd.SetArgs([]string{"invalid1", "invalid2"}) + testutil.SetArgs(cmd, []string{"invalid1", "invalid2"}) require.EqualError(t, cmd.ExecuteContext(ctx), "invalid1 is not a valid name or address: decoding bech32 failed: invalid separator index 7") fakeKeyName1 := "runShowCmd_Key1" @@ -92,7 +92,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, err) // Now try single key - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=", FlagBechPrefix), @@ -100,7 +100,7 @@ func Test_runShowCmd(t *testing.T) { }) require.EqualError(t, cmd.ExecuteContext(ctx), "invalid Bech32 prefix encoding provided: ") - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -115,7 +115,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, err) addr, err := k.GetAddress() require.NoError(t, err) - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ addr.String(), fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -125,7 +125,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, cmd.ExecuteContext(ctx)) // Now try multisig key - set bech to acc - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -136,7 +136,7 @@ func Test_runShowCmd(t *testing.T) { // Now try multisig key duplicate _, mockOut := testutil.ApplyMockIO(cmd) - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName1, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -146,7 +146,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, cmd.ExecuteContext(ctx)) require.Contains(t, mockOut.String(), fmt.Sprintf("WARNING: duplicate keys found: %s", fakeKeyName1)) - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -156,7 +156,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, cmd.ExecuteContext(ctx)) // Now try multisig key - set bech to acc + threshold=2 - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=acc", FlagBechPrefix), @@ -166,7 +166,7 @@ func Test_runShowCmd(t *testing.T) { }) require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for accounts stored in devices") - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=val", FlagBechPrefix), @@ -176,7 +176,7 @@ func Test_runShowCmd(t *testing.T) { }) require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for accounts") - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=val", FlagBechPrefix), @@ -187,22 +187,18 @@ func Test_runShowCmd(t *testing.T) { }) require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for addresses not pubkeys") - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=true", FlagAddress), fmt.Sprintf("--%s=true", flagQRCode), - // we have to reset following flags as they were set to true above, and won't be auto reset to false if we skip to specify these flags. - // Note: this maybe a bug about spf13/cobra as cmd.flags's value won't be reset by changing cmd.args with cmd.SetArgs. - fmt.Sprintf("--%s=false", FlagDevice), - fmt.Sprintf("--%s=false", FlagPublicKey), fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest), }) // try fetch by name require.NoError(t, cmd.ExecuteContext(ctx)) - cmd.SetArgs([]string{ + testutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=true", FlagAddress), diff --git a/testutil/cmd.go b/testutil/cmd.go new file mode 100644 index 000000000000..70f2e1cc4494 --- /dev/null +++ b/testutil/cmd.go @@ -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) +} diff --git a/testutil/cmd_test.go b/testutil/cmd_test.go new file mode 100644 index 000000000000..d15a4bb7c86d --- /dev/null +++ b/testutil/cmd_test.go @@ -0,0 +1,118 @@ +package testutil_test + +import ( + "fmt" + "testing" + + "github.com/cosmos/cosmos-sdk/testutil" + "github.com/spf13/cobra" + "github.com/stretchr/testify/require" +) + +// 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()) + + // although we called cmd.SetArgs and expected that only b was set, but a has already set by last call `cmd.SetArgs` + cmd.SetArgs([]string{ + "testcmd", + "--b=true", + }) + require.True(t, cmd.Flags().Changed("a")) + require.Error(t, cmd.Execute()) + + // although we called cmd.SetArgs and expected that only c was set, but a,b has already set by last two call `cmd.SetArgs` + cmd.SetArgs([]string{ + "testcmd", + "--c=true", + }) + require.Error(t, cmd.Execute()) + + // we should explicitly set all args that has changed before, so that it works as we expected + 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()) +} From 1db5246df41f20a5e010862f53a39173eb377509 Mon Sep 17 00:00:00 2001 From: levisyin Date: Sat, 23 Dec 2023 13:38:32 +0800 Subject: [PATCH 2/4] fix ci lint --- testutil/cmd_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testutil/cmd_test.go b/testutil/cmd_test.go index d15a4bb7c86d..09e5c1a2d236 100644 --- a/testutil/cmd_test.go +++ b/testutil/cmd_test.go @@ -4,9 +4,10 @@ import ( "fmt" "testing" - "github.com/cosmos/cosmos-sdk/testutil" "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 From d75ef0889adf92c485e94896714af79994b89f82 Mon Sep 17 00:00:00 2001 From: levisyin <150114626+levisyin@users.noreply.github.com> Date: Sat, 23 Dec 2023 13:45:58 +0800 Subject: [PATCH 3/4] Update testutil/cmd_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- testutil/cmd_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testutil/cmd_test.go b/testutil/cmd_test.go index 09e5c1a2d236..53f3526f7bdc 100644 --- a/testutil/cmd_test.go +++ b/testutil/cmd_test.go @@ -41,7 +41,7 @@ func TestSetArgsWithOriginalMethod(t *testing.T) { }) require.NoError(t, cmd.Execute()) - // although we called cmd.SetArgs and expected that only b was set, but a has already set by last call `cmd.SetArgs` + // 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", @@ -49,14 +49,14 @@ func TestSetArgsWithOriginalMethod(t *testing.T) { require.True(t, cmd.Flags().Changed("a")) require.Error(t, cmd.Execute()) - // although we called cmd.SetArgs and expected that only c was set, but a,b has already set by last two call `cmd.SetArgs` + // 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()) - // we should explicitly set all args that has changed before, so that it works as we expected + // 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", From 246a2e08490a1061193aa02f52e4e214396fe3f3 Mon Sep 17 00:00:00 2001 From: levisyin Date: Sat, 23 Dec 2023 17:33:18 +0800 Subject: [PATCH 4/4] refactor: move to internal package --- client/keys/show_test.go | 27 +++++++++++---------- {testutil => internal/testutil}/cmd.go | 0 {testutil => internal/testutil}/cmd_test.go | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) rename {testutil => internal/testutil}/cmd.go (100%) rename {testutil => internal/testutil}/cmd_test.go (98%) diff --git a/client/keys/show_test.go b/client/keys/show_test.go index 33235b095465..b1c795fcd152 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -17,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + internaltestutil "github.com/cosmos/cosmos-sdk/internal/testutil" "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" @@ -69,10 +70,10 @@ func Test_runShowCmd(t *testing.T) { ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) - testutil.SetArgs(cmd, []string{"invalid"}) + internaltestutil.SetArgs(cmd, []string{"invalid"}) require.EqualError(t, cmd.ExecuteContext(ctx), "invalid is not a valid name or address: decoding bech32 failed: invalid bech32 string length 7") - testutil.SetArgs(cmd, []string{"invalid1", "invalid2"}) + internaltestutil.SetArgs(cmd, []string{"invalid1", "invalid2"}) require.EqualError(t, cmd.ExecuteContext(ctx), "invalid1 is not a valid name or address: decoding bech32 failed: invalid separator index 7") fakeKeyName1 := "runShowCmd_Key1" @@ -92,7 +93,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, err) // Now try single key - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=", FlagBechPrefix), @@ -100,7 +101,7 @@ func Test_runShowCmd(t *testing.T) { }) require.EqualError(t, cmd.ExecuteContext(ctx), "invalid Bech32 prefix encoding provided: ") - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -115,7 +116,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, err) addr, err := k.GetAddress() require.NoError(t, err) - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ addr.String(), fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -125,7 +126,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, cmd.ExecuteContext(ctx)) // Now try multisig key - set bech to acc - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -136,7 +137,7 @@ func Test_runShowCmd(t *testing.T) { // Now try multisig key duplicate _, mockOut := testutil.ApplyMockIO(cmd) - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName1, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -146,7 +147,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, cmd.ExecuteContext(ctx)) require.Contains(t, mockOut.String(), fmt.Sprintf("WARNING: duplicate keys found: %s", fakeKeyName1)) - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=%s", FlagBechPrefix, sdk.PrefixAccount), @@ -156,7 +157,7 @@ func Test_runShowCmd(t *testing.T) { require.NoError(t, cmd.ExecuteContext(ctx)) // Now try multisig key - set bech to acc + threshold=2 - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=acc", FlagBechPrefix), @@ -166,7 +167,7 @@ func Test_runShowCmd(t *testing.T) { }) require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for accounts stored in devices") - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=val", FlagBechPrefix), @@ -176,7 +177,7 @@ func Test_runShowCmd(t *testing.T) { }) require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for accounts") - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=val", FlagBechPrefix), @@ -187,7 +188,7 @@ func Test_runShowCmd(t *testing.T) { }) require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for addresses not pubkeys") - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=true", FlagAddress), @@ -198,7 +199,7 @@ func Test_runShowCmd(t *testing.T) { // try fetch by name require.NoError(t, cmd.ExecuteContext(ctx)) - testutil.SetArgs(cmd, []string{ + internaltestutil.SetArgs(cmd, []string{ fakeKeyName1, fakeKeyName2, fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), fmt.Sprintf("--%s=true", FlagAddress), diff --git a/testutil/cmd.go b/internal/testutil/cmd.go similarity index 100% rename from testutil/cmd.go rename to internal/testutil/cmd.go diff --git a/testutil/cmd_test.go b/internal/testutil/cmd_test.go similarity index 98% rename from testutil/cmd_test.go rename to internal/testutil/cmd_test.go index 53f3526f7bdc..c609d7e1331b 100644 --- a/testutil/cmd_test.go +++ b/internal/testutil/cmd_test.go @@ -7,7 +7,7 @@ import ( "github.com/spf13/cobra" "github.com/stretchr/testify/require" - "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/internal/testutil" ) // TestSetArgsWithOriginalMethod is used to illustrate cobra.Command.SetArgs won't reset args as expected