From e357d5df16070f998022b1f4de57623e700f7f35 Mon Sep 17 00:00:00 2001 From: islishude Date: Sun, 12 May 2024 11:43:19 +0800 Subject: [PATCH 1/3] feat(client): overwrite client context instead of setting new one --- client/cmd.go | 13 ++++++++----- client/cmd_test.go | 19 ++++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/client/cmd.go b/client/cmd.go index e817649d24dc..d4f0693d7e4a 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -359,14 +359,17 @@ func GetClientContextFromCmd(cmd *cobra.Command) Context { // SetCmdClientContext sets a command's Context value to the provided argument. // If the context has not been set, set the given context as the default. func SetCmdClientContext(cmd *cobra.Command, clientCtx Context) error { - var cmdCtx context.Context - - if cmd.Context() == nil { + cmdCtx := cmd.Context() + if cmdCtx == nil { cmdCtx = context.Background() + } + + v := cmd.Context().Value(ClientContextKey) + if clientCtxPtr, ok := v.(*Context); ok { + *clientCtxPtr = clientCtx } else { - cmdCtx = cmd.Context() + cmd.SetContext(context.WithValue(cmdCtx, ClientContextKey, &clientCtx)) } - cmd.SetContext(context.WithValue(cmdCtx, ClientContextKey, &clientCtx)) return nil } diff --git a/client/cmd_test.go b/client/cmd_test.go index 81d5719ccfb6..0de14f0a7391 100644 --- a/client/cmd_test.go +++ b/client/cmd_test.go @@ -75,15 +75,19 @@ func TestSetCmdClientContextHandler(t *testing.T) { return c } + defaultContext := context.WithValue(context.Background(), client.ClientContextKey, &client.Context{}) + testCases := []struct { name string expectedContext client.Context args []string + ctx context.Context }{ { "no flags set", initClientCtx, []string{}, + defaultContext, }, { "flags set", @@ -91,6 +95,7 @@ func TestSetCmdClientContextHandler(t *testing.T) { []string{ fmt.Sprintf("--%s=new-chain-id", flags.FlagChainID), }, + defaultContext, }, { "flags set with space", @@ -99,6 +104,16 @@ func TestSetCmdClientContextHandler(t *testing.T) { fmt.Sprintf("--%s", flags.FlagHome), "/tmp/dir", }, + defaultContext, + }, + { + "no context provided", + initClientCtx.WithHomeDir("/tmp/noctx"), + []string{ + fmt.Sprintf("--%s", flags.FlagHome), + "/tmp/noctx", + }, + nil, }, } @@ -106,13 +121,11 @@ func TestSetCmdClientContextHandler(t *testing.T) { tc := tc t.Run(tc.name, func(t *testing.T) { - ctx := context.WithValue(context.Background(), client.ClientContextKey, &client.Context{}) - cmd := newCmd() _ = testutil.ApplyMockIODiscardOutErr(cmd) cmd.SetArgs(tc.args) - require.NoError(t, cmd.ExecuteContext(ctx)) + require.NoError(t, cmd.ExecuteContext(tc.ctx)) clientCtx := client.GetClientContextFromCmd(cmd) require.Equal(t, tc.expectedContext, clientCtx) From a11247129975c29f42e26ebeea843955f24d8867 Mon Sep 17 00:00:00 2001 From: islishude Date: Sun, 12 May 2024 18:50:33 +0800 Subject: [PATCH 2/3] test with invalid client in the context --- client/cmd_test.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/client/cmd_test.go b/client/cmd_test.go index 0de14f0a7391..559d31d39f40 100644 --- a/client/cmd_test.go +++ b/client/cmd_test.go @@ -75,8 +75,6 @@ func TestSetCmdClientContextHandler(t *testing.T) { return c } - defaultContext := context.WithValue(context.Background(), client.ClientContextKey, &client.Context{}) - testCases := []struct { name string expectedContext client.Context @@ -87,7 +85,7 @@ func TestSetCmdClientContextHandler(t *testing.T) { "no flags set", initClientCtx, []string{}, - defaultContext, + context.WithValue(context.Background(), client.ClientContextKey, &client.Context{}), }, { "flags set", @@ -95,7 +93,7 @@ func TestSetCmdClientContextHandler(t *testing.T) { []string{ fmt.Sprintf("--%s=new-chain-id", flags.FlagChainID), }, - defaultContext, + context.WithValue(context.Background(), client.ClientContextKey, &client.Context{}), }, { "flags set with space", @@ -104,7 +102,7 @@ func TestSetCmdClientContextHandler(t *testing.T) { fmt.Sprintf("--%s", flags.FlagHome), "/tmp/dir", }, - defaultContext, + context.Background(), }, { "no context provided", @@ -115,6 +113,15 @@ func TestSetCmdClientContextHandler(t *testing.T) { }, nil, }, + { + "with invalid client value in the context", + initClientCtx.WithHomeDir("/tmp/invalid"), + []string{ + fmt.Sprintf("--%s", flags.FlagHome), + "/tmp/invalid", + }, + context.WithValue(context.Background(), client.ClientContextKey, "invalid"), + }, } for _, tc := range testCases { From 34f4be9c67a5fd3c5787d4a89652580531f67306 Mon Sep 17 00:00:00 2001 From: islishude Date: Tue, 14 May 2024 21:28:52 +0800 Subject: [PATCH 3/3] Rebase and update change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e8b308835fe..5489e3bc0566 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i * (crypto/keyring) [#20212](https://github.com/cosmos/cosmos-sdk/pull/20212) Expose the db keyring used in the keystore. * (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis * (debug) [#20328](https://github.com/cosmos/cosmos-sdk/pull/20328) Add consensus address for debug cmd. +* (client) [#20356](https://github.com/cosmos/cosmos-sdk/pull/20356) Overwrite client context instead of setting new one ### Improvements