don't delete remote if name does not change while renaming (fixes #1495)
This commit is contained in:
parent
d55f8f0492
commit
2d2778eabf
2 changed files with 69 additions and 6 deletions
11
fs/config.go
11
fs/config.go
|
@ -718,7 +718,7 @@ func ChooseRemote() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadLine reads some input
|
// ReadLine reads some input
|
||||||
func ReadLine() string {
|
var ReadLine = func() string {
|
||||||
buf := bufio.NewReader(os.Stdin)
|
buf := bufio.NewReader(os.Stdin)
|
||||||
line, err := buf.ReadString('\n')
|
line, err := buf.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1024,22 +1024,25 @@ func DeleteRemote(name string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// copyRemote asks the user for a new remote name and copies name into
|
// copyRemote asks the user for a new remote name and copies name into
|
||||||
// it
|
// it. Returns the new name.
|
||||||
func copyRemote(name string) {
|
func copyRemote(name string) string {
|
||||||
newName := NewRemoteName()
|
newName := NewRemoteName()
|
||||||
// Copy the keys
|
// Copy the keys
|
||||||
for _, key := range configData.GetKeyList(name) {
|
for _, key := range configData.GetKeyList(name) {
|
||||||
value := configData.MustValue(name, key, "")
|
value := configData.MustValue(name, key, "")
|
||||||
configData.SetValue(newName, key, value)
|
configData.SetValue(newName, key, value)
|
||||||
}
|
}
|
||||||
|
return newName
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenameRemote renames a config section
|
// RenameRemote renames a config section
|
||||||
func RenameRemote(name string) {
|
func RenameRemote(name string) {
|
||||||
fmt.Printf("Enter new name for %q remote.\n", name)
|
fmt.Printf("Enter new name for %q remote.\n", name)
|
||||||
copyRemote(name)
|
newName := copyRemote(name)
|
||||||
|
if name != newName {
|
||||||
configData.DeleteSection(name)
|
configData.DeleteSection(name)
|
||||||
SaveConfig()
|
SaveConfig()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyRemote copies a config section
|
// CopyRemote copies a config section
|
||||||
|
|
|
@ -3,6 +3,8 @@ package fs
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"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
|
// Test some error cases
|
||||||
func TestReveal(t *testing.T) {
|
func TestReveal(t *testing.T) {
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
|
|
Loading…
Reference in a new issue