Browse Source

Save config on device rename (fixes #957)

Audrius Butkevicius 11 years ago
parent
commit
39a3b8922d
2 changed files with 18 additions and 5 deletions
  1. 6 4
      internal/model/model.go
  2. 12 1
      internal/model/model_test.go

+ 6 - 4
internal/model/model.go

@@ -527,12 +527,15 @@ func (m *Model) ClusterConfig(deviceID protocol.DeviceID, cm protocol.ClusterCon
 
 	l.Infof(`Device %s client is "%s %s"`, deviceID, cm.ClientName, cm.ClientVersion)
 
+	var changed bool
+
 	if name := cm.GetOption("name"); name != "" {
 		l.Infof("Device %s name is %q", deviceID, name)
 		device, ok := m.cfg.Devices()[deviceID]
 		if ok && device.Name == "" {
 			device.Name = name
 			m.cfg.SetDevice(device)
+			changed = true
 		}
 	}
 
@@ -540,7 +543,6 @@ func (m *Model) ClusterConfig(deviceID protocol.DeviceID, cm protocol.ClusterCon
 		// This device is an introducer. Go through the announced lists of folders
 		// and devices and add what we are missing.
 
-		var changed bool
 		for _, folder := range cm.Folders {
 			// If we don't have this folder yet, skip it. Ideally, we'd
 			// offer up something in the GUI to create the folder, but for the
@@ -599,10 +601,10 @@ func (m *Model) ClusterConfig(deviceID protocol.DeviceID, cm protocol.ClusterCon
 				changed = true
 			}
 		}
+	}
 
-		if changed {
-			m.cfg.Save()
-		}
+	if changed {
+		m.cfg.Save()
 	}
 }
 

+ 12 - 1
internal/model/model_test.go

@@ -258,6 +258,8 @@ func TestDeviceRename(t *testing.T) {
 		ClientVersion: "v0.9.4",
 	}
 
+	defer os.Remove("tmpconfig.xml")
+
 	cfg := config.New(device1)
 	cfg.Devices = []config.DeviceConfiguration{
 		{
@@ -266,7 +268,7 @@ func TestDeviceRename(t *testing.T) {
 	}
 
 	db, _ := leveldb.Open(storage.NewMemStorage(), nil)
-	m := NewModel(config.Wrap("/tmp/test", cfg), "device", "syncthing", "dev", db)
+	m := NewModel(config.Wrap("tmpconfig.xml", cfg), "device", "syncthing", "dev", db)
 	if cfg.Devices[0].Name != "" {
 		t.Errorf("Device already has a name")
 	}
@@ -292,6 +294,15 @@ func TestDeviceRename(t *testing.T) {
 	if cfg.Devices[0].Name != "tester" {
 		t.Errorf("Device name got overwritten")
 	}
+
+	cfgw, err := config.Load("tmpconfig.xml", protocol.LocalDeviceID)
+	if err != nil {
+		t.Error(err)
+		return
+	}
+	if cfgw.Devices()[device1].Name != "tester" {
+		t.Errorf("Device name not saved in config")
+	}
 }
 
 func TestClusterConfig(t *testing.T) {