don't delete remote if name does not change while renaming (fixes #1495)

This commit is contained in:
Stefan Breunig 2017-06-25 08:55:54 +02:00
parent d55f8f0492
commit 2d2778eabf
2 changed files with 69 additions and 6 deletions

View file

@ -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

View file

@ -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 {