From 3250fdc2cae87715b4c36c5e9e0e64b424b5ae39 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 1 May 2017 22:56:05 +0200 Subject: [PATCH] mem: Convert to test suite --- src/restic/backend/mem/backend_test.go | 87 ---------------------- src/restic/backend/mem/mem_backend_test.go | 74 +++++++++++------- 2 files changed, 47 insertions(+), 114 deletions(-) delete mode 100644 src/restic/backend/mem/backend_test.go diff --git a/src/restic/backend/mem/backend_test.go b/src/restic/backend/mem/backend_test.go deleted file mode 100644 index 13e95f115..000000000 --- a/src/restic/backend/mem/backend_test.go +++ /dev/null @@ -1,87 +0,0 @@ -// DO NOT EDIT, AUTOMATICALLY GENERATED -package mem_test - -import ( - "testing" - - "restic/backend/test" -) - -var SkipMessage string - -func TestMemBackendCreate(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestCreate(t) -} - -func TestMemBackendOpen(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestOpen(t) -} - -func TestMemBackendCreateWithConfig(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestCreateWithConfig(t) -} - -func TestMemBackendLocation(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestLocation(t) -} - -func TestMemBackendConfig(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestConfig(t) -} - -func TestMemBackendLoad(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestLoad(t) -} - -func TestMemBackendSave(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestSave(t) -} - -func TestMemBackendSaveFilenames(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestSaveFilenames(t) -} - -func TestMemBackendBackend(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestBackend(t) -} - -func TestMemBackendDelete(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestDelete(t) -} - -func TestMemBackendCleanup(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestCleanup(t) -} diff --git a/src/restic/backend/mem/mem_backend_test.go b/src/restic/backend/mem/mem_backend_test.go index 75b65f4c7..2f39787a9 100644 --- a/src/restic/backend/mem/mem_backend_test.go +++ b/src/restic/backend/mem/mem_backend_test.go @@ -2,6 +2,7 @@ package mem_test import ( "restic" + "testing" "restic/errors" @@ -9,31 +10,50 @@ import ( "restic/backend/test" ) -var be restic.Backend - -//go:generate go run ../test/generate_backend_tests.go - -func init() { - test.CreateFn = func() (restic.Backend, error) { - if be != nil { - return nil, errors.New("temporary memory backend dir already exists") - } - - be = mem.New() - - return be, nil - } - - test.OpenFn = func() (restic.Backend, error) { - if be == nil { - return nil, errors.New("repository not initialized") - } - - return be, nil - } - - test.CleanupFn = func() error { - be = nil - return nil - } +type memConfig struct { + be restic.Backend +} + +func TestSuiteBackendMem(t *testing.T) { + suite := test.Suite{ + // NewConfig returns a config for a new temporary backend that will be used in tests. + NewConfig: func() (interface{}, error) { + return &memConfig{}, nil + }, + + // CreateFn is a function that creates a temporary repository for the tests. + Create: func(cfg interface{}) (restic.Backend, error) { + c := cfg.(*memConfig) + if c.be != nil { + ok, err := c.be.Test(restic.Handle{Type: restic.ConfigFile}) + if err != nil { + return nil, err + } + + if ok { + return nil, errors.New("config already exists") + } + } + + c.be = mem.New() + return c.be, nil + }, + + // OpenFn is a function that opens a previously created temporary repository. + Open: func(cfg interface{}) (restic.Backend, error) { + c := cfg.(*memConfig) + if c.be == nil { + c.be = mem.New() + } + return c.be, nil + }, + + // CleanupFn removes data created during the tests. + Cleanup: func(cfg interface{}) error { + // no cleanup needed + return nil + }, + } + + suite.RunTests(t) }