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

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