github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/restic/config_test.go (about) 1 package restic_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/restic/restic/internal/restic" 8 rtest "github.com/restic/restic/internal/test" 9 ) 10 11 type saver func(restic.FileType, interface{}) (restic.ID, error) 12 13 func (s saver) SaveJSONUnpacked(t restic.FileType, arg interface{}) (restic.ID, error) { 14 return s(t, arg) 15 } 16 17 type loader func(context.Context, restic.FileType, restic.ID, interface{}) error 18 19 func (l loader) LoadJSONUnpacked(ctx context.Context, t restic.FileType, id restic.ID, arg interface{}) error { 20 return l(ctx, t, id, arg) 21 } 22 23 func TestConfig(t *testing.T) { 24 resultConfig := restic.Config{} 25 save := func(tpe restic.FileType, arg interface{}) (restic.ID, error) { 26 rtest.Assert(t, tpe == restic.ConfigFile, 27 "wrong backend type: got %v, wanted %v", 28 tpe, restic.ConfigFile) 29 30 cfg := arg.(restic.Config) 31 resultConfig = cfg 32 return restic.ID{}, nil 33 } 34 35 cfg1, err := restic.CreateConfig() 36 rtest.OK(t, err) 37 38 _, err = saver(save).SaveJSONUnpacked(restic.ConfigFile, cfg1) 39 40 load := func(ctx context.Context, tpe restic.FileType, id restic.ID, arg interface{}) error { 41 rtest.Assert(t, tpe == restic.ConfigFile, 42 "wrong backend type: got %v, wanted %v", 43 tpe, restic.ConfigFile) 44 45 cfg := arg.(*restic.Config) 46 *cfg = resultConfig 47 return nil 48 } 49 50 cfg2, err := restic.LoadConfig(context.TODO(), loader(load)) 51 rtest.OK(t, err) 52 53 rtest.Assert(t, cfg1 == cfg2, 54 "configs aren't equal: %v != %v", cfg1, cfg2) 55 }