From 2d2778eabf9d9395e728be8d77fd121e2a336e7b Mon Sep 17 00:00:00 2001 From: Stefan Breunig Date: Sun, 25 Jun 2017 08:55:54 +0200 Subject: [PATCH] don't delete remote if name does not change while renaming (fixes #1495) --- fs/config.go | 15 +++++++----- fs/config_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/fs/config.go b/fs/config.go index 7573adbc1..44b4689e8 100644 --- a/fs/config.go +++ b/fs/config.go @@ -718,7 +718,7 @@ func ChooseRemote() string { } // ReadLine reads some input -func ReadLine() string { +var ReadLine = func() string { buf := bufio.NewReader(os.Stdin) line, err := buf.ReadString('\n') if err != nil { @@ -1024,22 +1024,25 @@ func DeleteRemote(name string) { } // copyRemote asks the user for a new remote name and copies name into -// it -func copyRemote(name string) { +// it. Returns the new name. +func copyRemote(name string) string { newName := NewRemoteName() // Copy the keys for _, key := range configData.GetKeyList(name) { value := configData.MustValue(name, key, "") configData.SetValue(newName, key, value) } + return newName } // RenameRemote renames a config section func RenameRemote(name string) { fmt.Printf("Enter new name for %q remote.\n", name) - copyRemote(name) - configData.DeleteSection(name) - SaveConfig() + newName := copyRemote(name) + if name != newName { + configData.DeleteSection(name) + SaveConfig() + } } // CopyRemote copies a config section diff --git a/fs/config_test.go b/fs/config_test.go index c21116b7f..22d70f59a 100644 --- a/fs/config_test.go +++ b/fs/config_test.go @@ -3,6 +3,8 @@ package fs import ( "bytes" "crypto/rand" + "io/ioutil" + "os" "testing" "github.com/stretchr/testify/assert" @@ -38,6 +40,64 @@ func TestObscure(t *testing.T) { } } +func TestCRUD(t *testing.T) { + configKey = nil // reset password + // create temp config file + tempFile, err := ioutil.TempFile("", "crud.conf") + assert.NoError(t, err) + path := tempFile.Name() + defer func() { + err := os.Remove(path) + assert.NoError(t, err) + }() + assert.NoError(t, tempFile.Close()) + + // temporarily adapt configuration + oldOsStdout := os.Stdout + oldConfigFile := configFile + oldConfig := Config + oldReadLine := ReadLine + os.Stdout = nil + configFile = &path + Config = &ConfigInfo{} + defer func() { + os.Stdout = oldOsStdout + configFile = oldConfigFile + ReadLine = oldReadLine + Config = oldConfig + }() + + LoadConfig() + assert.Equal(t, []string{}, configData.GetSectionList()) + + // add new remote + i := 0 + ReadLine = func() string { + answers := []string{ + "local", // type is local + "1", // yes, disable long filenames + "y", // looks good, save + } + i = i + 1 + return answers[i-1] + } + NewRemote("test") + assert.Equal(t, []string{"test"}, configData.GetSectionList()) + + // normal rename, test → asdf + ReadLine = func() string { return "asdf" } + RenameRemote("test") + assert.Equal(t, []string{"asdf"}, configData.GetSectionList()) + + // no-op rename, asdf → asdf + RenameRemote("asdf") + assert.Equal(t, []string{"asdf"}, configData.GetSectionList()) + + // delete remote + DeleteRemote("asdf") + assert.Equal(t, []string{}, configData.GetSectionList()) +} + // Test some error cases func TestReveal(t *testing.T) { for _, test := range []struct {