/
helpers.go
53 lines (41 loc) · 1.45 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package testutil
import (
"context"
"fmt"
"github.com/spf13/viper"
tmcfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/types/module"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
)
func ExecInitCmd(testMbm module.BasicManager, home string, cdc codec.Codec) error {
logger := log.NewNopLogger()
cfg, err := CreateDefaultTendermintConfig(home)
if err != nil {
return err
}
cmd := genutilcli.InitCmd(testMbm, home)
serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithCodec(cdc).WithHomeDir(home)
_, out := testutil.ApplyMockIO(cmd)
clientCtx = clientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
cmd.SetArgs([]string{"appnode-test", fmt.Sprintf("--%s=%s", cli.HomeFlag, home)})
return cmd.ExecuteContext(ctx)
}
func CreateDefaultTendermintConfig(rootDir string) (*tmcfg.Config, error) {
conf := tmcfg.DefaultConfig()
conf.SetRoot(rootDir)
tmcfg.EnsureRoot(rootDir)
if err := conf.ValidateBasic(); err != nil {
return nil, fmt.Errorf("error in config file: %v", err)
}
return conf, nil
}