From bf29443ad445b3e7808916526460d07f3108e102 Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Fri, 25 Aug 2023 10:28:09 -0700 Subject: [PATCH] Use interface for test helper functions (#4114) Take an interface as the parameter for test helper functions. This allows them to be used with *testing.B (benchmark) as well as *testing.T (test). --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [x] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- src/internal/kopia/conn_test.go | 2 +- src/internal/tester/context.go | 7 +++---- src/internal/tester/tester.go | 10 +++++++++- src/pkg/repository/repository_test.go | 2 +- src/pkg/storage/testdata/storage.go | 3 +-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/internal/kopia/conn_test.go b/src/internal/kopia/conn_test.go index 36a2bcdfd..b849176dd 100644 --- a/src/internal/kopia/conn_test.go +++ b/src/internal/kopia/conn_test.go @@ -23,7 +23,7 @@ import ( ) func openKopiaRepo( - t *testing.T, + t tester.TestT, ctx context.Context, //revive:disable-line:context-as-argument ) (*conn, error) { st := storeTD.NewPrefixedS3Storage(t) diff --git a/src/internal/tester/context.go b/src/internal/tester/context.go index 7bb5b98c6..1e6e1ccc7 100644 --- a/src/internal/tester/context.go +++ b/src/internal/tester/context.go @@ -3,7 +3,6 @@ package tester import ( "context" "os" - "testing" "github.com/alcionai/clues" "github.com/google/uuid" @@ -11,7 +10,7 @@ import ( "github.com/alcionai/corso/src/pkg/logger" ) -func NewContext(t *testing.T) (context.Context, func()) { +func NewContext(t TestT) (context.Context, func()) { level := logger.LLInfo format := logger.LFText @@ -34,7 +33,7 @@ func NewContext(t *testing.T) (context.Context, func()) { } func WithContext( - t *testing.T, + t TestT, ctx context.Context, //revive:disable-line:context-as-argument ) (context.Context, func()) { ls := logger.Settings{ @@ -48,7 +47,7 @@ func WithContext( } func enrichTestCtx( - t *testing.T, + t TestT, ctx context.Context, //revive:disable-line:context-as-argument ) context.Context { if t == nil { diff --git a/src/internal/tester/tester.go b/src/internal/tester/tester.go index 10cb070f7..909c5b04c 100644 --- a/src/internal/tester/tester.go +++ b/src/internal/tester/tester.go @@ -7,6 +7,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // AreSameFunc asserts whether the two funcs are the same func. @@ -26,8 +27,15 @@ func AreSameFunc(t *testing.T, expect, have any) { ) } +type TestT interface { + Logf(format string, args ...any) + Name() string + TempDir() string + require.TestingT +} + // LogTimeOfTest logs the test name and the time that it was run. -func LogTimeOfTest(t *testing.T) string { +func LogTimeOfTest(t TestT) string { now := time.Now().UTC().Format(time.RFC3339Nano) name := t.Name() diff --git a/src/pkg/repository/repository_test.go b/src/pkg/repository/repository_test.go index b051581f0..8f9725dc2 100644 --- a/src/pkg/repository/repository_test.go +++ b/src/pkg/repository/repository_test.go @@ -126,7 +126,7 @@ func (suite *RepositoryIntegrationSuite) TestInitialize() { table := []struct { name string account account.Account - storage func(*testing.T) storage.Storage + storage func(tester.TestT) storage.Storage errCheck assert.ErrorAssertionFunc }{ { diff --git a/src/pkg/storage/testdata/storage.go b/src/pkg/storage/testdata/storage.go index f2a181d43..d6703af20 100644 --- a/src/pkg/storage/testdata/storage.go +++ b/src/pkg/storage/testdata/storage.go @@ -2,7 +2,6 @@ package testdata import ( "os" - "testing" "github.com/alcionai/clues" "github.com/stretchr/testify/require" @@ -28,7 +27,7 @@ var AWSStorageCredEnvs = []string{ // Uses t.TempDir() to generate a unique config storage and caching directory for this // test. Suites that need to identify this value can retrieve it again from the common // configs. -func NewPrefixedS3Storage(t *testing.T) storage.Storage { +func NewPrefixedS3Storage(t tester.TestT) storage.Storage { now := tester.LogTimeOfTest(t) cfg, err := tconfig.ReadTestConfig()