cosmos-sdk/x/gov/client/testutil/helpers.go
dependabot-preview[bot] f37308778a
build(deps): bump github.com/spf13/viper from 1.6.3 to 1.7.0 (#6183)
* build(deps): bump github.com/spf13/viper from 1.6.3 to 1.7.0

Bumps [github.com/spf13/viper](https://github.com/spf13/viper) from 1.6.3 to 1.7.0.
- [Release notes](https://github.com/spf13/viper/releases)
- [Commits](https://github.com/spf13/viper/compare/v1.6.3...v1.7.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* refresh go.sum

* run make format

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
2020-05-11 10:22:36 +02:00

174 lines
6.6 KiB
Go

package testutil
import (
"fmt"
"strings"
"github.com/stretchr/testify/require"
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
)
//___________________________________________________________________________________
// simcli query gov
// QueryGovParamDeposit is simcli query gov param deposit
func QueryGovParamDeposit(f *cli.Fixtures) gov.DepositParams {
cmd := fmt.Sprintf("%s query gov param deposit %s", f.SimcliBinary, f.Flags())
out, _ := tests.ExecuteT(f.T, cmd, "")
var depositParam gov.DepositParams
err := f.Cdc.UnmarshalJSON([]byte(out), &depositParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return depositParam
}
// QueryGovParamVoting is simcli query gov param voting
func QueryGovParamVoting(f *cli.Fixtures) gov.VotingParams {
cmd := fmt.Sprintf("%s query gov param voting %s", f.SimcliBinary, f.Flags())
out, _ := tests.ExecuteT(f.T, cmd, "")
var votingParam gov.VotingParams
err := f.Cdc.UnmarshalJSON([]byte(out), &votingParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return votingParam
}
// QueryGovParamTallying is simcli query gov param tallying
func QueryGovParamTallying(f *cli.Fixtures) gov.TallyParams {
cmd := fmt.Sprintf("%s query gov param tallying %s", f.SimcliBinary, f.Flags())
out, _ := tests.ExecuteT(f.T, cmd, "")
var tallyingParam gov.TallyParams
err := f.Cdc.UnmarshalJSON([]byte(out), &tallyingParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return tallyingParam
}
// QueryGovProposal is simcli query gov proposal
func QueryGovProposal(f *cli.Fixtures, proposalID int, flags ...string) gov.Proposal {
cmd := fmt.Sprintf("%s query gov proposal %d %v", f.SimcliBinary, proposalID, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var proposal gov.Proposal
err := f.Cdc.UnmarshalJSON([]byte(out), &proposal)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return proposal
}
// QueryGovProposals is simcli query gov proposals
func QueryGovProposals(f *cli.Fixtures, flags ...string) gov.Proposals {
cmd := fmt.Sprintf("%s query gov proposals %v", f.SimcliBinary, f.Flags())
stdout, stderr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
if strings.Contains(stderr, "no matching proposals found") {
return gov.Proposals{}
}
require.Empty(f.T, stderr)
var out gov.Proposals
err := f.Cdc.UnmarshalJSON([]byte(stdout), &out)
require.NoError(f.T, err)
return out
}
// QueryGovVote is simcli query gov vote
func QueryGovVote(f *cli.Fixtures, proposalID int, voter sdk.AccAddress, flags ...string) gov.Vote {
cmd := fmt.Sprintf("%s query gov vote %d %s %v", f.SimcliBinary, proposalID, voter, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var vote gov.Vote
err := f.Cdc.UnmarshalJSON([]byte(out), &vote)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return vote
}
// QueryGovVotes is simcli query gov votes
func QueryGovVotes(f *cli.Fixtures, proposalID int, flags ...string) []gov.Vote {
cmd := fmt.Sprintf("%s query gov votes %d %v", f.SimcliBinary, proposalID, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var votes []gov.Vote
err := f.Cdc.UnmarshalJSON([]byte(out), &votes)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return votes
}
// QueryGovDeposit is simcli query gov deposit
func QueryGovDeposit(f *cli.Fixtures, proposalID int, depositor sdk.AccAddress, flags ...string) gov.Deposit {
cmd := fmt.Sprintf("%s query gov deposit %d %s %v", f.SimcliBinary, proposalID, depositor, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var deposit gov.Deposit
err := f.Cdc.UnmarshalJSON([]byte(out), &deposit)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return deposit
}
// QueryGovDeposits is simcli query gov deposits
func QueryGovDeposits(f *cli.Fixtures, propsalID int, flags ...string) []gov.Deposit {
cmd := fmt.Sprintf("%s query gov deposits %d %v", f.SimcliBinary, propsalID, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var deposits []gov.Deposit
err := f.Cdc.UnmarshalJSON([]byte(out), &deposits)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return deposits
}
//___________________________________________________________________________________
// simcli tx gov
// TxGovSubmitProposal is simcli tx gov submit-proposal
func TxGovSubmitProposal(f *cli.Fixtures, from, typ, title, description string, deposit sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx gov submit-proposal %v --keyring-backend=test --from=%s --type=%s",
f.SimcliBinary, f.Flags(), from, typ)
cmd += fmt.Sprintf(" --title=%s --description=%s --deposit=%s", title, description, deposit)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovDeposit is simcli tx gov deposit
func TxGovDeposit(f *cli.Fixtures, proposalID int, from string, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx gov deposit %d %s --keyring-backend=test --from=%s %v",
f.SimcliBinary, proposalID, amount, from, f.Flags())
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovVote is simcli tx gov vote
func TxGovVote(f *cli.Fixtures, proposalID int, option gov.VoteOption, from string, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx gov vote %d %s --keyring-backend=test --from=%s %v",
f.SimcliBinary, proposalID, option, from, f.Flags())
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovSubmitParamChangeProposal executes a CLI parameter change proposal
// submission.
func TxGovSubmitParamChangeProposal(f *cli.Fixtures,
from, proposalPath string, deposit sdk.Coin, flags ...string,
) (bool, string, string) {
cmd := fmt.Sprintf(
"%s tx gov submit-proposal param-change %s --keyring-backend=test --from=%s %v",
f.SimcliBinary, proposalPath, from, f.Flags(),
)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovSubmitCommunityPoolSpendProposal executes a CLI community pool spend proposal
// submission.
func TxGovSubmitCommunityPoolSpendProposal(f *cli.Fixtures,
from, proposalPath string, deposit sdk.Coin, flags ...string,
) (bool, string, string) {
cmd := fmt.Sprintf(
"%s tx gov submit-proposal community-pool-spend %s --keyring-backend=test --from=%s %v",
f.SimcliBinary, proposalPath, from, f.Flags(),
)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}