mirror of
https://github.com/restic/restic.git
synced 2025-08-12 01:19:47 +00:00
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:
@@ -1,58 +1,20 @@
|
||||
package mem_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
|
||||
"github.com/restic/restic/internal/backend/mem"
|
||||
"github.com/restic/restic/internal/backend/test"
|
||||
)
|
||||
|
||||
type memConfig struct {
|
||||
be restic.Backend
|
||||
}
|
||||
|
||||
func newTestSuite() *test.Suite[*memConfig] {
|
||||
return &test.Suite[*memConfig]{
|
||||
func newTestSuite() *test.Suite[struct{}] {
|
||||
return &test.Suite[struct{}]{
|
||||
// NewConfig returns a config for a new temporary backend that will be used in tests.
|
||||
NewConfig: func() (**memConfig, error) {
|
||||
cfg := &memConfig{}
|
||||
return &cfg, nil
|
||||
NewConfig: func() (*struct{}, error) {
|
||||
return &struct{}{}, nil
|
||||
},
|
||||
|
||||
// CreateFn is a function that creates a temporary repository for the tests.
|
||||
Create: func(cfg *memConfig) (restic.Backend, error) {
|
||||
if cfg.be != nil {
|
||||
_, err := cfg.be.Stat(context.TODO(), restic.Handle{Type: restic.ConfigFile})
|
||||
if err != nil && !cfg.be.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
return nil, errors.New("config already exists")
|
||||
}
|
||||
}
|
||||
|
||||
cfg.be = mem.New()
|
||||
return cfg.be, nil
|
||||
},
|
||||
|
||||
// OpenFn is a function that opens a previously created temporary repository.
|
||||
Open: func(cfg *memConfig) (restic.Backend, error) {
|
||||
if cfg.be == nil {
|
||||
cfg.be = mem.New()
|
||||
}
|
||||
return cfg.be, nil
|
||||
},
|
||||
|
||||
// CleanupFn removes data created during the tests.
|
||||
Cleanup: func(cfg *memConfig) error {
|
||||
// no cleanup needed
|
||||
return nil
|
||||
},
|
||||
Factory: mem.NewFactory(),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user