mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-24 10:11:48 +00:00
One more helper function for cli tests...
This commit is contained in:
@ -2,10 +2,30 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// WriteDemoConfig writes a toml file with the given values.
|
||||||
|
// It returns the RootDir the config.toml file is stored in,
|
||||||
|
// or an error if writing was impossible
|
||||||
|
func WriteDemoConfig(vals map[string]string) (string, error) {
|
||||||
|
cdir, err := ioutil.TempDir("", "test-cli")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
data := ""
|
||||||
|
for k, v := range vals {
|
||||||
|
data = data + fmt.Sprintf("%s = \"%s\"\n", k, v)
|
||||||
|
}
|
||||||
|
cfile := filepath.Join(cdir, "config.toml")
|
||||||
|
err = ioutil.WriteFile(cfile, []byte(data), 0666)
|
||||||
|
return cdir, err
|
||||||
|
}
|
||||||
|
|
||||||
// RunWithArgs executes the given command with the specified command line args
|
// RunWithArgs executes the given command with the specified command line args
|
||||||
// and environmental variables set. It returns any error returned from cmd.Execute()
|
// and environmental variables set. It returns any error returned from cmd.Execute()
|
||||||
func RunWithArgs(cmd Executable, args []string, env map[string]string) error {
|
func RunWithArgs(cmd Executable, args []string, env map[string]string) error {
|
||||||
|
@ -2,8 +2,6 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -57,30 +55,16 @@ func TestSetupEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeConfig(vals map[string]string) (string, error) {
|
|
||||||
cdir, err := ioutil.TempDir("", "test-cli")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
data := ""
|
|
||||||
for k, v := range vals {
|
|
||||||
data = data + fmt.Sprintf("%s = \"%s\"\n", k, v)
|
|
||||||
}
|
|
||||||
cfile := filepath.Join(cdir, "config.toml")
|
|
||||||
err = ioutil.WriteFile(cfile, []byte(data), 0666)
|
|
||||||
return cdir, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetupConfig(t *testing.T) {
|
func TestSetupConfig(t *testing.T) {
|
||||||
assert, require := assert.New(t), require.New(t)
|
assert, require := assert.New(t), require.New(t)
|
||||||
|
|
||||||
// we pre-create two config files we can refer to in the rest of
|
// we pre-create two config files we can refer to in the rest of
|
||||||
// the test cases.
|
// the test cases.
|
||||||
cval1, cval2 := "fubble", "wubble"
|
cval1, cval2 := "fubble", "wubble"
|
||||||
conf1, err := writeConfig(map[string]string{"boo": cval1})
|
conf1, err := WriteDemoConfig(map[string]string{"boo": cval1})
|
||||||
require.Nil(err)
|
require.Nil(err)
|
||||||
// even with some ignored fields, should be no problem
|
// even with some ignored fields, should be no problem
|
||||||
conf2, err := writeConfig(map[string]string{"boo": cval2, "foo": "bar"})
|
conf2, err := WriteDemoConfig(map[string]string{"boo": cval2, "foo": "bar"})
|
||||||
require.Nil(err)
|
require.Nil(err)
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
@ -135,10 +119,10 @@ func TestSetupUnmarshal(t *testing.T) {
|
|||||||
// we pre-create two config files we can refer to in the rest of
|
// we pre-create two config files we can refer to in the rest of
|
||||||
// the test cases.
|
// the test cases.
|
||||||
cval1, cval2 := "someone", "else"
|
cval1, cval2 := "someone", "else"
|
||||||
conf1, err := writeConfig(map[string]string{"name": cval1})
|
conf1, err := WriteDemoConfig(map[string]string{"name": cval1})
|
||||||
require.Nil(err)
|
require.Nil(err)
|
||||||
// even with some ignored fields, should be no problem
|
// even with some ignored fields, should be no problem
|
||||||
conf2, err := writeConfig(map[string]string{"name": cval2, "foo": "bar"})
|
conf2, err := WriteDemoConfig(map[string]string{"name": cval2, "foo": "bar"})
|
||||||
require.Nil(err)
|
require.Nil(err)
|
||||||
|
|
||||||
// unused is not declared on a flag and remains from base
|
// unused is not declared on a flag and remains from base
|
||||||
|
Reference in New Issue
Block a user