json testing, addressed init option PR comments

This commit is contained in:
rigelrozanski 2017-09-06 01:10:06 -04:00
parent 748a1e97e4
commit 4ac089f084
4 changed files with 27 additions and 12 deletions

View File

@ -37,6 +37,7 @@ test_unit:
@go test `glide novendor`
test_cli: $(TEST_EXAMPLES)
./tests/cli/init-server.sh
# sudo apt-get install jq
# wget "https://raw.githubusercontent.com/kward/shunit2/master/source/2.1/src/shunit2"

View File

@ -31,7 +31,7 @@ var (
func init() {
InitCmd.Flags().String(FlagChainID, "test_chain_id", "Chain ID")
InitCmd.Flags().StringSlice(FlagOption, []string{}, "Genesis option in the format <app>/<option>/<value>")
InitCmd.Flags().StringSliceP(FlagOption, "p", []string{}, "Genesis option in the format <app>/<option>/<value>")
}
// returns 1 iff it set a file, otherwise 0 (so we can add them)
@ -67,12 +67,13 @@ func initCmd(cmd *cobra.Command, args []string) error {
return errors.New("Address must be 20-bytes in hex")
}
var options []string
var optionsStr string
sep := ",\n "
optionsRaw := viper.GetStringSlice(FlagOption)
if len(optionsRaw) > 0 {
optionsStr = sep
var options []string
sep := ",\n "
for i := 0; i < len(optionsRaw); i++ {
s := strings.SplitN(optionsRaw[i], "/", 3)
if len(s) != 3 {
@ -87,8 +88,8 @@ func initCmd(cmd *cobra.Command, args []string) error {
option := `"` + s[0] + `/` + s[1] + `", ` + s[2]
options = append(options, option)
}
optionsStr = sep + strings.Join(options[:], sep)
}
optionsStr += strings.Join(options[:], sep)
genesis := GetGenesisJSON(viper.GetString(FlagChainID), userAddr, optionsStr)
return CreateGenesisValidatorFiles(cfg, genesis, cmd.Root().Name())

View File

@ -35,6 +35,7 @@ func preRunSetup(cmd *cobra.Command, args []string) (err error) {
return nil
}
// SetUpRoot - initialize the root command
func SetUpRoot(cmd *cobra.Command) {
cmd.PersistentPreRunE = preRunSetup
cmd.PersistentFlags().String(FlagLogLevel, defaultLogLevel, "Log level")

View File

@ -8,24 +8,36 @@ test01initOption() {
rm -rf "$BASE"
mkdir -p "$BASE"
SERVER="${BASE}/server"
GENESIS_FILE=${SERVER}/genesis.json
SERVE_DIR="${BASE}/server"
GENESIS_FILE=${SERVE_DIR}/genesis.json
HEX="deadbeef1234deadbeef1234deadbeef1234aaaa"
${SERVER_EXE} init ${HEX} --home="$SERVER" --option=app1/key1/val1 --option=app2/key2/val2 >/dev/null
${SERVER_EXE} init ${HEX} --home="$SERVE_DIR" -p=app1/key1/val1 -p='"app2/key2/{""name"": ""joe"", ""age"": ""100""}"' >/dev/null
if ! assertTrue "line=${LINENO}" $?; then return 1; fi
OPTION1KEY=$(cat ${GENESIS_FILE} | jq '.app_options.plugin_options[2]')
OPTION1VAL=$(cat ${GENESIS_FILE} | jq '.app_options.plugin_options[3]')
OPTION2KEY=$(cat ${GENESIS_FILE} | jq '.app_options.plugin_options[4]')
OPTION2VAL=$(cat ${GENESIS_FILE} | jq '.app_options.plugin_options[5]')
OPTION2VALEXPECTED=$(echo '{"name": "joe", "age": "100"}' | jq '.')
assertEquals "line=${LINENO}" '"app1/key1"' $OPTION1KEY
assertEquals "line=${LINENO}" '"val1"' $OPTION1VAL
assertEquals "line=${LINENO}" '"app2/key2"' $OPTION2KEY
assertEquals "line=${LINENO}" '"val2"' $OPTION2VAL
assertEquals "line=${LINENO}" "$OPTION2VALEXPECTED" "$OPTION2VAL"
}
test02runServer() {
# Attempt to begin the server with the custom genesis
SERVER_LOG=$BASE/${SERVER_EXE}.log
startServer $SERVE_DIR $SERVER_LOG
}
oneTimeTearDown() {
quickTearDown
}
# load and run these tests with shunit2!
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #get this files directory
. $DIR/shunit2
CLI_DIR=$GOPATH/src/github.com/cosmos/cosmos-sdk/tests/cli
. $CLI_DIR/common.sh
. $CLI_DIR/shunit2