diff --git a/.pending/bugfixes/gaiacli/3899-Using-gaiacli-c b/.pending/bugfixes/gaiacli/3899-Using-gaiacli-c new file mode 100644 index 000000000..0e732b58a --- /dev/null +++ b/.pending/bugfixes/gaiacli/3899-Using-gaiacli-c @@ -0,0 +1 @@ +#3899 Using 'gaiacli config node' breaks ~/config/config.toml \ No newline at end of file diff --git a/client/config.go b/client/config.go index ce57434a5..2bbc7c79e 100644 --- a/client/config.go +++ b/client/config.go @@ -144,7 +144,7 @@ func loadConfigFile(cfgFile string) (*toml.Tree, error) { } func saveConfigFile(cfgFile string, tree *toml.Tree) error { - fp, err := os.OpenFile(cfgFile, os.O_WRONLY|os.O_CREATE, 0644) + fp, err := os.OpenFile(cfgFile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) if err != nil { return err } diff --git a/client/config_test.go b/client/config_test.go new file mode 100644 index 000000000..6263a212a --- /dev/null +++ b/client/config_test.go @@ -0,0 +1,44 @@ +package client + +import ( + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/libs/cli" + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +// For https://github.com/cosmos/cosmos-sdk/issues/3899 +func Test_runConfigCmdTwiceWithShorterNodeValue(t *testing.T) { + // Prepare environment + t.Parallel() + configHome, cleanup := tmpDir(t) + defer cleanup() + _ = os.RemoveAll(filepath.Join(configHome, "config")) + viper.Set(cli.HomeFlag, configHome) + + // Init command config + cmd := ConfigCmd(configHome) + assert.NotNil(t, cmd) + + err := cmd.RunE(cmd, []string{"node", "tcp://localhost:26657"}) + assert.Nil(t, err) + + err = cmd.RunE(cmd, []string{"node", "--get"}) + assert.Nil(t, err) + + err = cmd.RunE(cmd, []string{"node", "tcp://local:26657"}) + assert.Nil(t, err) + + err = cmd.RunE(cmd, []string{"node", "--get"}) + assert.Nil(t, err) +} + +func tmpDir(t *testing.T) (string, func()) { + dir, err := ioutil.TempDir("", t.Name()+"_") + require.NoError(t, err) + return dir, func() { _ = os.RemoveAll(dir) } +}