Skip to content

Commit

Permalink
x/auth: in-process test refactor (#6573)
Browse files Browse the repository at this point in the history
* remove unused functions

* create helper func to send tx

* refactor to use test help to send tx by client

* Commit before getting backend.

* Temporal commit

* temp commit

* remove the creation of txbuilder from cli

* fix imports

* update changelog

* Remove unused function.

* Add flag home into tx sign command.

* migrade TestCLIValidateSignatures to use new test suite

* migrate test one

* Add changes to make sign batch.

* make test pass

* refactor common logic

* First part of cli sign.

* Add test for sign batch.

* refactor a little and improve the test

* migrate broadcast command

* fix linter

* Remove printf for debug in bank module.

* Fix unused err var.

* fix linter

* fix test

* fix tests client

* Fix linter.

* Temp commit signature.

* encode tx

* migrate tests

* Fix imports.

* Remove changelog

* fix tests

* Fix tests.

* Update x/bank/client/testutil/cli_helpers.go

* Remove alias.

* Remove wait for N block func.

* export callCmd function into its own file.

* fix imports

* bring back to inner functions

* apply mock io

* the helpers use mockio

* fix bug

* Add Helpers.

* return to put the function in testutil package

* return BufferWriter in ExecTestCLICmd

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 14, 2020
1 parent 5656e86 commit 351192a
Show file tree
Hide file tree
Showing 20 changed files with 769 additions and 546 deletions.
14 changes: 7 additions & 7 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ func txCommand() *cobra.Command {
}

cmd.AddCommand(
authcmd.GetSignCommand(initClientCtx),
authcmd.GetSignBatchCommand(encodingConfig.Amino),
authcmd.GetMultiSignCommand(initClientCtx),
authcmd.GetValidateSignaturesCommand(initClientCtx),
authcmd.GetSignCommand(),
authcmd.GetSignBatchCommand(),
authcmd.GetMultiSignCommand(),
authcmd.GetValidateSignaturesCommand(),
flags.LineBreak,
authcmd.GetBroadcastCommand(initClientCtx),
authcmd.GetEncodeCommand(initClientCtx),
authcmd.GetDecodeCommand(initClientCtx),
authcmd.GetBroadcastCommand(),
authcmd.GetEncodeCommand(),
authcmd.GetDecodeCommand(),
flags.LineBreak,
)

Expand Down
2 changes: 1 addition & 1 deletion tests/cli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func AddFlags(cmd string, flags []string) string {
return strings.TrimSpace(cmd)
}

func UnmarshalStdTx(t require.TestingT, c *codec.Codec, s string) (stdTx authtypes.StdTx) {
func UnmarshalStdTx(t require.TestingT, c codec.JSONMarshaler, s string) (stdTx authtypes.StdTx) {
require.Nil(t, c.UnmarshalJSON([]byte(s), &stdTx))
return
}
Expand Down
27 changes: 27 additions & 0 deletions testutil/cli/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cli

import (
"context"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/testutil"
)

// ExecTestCLICmd builds the client context, mocks the output and executes the command.
func ExecTestCLICmd(clientCtx client.Context, cmd *cobra.Command, extraArgs []string) (testutil.BufferWriter, error) {
cmd.SetArgs(extraArgs)

_, out := testutil.ApplyMockIO(cmd)
clientCtx = clientCtx.WithOutput(out)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

if err := cmd.ExecuteContext(ctx); err != nil {
return out, err
}

return out, nil
}
2 changes: 1 addition & 1 deletion testutil/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ApplyMockIODiscardOutErr(c *cobra.Command) BufferReader {
// the caller must call to remove the file when it is
// no longer needed.
func WriteToNewTempFile(t testing.TB, s string) (*os.File, func()) {
fp, err := ioutil.TempFile("", t.Name()+"_")
fp, err := ioutil.TempFile("", strings.ReplaceAll(t.Name(), "/", "_")+"_")
require.Nil(t, err)

_, err = fp.WriteString(s)
Expand Down
4 changes: 2 additions & 2 deletions x/auth/client/cli/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// GetBroadcastCommand returns the tx broadcast command.
func GetBroadcastCommand(clientCtx client.Context) *cobra.Command {
func GetBroadcastCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "broadcast [file_path]",
Short: "Broadcast transactions generated offline",
Expand All @@ -25,7 +25,7 @@ $ <appcli> tx broadcast ./mytxn.json
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx = clientCtx.Init()
clientCtx := client.GetClientContextFromCmd(cmd)

if offline, _ := cmd.Flags().GetBool(flags.FlagOffline); offline {
return errors.New("cannot broadcast tx during offline mode")
Expand Down
46 changes: 0 additions & 46 deletions x/auth/client/cli/broadcast_test.go

This file was deleted.

Loading

0 comments on commit 351192a

Please sign in to comment.