@@ -316,3 +316,45 @@ func TestAllConfig(t *testing.T) {
316316 assert .Equal ("repo 4" , allConfig .Get ("test.key4" ))
317317 assert .Equal ("repo 5" , allConfig .Get ("test.key5" ))
318318}
319+
320+ func TestSaveConfig (t * testing.T ) {
321+ assert := assert .New (t )
322+
323+ tmpdir , err := ioutil .TempDir ("" , "goconfig" )
324+ if err != nil {
325+ panic (err )
326+ }
327+ defer func (dir string ) {
328+ os .RemoveAll (dir )
329+ }(tmpdir )
330+
331+ home := os .Getenv ("HOME" )
332+ os .Setenv ("HOME" , tmpdir )
333+ defer os .Setenv ("HOME" , home )
334+
335+ // Create system config
336+ cfgFile := filepath .Join (tmpdir , "gitconfig" )
337+
338+ assert .Nil (exec .Command ("git" , "config" , "-f" , cfgFile , "ab.cd.ef" , "value-1" ).Run ())
339+ assert .Nil (exec .Command ("git" , "config" , "-f" , cfgFile , "ab.cd e.fg" , "value 2" ).Run ())
340+ assert .Nil (exec .Command ("git" , "config" , "-f" , cfgFile , "--add" , "ab.cd e.fg" , "value 3" ).Run ())
341+ assert .Nil (exec .Command ("git" , "config" , "-f" , cfgFile , "--add" , "ab.cd e.fg" , "value 4" ).Run ())
342+ assert .Nil (exec .Command ("git" , "config" , "-f" , cfgFile , "ab.cd" , "value has space " ).Run ())
343+
344+ // Load cfgFile
345+ cfg , err := LoadFile (cfgFile , false )
346+ assert .Nil (err )
347+ assert .Equal ("value has space " , cfg .Get ("ab.cd" ))
348+
349+ // Save file
350+ newCfgFile := cfgFile + ".new"
351+ err = cfg .Save (newCfgFile )
352+ assert .Nil (err )
353+ _ , err = os .Stat (newCfgFile )
354+ assert .Nil (err )
355+
356+ // Load new file
357+ newCfg , err := LoadFile (newCfgFile , false )
358+ assert .Nil (err )
359+ assert .Equal (cfg , newCfg )
360+ }
0 commit comments