From 93766f54b9b72a0ada607371928820456ea91bf0 Mon Sep 17 00:00:00 2001 From: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> Date: Wed, 2 Nov 2022 23:55:53 +0530 Subject: [PATCH 1/3] fix: Fix Evidence CLI query flag parsing (#13458) (cherry picked from commit 709b4e2ca046a2c627926fb6e97f5cf398eb5866) # Conflicts: # x/evidence/client/cli/tx_test.go --- x/evidence/client/cli/query.go | 1 - x/evidence/client/cli/tx_test.go | 149 +++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 x/evidence/client/cli/tx_test.go diff --git a/x/evidence/client/cli/query.go b/x/evidence/client/cli/query.go index bf74325f949f..e40465a84b9a 100644 --- a/x/evidence/client/cli/query.go +++ b/x/evidence/client/cli/query.go @@ -32,7 +32,6 @@ $ %s query %s --page=2 --limit=50 ), ), Args: cobra.MaximumNArgs(1), - DisableFlagParsing: true, SuggestionsMinimumDistance: 2, RunE: QueryEvidenceCmd(), } diff --git a/x/evidence/client/cli/tx_test.go b/x/evidence/client/cli/tx_test.go new file mode 100644 index 000000000000..a7d626bf0e24 --- /dev/null +++ b/x/evidence/client/cli/tx_test.go @@ -0,0 +1,149 @@ +package cli_test + +import ( + "bytes" + "context" + "fmt" + "io" + "strings" + "testing" + + "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + tmbytes "github.com/tendermint/tendermint/libs/bytes" + rpcclient "github.com/tendermint/tendermint/rpc/client" + rpcclientmock "github.com/tendermint/tendermint/rpc/client/mock" + coretypes "github.com/tendermint/tendermint/rpc/core/types" + tmtypes "github.com/tendermint/tendermint/types" + + "github.com/cosmos/cosmos-sdk/client/flags" + svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/evidence" + "github.com/cosmos/cosmos-sdk/x/evidence/client/cli" +) + +var _ client.TendermintRPC = (*mockTendermintRPC)(nil) + +type mockTendermintRPC struct { + rpcclientmock.Client + + responseQuery abci.ResponseQuery +} + +func newMockTendermintRPC(respQuery abci.ResponseQuery) mockTendermintRPC { + return mockTendermintRPC{responseQuery: respQuery} +} + +func (mockTendermintRPC) BroadcastTxSync(_ context.Context, _ tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) { + return &coretypes.ResultBroadcastTx{}, nil +} + +func (m mockTendermintRPC) ABCIQueryWithOptions( + _ context.Context, + _ string, _ tmbytes.HexBytes, + _ rpcclient.ABCIQueryOptions, +) (*coretypes.ResultABCIQuery, error) { + return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil +} + +func TestGetQueryCmd(t *testing.T) { + cmd := cli.GetQueryCmd() + cmd.SetOut(io.Discard) + require.NotNil(t, cmd) + + encCfg := testutilmod.MakeTestEncodingConfig(evidence.AppModuleBasic{}) + kr := keyring.NewInMemory(encCfg.Codec) + baseCtx := client.Context{}. + WithKeyring(kr). + WithTxConfig(encCfg.TxConfig). + WithCodec(encCfg.Codec). + WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}). + WithAccountRetriever(client.MockAccountRetriever{}). + WithOutput(io.Discard). + WithChainID("test-chain") + + testCases := map[string]struct { + args []string + ctxGen func() client.Context + expCmdOutput string + expectedOutput string + expectErr bool + }{ + "non-existent evidence": { + []string{"DF0C23E8634E480F84B9D5674A7CDC9816466DEC28A3358F73260F68D28D7660"}, + func() client.Context { + bz, _ := encCfg.Codec.Marshal(&sdk.TxResponse{}) + c := newMockTendermintRPC(abci.ResponseQuery{ + Value: bz, + }) + return baseCtx.WithClient(c) + }, + "DF0C23E8634E480F84B9D5674A7CDC9816466DEC28A3358F73260F68D28D7660", + "", + true, + }, + "all evidence (default pagination)": { + []string{}, + func() client.Context { + bz, _ := encCfg.Codec.Marshal(&sdk.TxResponse{}) + c := newMockTendermintRPC(abci.ResponseQuery{ + Value: bz, + }) + return baseCtx.WithClient(c) + }, + "", + "evidence: []\npagination: null", + false, + }, + "all evidence (json output)": { + []string{ + fmt.Sprintf("--%s=json", flags.FlagOutput), + }, + func() client.Context { + bz, _ := encCfg.Codec.Marshal(&sdk.TxResponse{}) + c := newMockTendermintRPC(abci.ResponseQuery{ + Value: bz, + }) + return baseCtx.WithClient(c) + }, + "", + `{"evidence":[],"pagination":null}`, + false, + }, + } + + for name, tc := range testCases { + tc := tc + + t.Run(name, func(t *testing.T) { + var outBuf bytes.Buffer + + clientCtx := tc.ctxGen().WithOutput(&outBuf) + ctx := svrcmd.CreateExecuteContext(context.Background()) + + cmd.SetContext(ctx) + cmd.SetArgs(tc.args) + + require.NoError(t, client.SetCmdClientContextHandler(clientCtx, cmd)) + + if len(tc.args) != 0 { + require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput) + } + + out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.args) + if tc.expectErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } + require.Contains(t, fmt.Sprint(cmd), "evidence [] [] Query for evidence by hash or for all (paginated) submitted evidence") + require.Contains(t, strings.TrimSpace(out.String()), tc.expectedOutput) + }) + } +} From a88036d7dfd841ba6fedb3f792be89190493a78b Mon Sep 17 00:00:00 2001 From: HuangYi Date: Fri, 27 Jan 2023 12:31:47 +0800 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf9de76481cf..855e81ddfabd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Bug Fixes + +- (cli) [#14799](https://github.com/cosmos/cosmos-sdk/pull/14799) Fix Evidence CLI query flag parsing (backport #13458) + ## [v0.46.8](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.8) - 2022-01-23 ### Improvements From 0051ab87503f3d33629ca1af893d2e2ec4ed2b7b Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Fri, 27 Jan 2023 11:18:29 +0100 Subject: [PATCH 3/3] remove test --- x/evidence/client/cli/tx_test.go | 149 ------------------------------- 1 file changed, 149 deletions(-) delete mode 100644 x/evidence/client/cli/tx_test.go diff --git a/x/evidence/client/cli/tx_test.go b/x/evidence/client/cli/tx_test.go deleted file mode 100644 index a7d626bf0e24..000000000000 --- a/x/evidence/client/cli/tx_test.go +++ /dev/null @@ -1,149 +0,0 @@ -package cli_test - -import ( - "bytes" - "context" - "fmt" - "io" - "strings" - "testing" - - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - tmbytes "github.com/tendermint/tendermint/libs/bytes" - rpcclient "github.com/tendermint/tendermint/rpc/client" - rpcclientmock "github.com/tendermint/tendermint/rpc/client/mock" - coretypes "github.com/tendermint/tendermint/rpc/core/types" - tmtypes "github.com/tendermint/tendermint/types" - - "github.com/cosmos/cosmos-sdk/client/flags" - svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - sdk "github.com/cosmos/cosmos-sdk/types" - testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil" - "github.com/cosmos/cosmos-sdk/x/evidence" - "github.com/cosmos/cosmos-sdk/x/evidence/client/cli" -) - -var _ client.TendermintRPC = (*mockTendermintRPC)(nil) - -type mockTendermintRPC struct { - rpcclientmock.Client - - responseQuery abci.ResponseQuery -} - -func newMockTendermintRPC(respQuery abci.ResponseQuery) mockTendermintRPC { - return mockTendermintRPC{responseQuery: respQuery} -} - -func (mockTendermintRPC) BroadcastTxSync(_ context.Context, _ tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) { - return &coretypes.ResultBroadcastTx{}, nil -} - -func (m mockTendermintRPC) ABCIQueryWithOptions( - _ context.Context, - _ string, _ tmbytes.HexBytes, - _ rpcclient.ABCIQueryOptions, -) (*coretypes.ResultABCIQuery, error) { - return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil -} - -func TestGetQueryCmd(t *testing.T) { - cmd := cli.GetQueryCmd() - cmd.SetOut(io.Discard) - require.NotNil(t, cmd) - - encCfg := testutilmod.MakeTestEncodingConfig(evidence.AppModuleBasic{}) - kr := keyring.NewInMemory(encCfg.Codec) - baseCtx := client.Context{}. - WithKeyring(kr). - WithTxConfig(encCfg.TxConfig). - WithCodec(encCfg.Codec). - WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}). - WithAccountRetriever(client.MockAccountRetriever{}). - WithOutput(io.Discard). - WithChainID("test-chain") - - testCases := map[string]struct { - args []string - ctxGen func() client.Context - expCmdOutput string - expectedOutput string - expectErr bool - }{ - "non-existent evidence": { - []string{"DF0C23E8634E480F84B9D5674A7CDC9816466DEC28A3358F73260F68D28D7660"}, - func() client.Context { - bz, _ := encCfg.Codec.Marshal(&sdk.TxResponse{}) - c := newMockTendermintRPC(abci.ResponseQuery{ - Value: bz, - }) - return baseCtx.WithClient(c) - }, - "DF0C23E8634E480F84B9D5674A7CDC9816466DEC28A3358F73260F68D28D7660", - "", - true, - }, - "all evidence (default pagination)": { - []string{}, - func() client.Context { - bz, _ := encCfg.Codec.Marshal(&sdk.TxResponse{}) - c := newMockTendermintRPC(abci.ResponseQuery{ - Value: bz, - }) - return baseCtx.WithClient(c) - }, - "", - "evidence: []\npagination: null", - false, - }, - "all evidence (json output)": { - []string{ - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - func() client.Context { - bz, _ := encCfg.Codec.Marshal(&sdk.TxResponse{}) - c := newMockTendermintRPC(abci.ResponseQuery{ - Value: bz, - }) - return baseCtx.WithClient(c) - }, - "", - `{"evidence":[],"pagination":null}`, - false, - }, - } - - for name, tc := range testCases { - tc := tc - - t.Run(name, func(t *testing.T) { - var outBuf bytes.Buffer - - clientCtx := tc.ctxGen().WithOutput(&outBuf) - ctx := svrcmd.CreateExecuteContext(context.Background()) - - cmd.SetContext(ctx) - cmd.SetArgs(tc.args) - - require.NoError(t, client.SetCmdClientContextHandler(clientCtx, cmd)) - - if len(tc.args) != 0 { - require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput) - } - - out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.args) - if tc.expectErr { - require.Error(t, err) - } else { - require.NoError(t, err) - } - require.Contains(t, fmt.Sprint(cmd), "evidence [] [] Query for evidence by hash or for all (paginated) submitted evidence") - require.Contains(t, strings.TrimSpace(out.String()), tc.expectedOutput) - }) - } -}