refactor: remove dependency on simapp from client tests (#12545)

* 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
This commit is contained in:
Matt Kocubinski 2022-07-12 18:43:02 -05:00 committed by GitHub
parent 9f9553484b
commit 203d563074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 112 additions and 54 deletions

View File

@ -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"
)
@ -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)
@ -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{
@ -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
@ -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

View File

@ -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"
@ -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)
@ -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
@ -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()

View File

@ -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"
@ -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)

View File

@ -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

View File

@ -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

View File

@ -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"
@ -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)

View File

@ -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"
)
@ -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()

View File

@ -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"
@ -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)

View File

@ -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"
@ -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)

32
client/testutil/util.go Normal file
View File

@ -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
}

View File

@ -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
@ -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
},
@ -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
},

View File

@ -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"
@ -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() {
@ -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)
@ -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)
@ -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)
}

View File

@ -8,22 +8,30 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/client"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func NewTestTxConfig() client.TxConfig {
cfg := simapp.MakeTestEncodingConfig()
return cfg.TxConfig
func newTestTxConfig(t *testing.T) (client.TxConfig, codec.Codec) {
var (
pcdc codec.ProtoCodecMarshaler
cdc codec.Codec
)
err := depinject.Inject(clienttestutil.TestConfig, &pcdc, &cdc)
require.NoError(t, err)
return authtx.NewTxConfig(pcdc, authtx.DefaultSignModes), cdc
}
// mockContext is a mock client.Context to return abitrary simulation response, used to
@ -70,7 +78,7 @@ func TestCalculateGas(t *testing.T) {
for _, tc := range testCases {
stc := tc
txCfg := NewTestTxConfig()
txCfg, _ := newTestTxConfig(t)
txf := tx.Factory{}.
WithChainID("test-chain").
@ -96,10 +104,9 @@ func TestCalculateGas(t *testing.T) {
}
func TestBuildSimTx(t *testing.T) {
txCfg := NewTestTxConfig()
encCfg := simapp.MakeTestEncodingConfig()
txCfg, cdc := newTestTxConfig(t)
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, encCfg.Codec)
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, cdc)
require.NoError(t, err)
path := hd.CreateHDPath(118, 0, 0).String()
@ -123,8 +130,8 @@ func TestBuildSimTx(t *testing.T) {
}
func TestBuildUnsignedTx(t *testing.T) {
encCfg := simapp.MakeTestEncodingConfig()
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, encCfg.Codec)
txConfig, cdc := newTestTxConfig(t)
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, cdc)
require.NoError(t, err)
path := hd.CreateHDPath(118, 0, 0).String()
@ -133,7 +140,7 @@ func TestBuildUnsignedTx(t *testing.T) {
require.NoError(t, err)
txf := tx.Factory{}.
WithTxConfig(NewTestTxConfig()).
WithTxConfig(txConfig).
WithAccountNumber(50).
WithSequence(23).
WithFees("50stake").
@ -152,10 +159,10 @@ func TestBuildUnsignedTx(t *testing.T) {
}
func TestSign(t *testing.T) {
txConfig, cdc := newTestTxConfig(t)
requireT := require.New(t)
path := hd.CreateHDPath(118, 0, 0).String()
encCfg := simapp.MakeTestEncodingConfig()
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, encCfg.Codec)
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, cdc)
requireT.NoError(err)
from1 := "test_key1"
@ -179,7 +186,7 @@ func TestSign(t *testing.T) {
t.Log("Pub keys:", pubKey1, pubKey2)
txfNoKeybase := tx.Factory{}.
WithTxConfig(NewTestTxConfig()).
WithTxConfig(txConfig).
WithAccountNumber(50).
WithSequence(23).
WithFees("50stake").