Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Oct 4, 2023
1 parent a527e05 commit 77f5068
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 400 deletions.
86 changes: 84 additions & 2 deletions client/v2/autocli/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"google.golang.org/protobuf/reflect/protoregistry"
"gotest.tools/v3/assert"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv2alpha1 "cosmossdk.io/api/cosmos/base/reflection/v2alpha1"
"cosmossdk.io/client/v2/autocli/flag"
"cosmossdk.io/client/v2/internal/testpb"
Expand All @@ -22,6 +23,8 @@ import (
sdkkeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/bank"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

type fixture struct {
Expand All @@ -47,15 +50,15 @@ func initFixture(t *testing.T) *fixture {
clientConn, err := grpc.Dial(listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NilError(t, err)

encodingConfig := moduletestutil.MakeTestEncodingConfig()
encodingConfig := moduletestutil.MakeTestEncodingConfig(bank.AppModuleBasic{})
kr, err := sdkkeyring.New(sdk.KeyringServiceName(), sdkkeyring.BackendMemory, home, nil, encodingConfig.Codec)
assert.NilError(t, err)

akr, err := sdkkeyring.NewAutoCLIKeyring(kr)
assert.NilError(t, err)

interfaceRegistry := encodingConfig.Codec.InterfaceRegistry()
interfaceRegistry.RegisterInterface(sdk.MsgTypeURL(&testpb.MsgRequest{}), (*sdk.Msg)(nil), &testpb.MsgRequest{})
banktypes.RegisterInterfaces(interfaceRegistry)

var initClientCtx client.Context
initClientCtx = initClientCtx.
Expand Down Expand Up @@ -144,3 +147,82 @@ func (t testEchoServer) Echo(_ context.Context, request *testpb.EchoRequest) (*t
}

var _ testpb.QueryServer = testEchoServer{}

func TestEnhanceCommand(t *testing.T) {
b := &Builder{}
// Test that the command has a subcommand
cmd := &cobra.Command{Use: "test"}
cmd.AddCommand(&cobra.Command{Use: "test"})

for i := 0; i < 2; i++ {
cmdTp := cmdType(i)

appOptions := AppOptions{
ModuleOptions: map[string]*autocliv1.ModuleOptions{
"test": {},
},
}

err := b.enhanceCommandCommon(cmd, cmdTp, appOptions, map[string]*cobra.Command{})
assert.NilError(t, err)

cmd = &cobra.Command{Use: "test"}

appOptions = AppOptions{
ModuleOptions: map[string]*autocliv1.ModuleOptions{},
}
customCommands := map[string]*cobra.Command{
"test2": {Use: "test"},
}
err = b.enhanceCommandCommon(cmd, cmdTp, appOptions, customCommands)
assert.NilError(t, err)

cmd = &cobra.Command{Use: "test"}
appOptions = AppOptions{
ModuleOptions: map[string]*autocliv1.ModuleOptions{
"test": {Tx: nil},
},
}
err = b.enhanceCommandCommon(cmd, cmdTp, appOptions, map[string]*cobra.Command{})
assert.NilError(t, err)
}
}

func TestErrorBuildCommand(t *testing.T) {
fixture := initFixture(t)
b := fixture.b
b.AddQueryConnFlags = nil
b.AddTxConnFlags = nil

commandDescriptor := &autocliv1.ServiceCommandDescriptor{
Service: testpb.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Send",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{
ProtoField: "un-existent-proto-field",
},
},
},
},
}

appOptions := AppOptions{
ModuleOptions: map[string]*autocliv1.ModuleOptions{
"test": {
Query: commandDescriptor,
Tx: commandDescriptor,
},
},
ClientCtx: b.ClientCtx,
}

_, err := b.BuildMsgCommand(appOptions, nil)
assert.ErrorContains(t, err, "can't find field un-existent-proto-field")

appOptions.ModuleOptions["test"].Tx = &autocliv1.ServiceCommandDescriptor{Service: "un-existent-service"}
appOptions.ModuleOptions["test"].Query = &autocliv1.ServiceCommandDescriptor{Service: "un-existent-service"}
_, err = b.BuildMsgCommand(appOptions, nil)
assert.ErrorContains(t, err, "can't find service un-existent-service")
}
1 change: 1 addition & 0 deletions client/v2/autocli/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (b *Builder) BuildMsgMethodCommand(descriptor protoreflect.MethodDescriptor
return err
}
clientCtx = clientCtx.WithTxConfig(txConfigWithTextual)
clientCtx.Output = cmd.OutOrStdout()

// AutoCLI uses protov2 messages, while the SDK only supports proto v1 messages.
// Here we use dynamicpb, to create a proto v1 compatible message.
Expand Down
Loading

0 comments on commit 77f5068

Please sign in to comment.