From a27b7f1370deb1d831af1c977427d5dc48e04d8f Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 21 Apr 2023 21:06:56 +0200 Subject: [PATCH] backend: use generic instead of any type for test suite --- internal/backend/azure/azure_test.go | 18 +++++------- internal/backend/b2/b2_test.go | 17 +++++------- internal/backend/gs/gs_test.go | 19 +++++-------- internal/backend/local/local_test.go | 15 ++++------ internal/backend/mem/mem_backend_test.go | 30 ++++++++++---------- internal/backend/rclone/backend_test.go | 12 ++++---- internal/backend/rest/rest_test.go | 14 ++++------ internal/backend/s3/s3_test.go | 35 +++++++++--------------- internal/backend/sftp/sftp_test.go | 15 ++++------ internal/backend/swift/swift_test.go | 21 ++++++-------- internal/backend/test/benchmarks.go | 8 +++--- internal/backend/test/suite.go | 26 +++++++++--------- internal/backend/test/tests.go | 24 ++++++++-------- 13 files changed, 108 insertions(+), 146 deletions(-) diff --git a/internal/backend/azure/azure_test.go b/internal/backend/azure/azure_test.go index 4e33e8a39..f7492ae1b 100644 --- a/internal/backend/azure/azure_test.go +++ b/internal/backend/azure/azure_test.go @@ -18,21 +18,21 @@ import ( rtest "github.com/restic/restic/internal/test" ) -func newAzureTestSuite(t testing.TB) *test.Suite { +func newAzureTestSuite(t testing.TB) *test.Suite[azure.Config] { tr, err := backend.Transport(backend.TransportOptions{}) if err != nil { t.Fatalf("cannot create transport for tests: %v", err) } - return &test.Suite{ + return &test.Suite[azure.Config]{ // do not use excessive data MinimalData: true, // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (azure.Config, error) { cfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY")) if err != nil { - return nil, err + return azure.Config{}, err } cfg.AccountName = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_NAME") @@ -42,9 +42,7 @@ func newAzureTestSuite(t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(azure.Config) - + Create: func(cfg azure.Config) (restic.Backend, error) { ctx := context.TODO() be, err := azure.Create(ctx, cfg, tr) if err != nil { @@ -64,15 +62,13 @@ func newAzureTestSuite(t testing.TB) *test.Suite { }, // OpenFn is a function that opens a previously created temporary repository. - Open: func(config interface{}) (restic.Backend, error) { - cfg := config.(azure.Config) + Open: func(cfg azure.Config) (restic.Backend, error) { ctx := context.TODO() return azure.Open(ctx, cfg, tr) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { - cfg := config.(azure.Config) + Cleanup: func(cfg azure.Config) error { ctx := context.TODO() be, err := azure.Open(ctx, cfg, tr) if err != nil { diff --git a/internal/backend/b2/b2_test.go b/internal/backend/b2/b2_test.go index d7112410a..f634852d0 100644 --- a/internal/backend/b2/b2_test.go +++ b/internal/backend/b2/b2_test.go @@ -16,13 +16,13 @@ import ( rtest "github.com/restic/restic/internal/test" ) -func newB2TestSuite(t testing.TB) *test.Suite { +func newB2TestSuite(t testing.TB) *test.Suite[b2.Config] { tr, err := backend.Transport(backend.TransportOptions{}) if err != nil { t.Fatalf("cannot create transport for tests: %v", err) } - return &test.Suite{ + return &test.Suite[b2.Config]{ // do not use excessive data MinimalData: true, @@ -30,10 +30,10 @@ func newB2TestSuite(t testing.TB) *test.Suite { WaitForDelayedRemoval: 10 * time.Second, // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (b2.Config, error) { cfg, err := b2.ParseConfig(os.Getenv("RESTIC_TEST_B2_REPOSITORY")) if err != nil { - return nil, err + return b2.Config{}, err } cfg.AccountID = os.Getenv("RESTIC_TEST_B2_ACCOUNT_ID") @@ -43,20 +43,17 @@ func newB2TestSuite(t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(b2.Config) + Create: func(cfg b2.Config) (restic.Backend, error) { return b2.Create(context.Background(), cfg, tr) }, // OpenFn is a function that opens a previously created temporary repository. - Open: func(config interface{}) (restic.Backend, error) { - cfg := config.(b2.Config) + Open: func(cfg b2.Config) (restic.Backend, error) { return b2.Open(context.Background(), cfg, tr) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { - cfg := config.(b2.Config) + Cleanup: func(cfg b2.Config) error { be, err := b2.Open(context.Background(), cfg, tr) if err != nil { return err diff --git a/internal/backend/gs/gs_test.go b/internal/backend/gs/gs_test.go index 20b51e5cc..b9015e24c 100644 --- a/internal/backend/gs/gs_test.go +++ b/internal/backend/gs/gs_test.go @@ -15,21 +15,21 @@ import ( rtest "github.com/restic/restic/internal/test" ) -func newGSTestSuite(t testing.TB) *test.Suite { +func newGSTestSuite(t testing.TB) *test.Suite[gs.Config] { tr, err := backend.Transport(backend.TransportOptions{}) if err != nil { t.Fatalf("cannot create transport for tests: %v", err) } - return &test.Suite{ + return &test.Suite[gs.Config]{ // do not use excessive data MinimalData: true, // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (gs.Config, error) { cfg, err := gs.ParseConfig(os.Getenv("RESTIC_TEST_GS_REPOSITORY")) if err != nil { - return nil, err + return gs.Config{}, err } cfg.ProjectID = os.Getenv("RESTIC_TEST_GS_PROJECT_ID") @@ -38,9 +38,7 @@ func newGSTestSuite(t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(gs.Config) - + Create: func(cfg gs.Config) (restic.Backend, error) { be, err := gs.Create(context.Background(), cfg, tr) if err != nil { return nil, err @@ -59,15 +57,12 @@ func newGSTestSuite(t testing.TB) *test.Suite { }, // OpenFn is a function that opens a previously created temporary repository. - Open: func(config interface{}) (restic.Backend, error) { - cfg := config.(gs.Config) + Open: func(cfg gs.Config) (restic.Backend, error) { return gs.Open(cfg, tr) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { - cfg := config.(gs.Config) - + Cleanup: func(cfg gs.Config) error { be, err := gs.Open(cfg, tr) if err != nil { return err diff --git a/internal/backend/local/local_test.go b/internal/backend/local/local_test.go index 495f220a0..98afc5963 100644 --- a/internal/backend/local/local_test.go +++ b/internal/backend/local/local_test.go @@ -12,10 +12,10 @@ import ( rtest "github.com/restic/restic/internal/test" ) -func newTestSuite(t testing.TB) *test.Suite { - return &test.Suite{ +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() (interface{}, error) { + NewConfig: func() (local.Config, error) { dir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-local-") if err != nil { t.Fatal(err) @@ -31,20 +31,17 @@ func newTestSuite(t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(local.Config) + 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(config interface{}) (restic.Backend, error) { - cfg := config.(local.Config) + Open: func(cfg local.Config) (restic.Backend, error) { return local.Open(context.TODO(), cfg) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { - cfg := config.(local.Config) + Cleanup: func(cfg local.Config) error { if !rtest.TestCleanupTempDirs { t.Logf("leaving test backend dir at %v", cfg.Path) } diff --git a/internal/backend/mem/mem_backend_test.go b/internal/backend/mem/mem_backend_test.go index 819c6a2b6..7b862e314 100644 --- a/internal/backend/mem/mem_backend_test.go +++ b/internal/backend/mem/mem_backend_test.go @@ -15,19 +15,18 @@ type memConfig struct { be restic.Backend } -func newTestSuite() *test.Suite { - return &test.Suite{ +func newTestSuite() *test.Suite[*memConfig] { + return &test.Suite[*memConfig]{ // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (*memConfig, 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 { - _, err := c.be.Stat(context.TODO(), restic.Handle{Type: restic.ConfigFile}) - if err != nil && !c.be.IsNotExist(err) { + 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 } @@ -36,21 +35,20 @@ func newTestSuite() *test.Suite { } } - c.be = mem.New() - return c.be, nil + cfg.be = mem.New() + return cfg.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() + Open: func(cfg *memConfig) (restic.Backend, error) { + if cfg.be == nil { + cfg.be = mem.New() } - return c.be, nil + return cfg.be, nil }, // CleanupFn removes data created during the tests. - Cleanup: func(cfg interface{}) error { + Cleanup: func(cfg *memConfig) error { // no cleanup needed return nil }, diff --git a/internal/backend/rclone/backend_test.go b/internal/backend/rclone/backend_test.go index 12fed6274..4bff46d77 100644 --- a/internal/backend/rclone/backend_test.go +++ b/internal/backend/rclone/backend_test.go @@ -12,12 +12,12 @@ import ( rtest "github.com/restic/restic/internal/test" ) -func newTestSuite(t testing.TB) *test.Suite { +func newTestSuite(t testing.TB) *test.Suite[rclone.Config] { dir := rtest.TempDir(t) - return &test.Suite{ + return &test.Suite[rclone.Config]{ // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (rclone.Config, error) { t.Logf("use backend at %v", dir) cfg := rclone.NewConfig() cfg.Remote = dir @@ -25,9 +25,8 @@ func newTestSuite(t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { + Create: func(cfg rclone.Config) (restic.Backend, error) { t.Logf("Create()") - cfg := config.(rclone.Config) be, err := rclone.Create(context.TODO(), cfg) var e *exec.Error if errors.As(err, &e) && e.Err == exec.ErrNotFound { @@ -38,9 +37,8 @@ func newTestSuite(t testing.TB) *test.Suite { }, // OpenFn is a function that opens a previously created temporary repository. - Open: func(config interface{}) (restic.Backend, error) { + Open: func(cfg rclone.Config) (restic.Backend, error) { t.Logf("Open()") - cfg := config.(rclone.Config) return rclone.Open(cfg, nil) }, } diff --git a/internal/backend/rest/rest_test.go b/internal/backend/rest/rest_test.go index edbb3141c..10e7b5b5b 100644 --- a/internal/backend/rest/rest_test.go +++ b/internal/backend/rest/rest_test.go @@ -67,36 +67,34 @@ func runRESTServer(ctx context.Context, t testing.TB, dir string) (*url.URL, fun return url, cleanup } -func newTestSuite(_ context.Context, t testing.TB, url *url.URL, minimalData bool) *test.Suite { +func newTestSuite(_ context.Context, t testing.TB, url *url.URL, minimalData bool) *test.Suite[rest.Config] { tr, err := backend.Transport(backend.TransportOptions{}) if err != nil { t.Fatalf("cannot create transport for tests: %v", err) } - return &test.Suite{ + return &test.Suite[rest.Config]{ MinimalData: minimalData, // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (rest.Config, error) { cfg := rest.NewConfig() cfg.URL = url return cfg, nil }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(rest.Config) + Create: func(cfg rest.Config) (restic.Backend, error) { return rest.Create(context.TODO(), cfg, tr) }, // OpenFn is a function that opens a previously created temporary repository. - Open: func(config interface{}) (restic.Backend, error) { - cfg := config.(rest.Config) + Open: func(cfg rest.Config) (restic.Backend, error) { return rest.Open(cfg, tr) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { + Cleanup: func(cfg rest.Config) error { return nil }, } diff --git a/internal/backend/s3/s3_test.go b/internal/backend/s3/s3_test.go index 6df81232a..5c91e9f7f 100644 --- a/internal/backend/s3/s3_test.go +++ b/internal/backend/s3/s3_test.go @@ -120,15 +120,15 @@ func createS3(t testing.TB, cfg MinioTestConfig, tr http.RoundTripper) (be resti return be, err } -func newMinioTestSuite(ctx context.Context, t testing.TB) *test.Suite { +func newMinioTestSuite(ctx context.Context, t testing.TB) *test.Suite[MinioTestConfig] { tr, err := backend.Transport(backend.TransportOptions{}) if err != nil { t.Fatalf("cannot create transport for tests: %v", err) } - return &test.Suite{ + return &test.Suite[MinioTestConfig]{ // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (MinioTestConfig, error) { cfg := MinioTestConfig{} cfg.tempdir = rtest.TempDir(t) @@ -146,9 +146,7 @@ func newMinioTestSuite(ctx context.Context, t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(MinioTestConfig) - + Create: func(cfg MinioTestConfig) (restic.Backend, error) { be, err := createS3(t, cfg, tr) if err != nil { return nil, err @@ -167,14 +165,12 @@ func newMinioTestSuite(ctx context.Context, t testing.TB) *test.Suite { }, // OpenFn is a function that opens a previously created temporary repository. - Open: func(config interface{}) (restic.Backend, error) { - cfg := config.(MinioTestConfig) + Open: func(cfg MinioTestConfig) (restic.Backend, error) { return s3.Open(ctx, cfg.Config, tr) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { - cfg := config.(MinioTestConfig) + Cleanup: func(cfg MinioTestConfig) error { if cfg.stopServer != nil { cfg.stopServer() } @@ -217,21 +213,21 @@ func BenchmarkBackendMinio(t *testing.B) { newMinioTestSuite(ctx, t).RunBenchmarks(t) } -func newS3TestSuite(t testing.TB) *test.Suite { +func newS3TestSuite(t testing.TB) *test.Suite[s3.Config] { tr, err := backend.Transport(backend.TransportOptions{}) if err != nil { t.Fatalf("cannot create transport for tests: %v", err) } - return &test.Suite{ + return &test.Suite[s3.Config]{ // do not use excessive data MinimalData: true, // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (s3.Config, error) { cfg, err := s3.ParseConfig(os.Getenv("RESTIC_TEST_S3_REPOSITORY")) if err != nil { - return nil, err + return s3.Config{}, err } cfg.KeyID = os.Getenv("RESTIC_TEST_S3_KEY") @@ -241,9 +237,7 @@ func newS3TestSuite(t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(s3.Config) - + Create: func(cfg s3.Config) (restic.Backend, error) { be, err := s3.Create(context.TODO(), cfg, tr) if err != nil { return nil, err @@ -262,15 +256,12 @@ func newS3TestSuite(t testing.TB) *test.Suite { }, // OpenFn is a function that opens a previously created temporary repository. - Open: func(config interface{}) (restic.Backend, error) { - cfg := config.(s3.Config) + Open: func(cfg s3.Config) (restic.Backend, error) { return s3.Open(context.TODO(), cfg, tr) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { - cfg := config.(s3.Config) - + Cleanup: func(cfg s3.Config) error { be, err := s3.Open(context.TODO(), cfg, tr) if err != nil { return err diff --git a/internal/backend/sftp/sftp_test.go b/internal/backend/sftp/sftp_test.go index 0dbcd291c..34ad06554 100644 --- a/internal/backend/sftp/sftp_test.go +++ b/internal/backend/sftp/sftp_test.go @@ -29,10 +29,10 @@ func findSFTPServerBinary() string { var sftpServer = findSFTPServerBinary() -func newTestSuite(t testing.TB) *test.Suite { - return &test.Suite{ +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() (interface{}, error) { + NewConfig: func() (sftp.Config, error) { dir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-sftp-") if err != nil { t.Fatal(err) @@ -49,20 +49,17 @@ func newTestSuite(t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(sftp.Config) + 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(config interface{}) (restic.Backend, error) { - cfg := config.(sftp.Config) + Open: func(cfg sftp.Config) (restic.Backend, error) { return sftp.Open(context.TODO(), cfg) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { - cfg := config.(sftp.Config) + Cleanup: func(cfg sftp.Config) error { if !rtest.TestCleanupTempDirs { t.Logf("leaving test backend dir at %v", cfg.Path) } diff --git a/internal/backend/swift/swift_test.go b/internal/backend/swift/swift_test.go index 18c8db93c..316028b0c 100644 --- a/internal/backend/swift/swift_test.go +++ b/internal/backend/swift/swift_test.go @@ -15,13 +15,13 @@ import ( rtest "github.com/restic/restic/internal/test" ) -func newSwiftTestSuite(t testing.TB) *test.Suite { +func newSwiftTestSuite(t testing.TB) *test.Suite[swift.Config] { tr, err := backend.Transport(backend.TransportOptions{}) if err != nil { t.Fatalf("cannot create transport for tests: %v", err) } - return &test.Suite{ + return &test.Suite[swift.Config]{ // do not use excessive data MinimalData: true, @@ -42,14 +42,14 @@ func newSwiftTestSuite(t testing.TB) *test.Suite { }, // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig: func() (interface{}, error) { + NewConfig: func() (swift.Config, error) { cfg, err := swift.ParseConfig(os.Getenv("RESTIC_TEST_SWIFT")) if err != nil { - return nil, err + return swift.Config{}, err } if err = swift.ApplyEnvironment("RESTIC_TEST_", &cfg); err != nil { - return nil, err + return swift.Config{}, err } cfg.Prefix += fmt.Sprintf("/test-%d", time.Now().UnixNano()) t.Logf("using prefix %v", cfg.Prefix) @@ -57,9 +57,7 @@ func newSwiftTestSuite(t testing.TB) *test.Suite { }, // CreateFn is a function that creates a temporary repository for the tests. - Create: func(config interface{}) (restic.Backend, error) { - cfg := config.(swift.Config) - + Create: func(cfg swift.Config) (restic.Backend, error) { be, err := swift.Open(context.TODO(), cfg, tr) if err != nil { return nil, err @@ -78,15 +76,12 @@ func newSwiftTestSuite(t testing.TB) *test.Suite { }, // OpenFn is a function that opens a previously created temporary repository. - Open: func(config interface{}) (restic.Backend, error) { - cfg := config.(swift.Config) + Open: func(cfg swift.Config) (restic.Backend, error) { return swift.Open(context.TODO(), cfg, tr) }, // CleanupFn removes data created during the tests. - Cleanup: func(config interface{}) error { - cfg := config.(swift.Config) - + Cleanup: func(cfg swift.Config) error { be, err := swift.Open(context.TODO(), cfg, tr) if err != nil { return err diff --git a/internal/backend/test/benchmarks.go b/internal/backend/test/benchmarks.go index b977eb682..150ef3987 100644 --- a/internal/backend/test/benchmarks.go +++ b/internal/backend/test/benchmarks.go @@ -29,7 +29,7 @@ func remove(t testing.TB, be restic.Backend, h restic.Handle) { // BenchmarkLoadFile benchmarks the Load() method of a backend by // loading a complete file. -func (s *Suite) BenchmarkLoadFile(t *testing.B) { +func (s *Suite[C]) BenchmarkLoadFile(t *testing.B) { be := s.open(t) defer s.close(t, be) @@ -64,7 +64,7 @@ func (s *Suite) BenchmarkLoadFile(t *testing.B) { // BenchmarkLoadPartialFile benchmarks the Load() method of a backend by // loading the remainder of a file starting at a given offset. -func (s *Suite) BenchmarkLoadPartialFile(t *testing.B) { +func (s *Suite[C]) BenchmarkLoadPartialFile(t *testing.B) { be := s.open(t) defer s.close(t, be) @@ -101,7 +101,7 @@ func (s *Suite) BenchmarkLoadPartialFile(t *testing.B) { // BenchmarkLoadPartialFileOffset benchmarks the Load() method of a // backend by loading a number of bytes of a file starting at a given offset. -func (s *Suite) BenchmarkLoadPartialFileOffset(t *testing.B) { +func (s *Suite[C]) BenchmarkLoadPartialFileOffset(t *testing.B) { be := s.open(t) defer s.close(t, be) @@ -139,7 +139,7 @@ func (s *Suite) BenchmarkLoadPartialFileOffset(t *testing.B) { } // BenchmarkSave benchmarks the Save() method of a backend. -func (s *Suite) BenchmarkSave(t *testing.B) { +func (s *Suite[C]) BenchmarkSave(t *testing.B) { be := s.open(t) defer s.close(t, be) diff --git a/internal/backend/test/suite.go b/internal/backend/test/suite.go index 45c6d96bd..fcb1b00b4 100644 --- a/internal/backend/test/suite.go +++ b/internal/backend/test/suite.go @@ -11,21 +11,21 @@ import ( ) // Suite implements a test suite for restic backends. -type Suite struct { +type Suite[C any] struct { // Config should be used to configure the backend. - Config interface{} + Config C // NewConfig returns a config for a new temporary backend that will be used in tests. - NewConfig func() (interface{}, error) + NewConfig func() (C, error) // CreateFn is a function that creates a temporary repository for the tests. - Create func(cfg interface{}) (restic.Backend, error) + Create func(cfg C) (restic.Backend, error) // OpenFn is a function that opens a previously created temporary repository. - Open func(cfg interface{}) (restic.Backend, error) + Open func(cfg C) (restic.Backend, error) // CleanupFn removes data created during the tests. - Cleanup func(cfg interface{}) error + Cleanup func(cfg C) error // MinimalData instructs the tests to not use excessive data. MinimalData bool @@ -40,7 +40,7 @@ type Suite struct { } // RunTests executes all defined tests as subtests of t. -func (s *Suite) RunTests(t *testing.T) { +func (s *Suite[C]) RunTests(t *testing.T) { var err error s.Config, err = s.NewConfig() if err != nil { @@ -72,7 +72,7 @@ type testFunction struct { Fn func(*testing.T) } -func (s *Suite) testFuncs(t testing.TB) (funcs []testFunction) { +func (s *Suite[C]) testFuncs(t testing.TB) (funcs []testFunction) { tpe := reflect.TypeOf(s) v := reflect.ValueOf(s) @@ -107,7 +107,7 @@ type benchmarkFunction struct { Fn func(*testing.B) } -func (s *Suite) benchmarkFuncs(t testing.TB) (funcs []benchmarkFunction) { +func (s *Suite[C]) benchmarkFuncs(t testing.TB) (funcs []benchmarkFunction) { tpe := reflect.TypeOf(s) v := reflect.ValueOf(s) @@ -138,7 +138,7 @@ func (s *Suite) benchmarkFuncs(t testing.TB) (funcs []benchmarkFunction) { } // RunBenchmarks executes all defined benchmarks as subtests of b. -func (s *Suite) RunBenchmarks(b *testing.B) { +func (s *Suite[C]) RunBenchmarks(b *testing.B) { var err error s.Config, err = s.NewConfig() if err != nil { @@ -163,7 +163,7 @@ func (s *Suite) RunBenchmarks(b *testing.B) { } } -func (s *Suite) create(t testing.TB) restic.Backend { +func (s *Suite[C]) create(t testing.TB) restic.Backend { be, err := s.Create(s.Config) if err != nil { t.Fatal(err) @@ -171,7 +171,7 @@ func (s *Suite) create(t testing.TB) restic.Backend { return be } -func (s *Suite) open(t testing.TB) restic.Backend { +func (s *Suite[C]) open(t testing.TB) restic.Backend { be, err := s.Open(s.Config) if err != nil { t.Fatal(err) @@ -179,7 +179,7 @@ func (s *Suite) open(t testing.TB) restic.Backend { return be } -func (s *Suite) close(t testing.TB, be restic.Backend) { +func (s *Suite[C]) close(t testing.TB, be restic.Backend) { err := be.Close() if err != nil { t.Fatal(err) diff --git a/internal/backend/test/tests.go b/internal/backend/test/tests.go index a9514bf6b..b9dcc9ba5 100644 --- a/internal/backend/test/tests.go +++ b/internal/backend/test/tests.go @@ -38,7 +38,7 @@ func beTest(ctx context.Context, be restic.Backend, h restic.Handle) (bool, erro // TestCreateWithConfig tests that creating a backend in a location which already // has a config file fails. -func (s *Suite) TestCreateWithConfig(t *testing.T) { +func (s *Suite[C]) TestCreateWithConfig(t *testing.T) { b := s.open(t) defer s.close(t, b) @@ -70,7 +70,7 @@ func (s *Suite) TestCreateWithConfig(t *testing.T) { } // TestLocation tests that a location string is returned. -func (s *Suite) TestLocation(t *testing.T) { +func (s *Suite[C]) TestLocation(t *testing.T) { b := s.open(t) defer s.close(t, b) @@ -81,7 +81,7 @@ func (s *Suite) TestLocation(t *testing.T) { } // TestConfig saves and loads a config from the backend. -func (s *Suite) TestConfig(t *testing.T) { +func (s *Suite[C]) TestConfig(t *testing.T) { b := s.open(t) defer s.close(t, b) @@ -118,7 +118,7 @@ func (s *Suite) TestConfig(t *testing.T) { } // TestLoad tests the backend's Load function. -func (s *Suite) TestLoad(t *testing.T) { +func (s *Suite[C]) TestLoad(t *testing.T) { seedRand(t) b := s.open(t) @@ -223,7 +223,7 @@ func (s *Suite) TestLoad(t *testing.T) { } // TestList makes sure that the backend implements List() pagination correctly. -func (s *Suite) TestList(t *testing.T) { +func (s *Suite[C]) TestList(t *testing.T) { seedRand(t) numTestFiles := rand.Intn(20) + 20 @@ -326,7 +326,7 @@ func (s *Suite) TestList(t *testing.T) { } // TestListCancel tests that the context is respected and the error is returned by List. -func (s *Suite) TestListCancel(t *testing.T) { +func (s *Suite[C]) TestListCancel(t *testing.T) { seedRand(t) numTestFiles := 5 @@ -466,7 +466,7 @@ func (ec errorCloser) Rewind() error { } // TestSave tests saving data in the backend. -func (s *Suite) TestSave(t *testing.T) { +func (s *Suite[C]) TestSave(t *testing.T) { seedRand(t) b := s.open(t) @@ -582,7 +582,7 @@ func (r *incompleteByteReader) Length() int64 { } // TestSaveError tests saving data in the backend. -func (s *Suite) TestSaveError(t *testing.T) { +func (s *Suite[C]) TestSaveError(t *testing.T) { seedRand(t) b := s.open(t) @@ -621,7 +621,7 @@ func (b *wrongByteReader) Hash() []byte { } // TestSaveWrongHash tests that uploads with a wrong hash fail -func (s *Suite) TestSaveWrongHash(t *testing.T) { +func (s *Suite[C]) TestSaveWrongHash(t *testing.T) { seedRand(t) b := s.open(t) @@ -679,7 +679,7 @@ func testLoad(b restic.Backend, h restic.Handle) error { }) } -func (s *Suite) delayedRemove(t testing.TB, be restic.Backend, handles ...restic.Handle) error { +func (s *Suite[C]) delayedRemove(t testing.TB, be restic.Backend, handles ...restic.Handle) error { // Some backend (swift, I'm looking at you) may implement delayed // removal of data. Let's wait a bit if this happens. @@ -746,7 +746,7 @@ func delayedList(t testing.TB, b restic.Backend, tpe restic.FileType, max int, m } // TestBackend tests all functions of the backend. -func (s *Suite) TestBackend(t *testing.T) { +func (s *Suite[C]) TestBackend(t *testing.T) { b := s.open(t) defer s.close(t, b) @@ -867,7 +867,7 @@ func (s *Suite) TestBackend(t *testing.T) { } // TestZZZDelete tests the Delete function. The name ensures that this test is executed last. -func (s *Suite) TestZZZDelete(t *testing.T) { +func (s *Suite[C]) TestZZZDelete(t *testing.T) { if !test.TestCleanupTempDirs { t.Skipf("not removing backend, TestCleanupTempDirs is false") }