Skip to content

Commit

Permalink
refactor: remove dependency on simapp from client tests (cosmos#12545)
Browse files Browse the repository at this point in the history
* client/keys tests passing

* refactor into testutil package

* refactor into testutil package

* refactor client/tx off of simapp dep

* One more tx usage

* attention on ledger tests
  • Loading branch information
kocubinski authored Jul 12, 2022
1 parent 9f95534 commit 203d563
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 54 deletions.
12 changes: 6 additions & 6 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -44,7 +44,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
// Prepare a keybase
kbHome := t.TempDir()

cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)
clientCtx := client.Context{}.WithKeyringDir(kbHome).WithCodec(cdc)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

Expand Down Expand Up @@ -96,9 +96,9 @@ func Test_runAddCmdLedger(t *testing.T) {

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()
encCfg := simapp.MakeTestEncodingConfig()
cdc := clienttestutil.MakeTestCodec(t)

clientCtx := client.Context{}.WithKeyringDir(kbHome).WithCodec(encCfg.Codec)
clientCtx := client.Context{}.WithKeyringDir(kbHome).WithCodec(cdc)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

cmd.SetArgs([]string{
Expand All @@ -114,7 +114,7 @@ func Test_runAddCmdLedger(t *testing.T) {
require.NoError(t, cmd.ExecuteContext(ctx))

// Now check that it has been stored properly
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, encCfg.Codec)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)

// Now check that it has been stored properly
Expand All @@ -137,7 +137,7 @@ func Test_runAddCmdLedger(t *testing.T) {
}

func Test_runAddCmdLedgerDryRun(t *testing.T) {
cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)
testData := []struct {
name string
args []string
Expand Down
8 changes: 4 additions & 4 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -28,7 +28,7 @@ func Test_runAddCmdBasic(t *testing.T) {
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()

cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)
Expand Down Expand Up @@ -124,7 +124,7 @@ func Test_runAddCmdBasic(t *testing.T) {
func Test_runAddCmdDryRun(t *testing.T) {
pubkey1 := `{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtObiFVE4s+9+RX5SP8TN9r2mxpoaT4eGj9CJfK7VRzN"}`
pubkey2 := `{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A/se1vkqgdQ7VJQCM4mxN+L+ciGhnnJ4XYsQCRBMrdRi"}`
cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)

testData := []struct {
name string
Expand Down Expand Up @@ -235,7 +235,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
func TestAddRecoverFileBackend(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()
Expand Down
4 changes: 2 additions & 2 deletions client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -34,7 +34,7 @@ func Test_runDeleteCmd(t *testing.T) {
fakeKeyName2 := "runDeleteCmd_Key2"

path := sdk.GetConfig().GetFullBIP44Path()
cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)

cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome)})
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
Expand Down
4 changes: 2 additions & 2 deletions client/keys/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runExportCmd(t *testing.T) {
cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)
testCases := []struct {
name string
keyringBackend string
Expand Down
4 changes: 2 additions & 2 deletions client/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_runImportCmd(t *testing.T) {
cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)
testCases := []struct {
name string
keyringBackend string
Expand Down
6 changes: 3 additions & 3 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -35,8 +35,8 @@ func Test_runListCmd(t *testing.T) {
kbHome2 := t.TempDir()

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
encCfg := simapp.MakeTestEncodingConfig()
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn, encCfg.Codec)
cdc := clienttestutil.MakeTestCodec(t)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn, cdc)
require.NoError(t, err)

clientCtx := client.Context{}.WithKeyring(kb)
Expand Down
4 changes: 2 additions & 2 deletions client/keys/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
)

Expand All @@ -36,7 +36,7 @@ type MigrateTestSuite struct {

func (s *MigrateTestSuite) SetupSuite() {
s.dir = s.T().TempDir()
s.cdc = simapp.MakeTestEncodingConfig().Codec
s.cdc = clienttestutil.MakeTestCodec(s.T())
s.appName = "cosmos"
s.priv = cryptotypes.PrivKey(secp256k1.GenPrivKey())
s.pub = s.priv.PubKey()
Expand Down
4 changes: 2 additions & 2 deletions client/keys/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -32,7 +32,7 @@ func Test_runRenameCmd(t *testing.T) {

path := sdk.GetConfig().GetFullBIP44Path()

cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -53,7 +53,7 @@ func Test_runShowCmd(t *testing.T) {
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)

kbHome := t.TempDir()
cdc := simapp.MakeTestEncodingConfig().Codec
cdc := clienttestutil.MakeTestCodec(t)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)

Expand Down
32 changes: 32 additions & 0 deletions client/testutil/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package testutil

import (
"testing"

"github.com/stretchr/testify/require"

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/core/appconfig"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/codec"
_ "github.com/cosmos/cosmos-sdk/runtime"
)

var TestConfig = appconfig.Compose(&appv1alpha1.Config{
Modules: []*appv1alpha1.ModuleConfig{
{
Name: "runtime",
Config: appconfig.WrapAny(&runtimev1alpha1.Module{
AppName: "clientTest",
}),
},
},
})

func MakeTestCodec(t *testing.T) codec.Codec {
var cdc codec.Codec
err := depinject.Inject(TestConfig, &cdc)
require.NoError(t, err)
return cdc
}
22 changes: 17 additions & 5 deletions client/tx/aux_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@ import (

"github.com/stretchr/testify/require"

_ "cosmossdk.io/api/cosmos/bank/v1beta1"
"cosmossdk.io/depinject"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
typestx "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/bank"
)

func TestAuxTxBuilder(t *testing.T) {
encCfg := simapp.MakeTestEncodingConfig()
testdata.RegisterInterfaces(encCfg.InterfaceRegistry)
var (
reg codectypes.InterfaceRegistry
cdc codec.Codec
)
err := depinject.Inject(clienttestutil.TestConfig, &reg, &cdc)
bankModule := bank.AppModuleBasic{}

require.NoError(t, err)
testdata.RegisterInterfaces(reg)
// required for test case: "GetAuxSignerData works for DIRECT_AUX"
bankModule.RegisterInterfaces(reg)

var b tx.AuxTxBuilder

Expand Down Expand Up @@ -159,7 +171,7 @@ func TestAuxTxBuilder(t *testing.T) {
auxSignerData, err := b.GetAuxSignerData()

// Make sure auxSignerData is correctly populated
checkCorrectData(t, encCfg.Codec, auxSignerData, signing.SignMode_SIGN_MODE_DIRECT_AUX)
checkCorrectData(t, cdc, auxSignerData, signing.SignMode_SIGN_MODE_DIRECT_AUX)

return err
},
Expand Down Expand Up @@ -202,7 +214,7 @@ func TestAuxTxBuilder(t *testing.T) {
auxSignerData, err := b.GetAuxSignerData()

// Make sure auxSignerData is correctly populated
checkCorrectData(t, encCfg.Codec, auxSignerData, signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON)
checkCorrectData(t, cdc, auxSignerData, signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON)

return err
},
Expand Down
27 changes: 17 additions & 10 deletions client/tx/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/client"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
tx2 "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/params"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types"
typestx "github.com/cosmos/cosmos-sdk/types/tx"
Expand Down Expand Up @@ -58,16 +59,22 @@ func buildTestTx(t *testing.T, builder client.TxBuilder) {

type TestSuite struct {
suite.Suite
encCfg params.EncodingConfig
codec codec.Codec
amino *codec.LegacyAmino
protoCfg client.TxConfig
aminoCfg client.TxConfig
}

func (s *TestSuite) SetupSuite() {
encCfg := simapp.MakeTestEncodingConfig()
s.encCfg = encCfg
s.protoCfg = tx.NewTxConfig(codec.NewProtoCodec(encCfg.InterfaceRegistry), tx.DefaultSignModes)
s.aminoCfg = legacytx.StdTxConfig{Cdc: encCfg.Amino}
var (
reg codectypes.InterfaceRegistry
amino *codec.LegacyAmino
)
err := depinject.Inject(clienttestutil.TestConfig, &reg, &amino)
require.NoError(s.T(), err)

s.protoCfg = tx.NewTxConfig(codec.NewProtoCodec(reg), tx.DefaultSignModes)
s.aminoCfg = legacytx.StdTxConfig{Cdc: amino}
}

func (s *TestSuite) TestCopyTx() {
Expand Down Expand Up @@ -114,7 +121,7 @@ func (s *TestSuite) TestConvertTxToStdTx() {
// proto tx
protoBuilder := s.protoCfg.NewTxBuilder()
buildTestTx(s.T(), protoBuilder)
stdTx, err := tx2.ConvertTxToStdTx(s.encCfg.Amino, protoBuilder.GetTx())
stdTx, err := tx2.ConvertTxToStdTx(s.amino, protoBuilder.GetTx())
s.Require().NoError(err)
s.Require().Equal(memo, stdTx.Memo)
s.Require().Equal(gas, stdTx.Fee.Gas)
Expand All @@ -134,7 +141,7 @@ func (s *TestSuite) TestConvertTxToStdTx() {
},
})
s.Require().NoError(err)
stdTx, err = tx2.ConvertTxToStdTx(s.encCfg.Amino, protoBuilder.GetTx())
stdTx, err = tx2.ConvertTxToStdTx(s.amino, protoBuilder.GetTx())
s.Require().NoError(err)
s.Require().Equal(memo, stdTx.Memo)
s.Require().Equal(gas, stdTx.Fee.Gas)
Expand All @@ -148,7 +155,7 @@ func (s *TestSuite) TestConvertTxToStdTx() {
aminoBuilder := s.aminoCfg.NewTxBuilder()
buildTestTx(s.T(), aminoBuilder)
stdTx = aminoBuilder.GetTx().(legacytx.StdTx)
stdTx2, err := tx2.ConvertTxToStdTx(s.encCfg.Amino, stdTx)
stdTx2, err := tx2.ConvertTxToStdTx(s.amino, stdTx)
s.Require().NoError(err)
s.Require().Equal(stdTx, stdTx2)
}
Expand Down
Loading

0 comments on commit 203d563

Please sign in to comment.