backend: Adjust tests to use the Factory to instantiate the backend

This drastically reduces the amount of duplicated test code.
This commit is contained in:
Michael Eischer 2023-06-08 16:53:55 +02:00
parent 3d3bb88745
commit 13a8b5822f
13 changed files with 119 additions and 423 deletions

View file

@ -8,7 +8,6 @@ import (
"github.com/restic/restic/internal/backend/local"
"github.com/restic/restic/internal/backend/test"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
)
@ -16,11 +15,7 @@ func newTestSuite(t testing.TB) *test.Suite[local.Config] {
return &test.Suite[local.Config]{
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (*local.Config, error) {
dir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-local-")
if err != nil {
t.Fatal(err)
}
dir := rtest.TempDir(t)
t.Logf("create new backend at %v", dir)
cfg := &local.Config{
@ -30,25 +25,7 @@ func newTestSuite(t testing.TB) *test.Suite[local.Config] {
return cfg, nil
},
// CreateFn is a function that creates a temporary repository for the tests.
Create: func(cfg local.Config) (restic.Backend, error) {
return local.Create(context.TODO(), cfg)
},
// OpenFn is a function that opens a previously created temporary repository.
Open: func(cfg local.Config) (restic.Backend, error) {
return local.Open(context.TODO(), cfg)
},
// CleanupFn removes data created during the tests.
Cleanup: func(cfg local.Config) error {
if !rtest.TestCleanupTempDirs {
t.Logf("leaving test backend dir at %v", cfg.Path)
}
rtest.RemoveAll(t, cfg.Path)
return nil
},
Factory: local.NewFactory(),
}
}