func WriteConfigVals(dir string, vals map[string]string) error {
	data := ""
	for k, v := range vals {
		data += fmt.Sprintf("%s = \"%s\"\n", k, v)
	}
	cfile := filepath.Join(dir, "config.toml")
	return ioutil.WriteFile(cfile, []byte(data), 0600)
}
WriteConfigVals writes a toml file with the given values.
It returns an error if writing was impossible.
package commands
import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
	"testing"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	cfg "github.com/line/ostracon/config"
	"github.com/line/ostracon/libs/cli"
	tmos "github.com/line/ostracon/libs/os"
)
var (
	defaultRoot = os.ExpandEnv("$HOME/.some/test/dir")
)
func clearConfig(dir string) {
	if err := os.Unsetenv("OCHOME"); err != nil {
		panic(err)
	}
	if err := os.Unsetenv("OC_HOME"); err != nil {
		panic(err)
	}
	if err := os.RemoveAll(dir); err != nil {
		panic(err)
	}
	viper.Reset()
	config = cfg.DefaultConfig()
}
func testRootCmd() *cobra.Command {
	rootCmd := &cobra.Command{
		Use:               RootCmd.Use,
		PersistentPreRunE: RootCmd.PersistentPreRunE,
		Run:               func(cmd *cobra.Command, args []string) {},
	}
	registerFlagsRootCmd(rootCmd)
	var l string
	rootCmd.PersistentFlags().String("log", l, "Log")
	return rootCmd
}
func testSetup(rootDir string, args []string, env map[string]string) error {
	clearConfig(defaultRoot)
	rootCmd := testRootCmd()
	cmd := cli.PrepareBaseCmd(rootCmd, "OC", defaultRoot)
	args = append([]string{rootCmd.Use}, args...)
	return cli.RunWithArgs(cmd, args, env)
}
func TestRootHome(t *testing.T) {
	newRoot := filepath.Join(defaultRoot, "something-else")
	cases := []struct {
		args []string
		env  map[string]string
		root string
	}{
		{nil, nil, defaultRoot},
		{[]string{"--home", newRoot}, nil, newRoot},
		{nil, map[string]string{"OCHOME": newRoot}, newRoot},
	}
	for i, tc := range cases {
		idxString := strconv.Itoa(i)
		err := testSetup(defaultRoot, tc.args, tc.env)
		require.Nil(t, err, idxString)
		assert.Equal(t, tc.root, config.RootDir, idxString)
		assert.Equal(t, tc.root, config.P2P.RootDir, idxString)
		assert.Equal(t, tc.root, config.Consensus.RootDir, idxString)
		assert.Equal(t, tc.root, config.Mempool.RootDir, idxString)
	}
}
func TestRootFlagsEnv(t *testing.T) {
	defaults := cfg.DefaultConfig()
	defaultLogLvl := defaults.LogLevel
	cases := []struct {
		args     []string
		env      map[string]string
		logLevel string
	}{
		{[]string{"--log", "debug"}, nil, defaultLogLvl},                  
		{[]string{"--log_level", "debug"}, nil, "debug"},                  
		{nil, map[string]string{"OC_LOW": "debug"}, defaultLogLvl},        
		{nil, map[string]string{"MT_LOG_LEVEL": "debug"}, defaultLogLvl},  
		{nil, map[string]string{"OC_LOG_LEVEL": "debug"}, "debug"},        
	}
	for i, tc := range cases {
		idxString := strconv.Itoa(i)
		err := testSetup(defaultRoot, tc.args, tc.env)
		require.Nil(t, err, idxString)
		assert.Equal(t, tc.logLevel, config.LogLevel, idxString)
	}
}
func TestRootConfig(t *testing.T) {
	nonDefaultLogLvl := "abc:debug"
	cvals := map[string]string{
		"log_level": nonDefaultLogLvl,
	}
	cases := []struct {
		args []string
		env  map[string]string
		logLvl string
	}{
		{nil, nil, nonDefaultLogLvl},                                      
		{[]string{"--log_level=abc:info"}, nil, "abc:info"},               
		{nil, map[string]string{"OC_LOG_LEVEL": "abc:info"}, "abc:info"},  
	}
	for i, tc := range cases {
		idxString := strconv.Itoa(i)
		clearConfig(defaultRoot)
		configFilePath := filepath.Join(defaultRoot, "config")
		err := tmos.EnsureDir(configFilePath, 0700)
		require.Nil(t, err)
		err = WriteConfigVals(configFilePath, cvals)
		require.Nil(t, err)
		rootCmd := testRootCmd()
		cmd := cli.PrepareBaseCmd(rootCmd, "OC", defaultRoot)
		tc.args = append([]string{rootCmd.Use}, tc.args...)
		err = cli.RunWithArgs(cmd, tc.args, tc.env)
		require.Nil(t, err, idxString)
		assert.Equal(t, tc.logLvl, config.LogLevel, idxString)
	}
}