corso/src/internal/tester/context.go
ashmrtn bf29443ad4
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?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [x] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Test Plan

- [x] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
2023-08-25 17:28:09 +00:00

71 lines
1.4 KiB
Go

package tester
import (
"context"
"os"
"github.com/alcionai/clues"
"github.com/google/uuid"
"github.com/alcionai/corso/src/pkg/logger"
)
func NewContext(t TestT) (context.Context, func()) {
level := logger.LLInfo
format := logger.LFText
for _, a := range os.Args {
if a == "-test.v=true" {
level = logger.LLDebug
}
}
ls := logger.Settings{
Level: level,
Format: format,
}
//nolint:forbidigo
ctx, _ := logger.CtxOrSeed(context.Background(), ls)
ctx = enrichTestCtx(t, ctx)
return ctx, func() { logger.Flush(ctx) }
}
func WithContext(
t TestT,
ctx context.Context, //revive:disable-line:context-as-argument
) (context.Context, func()) {
ls := logger.Settings{
Level: logger.LLDebug,
Format: logger.LFText,
}
ctx, _ = logger.CtxOrSeed(ctx, ls)
ctx = enrichTestCtx(t, ctx)
return ctx, func() { logger.Flush(ctx) }
}
func enrichTestCtx(
t TestT,
ctx context.Context, //revive:disable-line:context-as-argument
) context.Context {
if t == nil {
return ctx
}
// ensure logs can be easily associated with each test
LogTimeOfTest(t)
ctx = clues.Add(
ctx,
// the actual test name, in case you want to look up
// logs correlated to a certain test.
"test_name", t.Name(),
// an arbitrary uuid might be easier to match on when
// looking up logs, in case of common log test names.
"test_uuid", uuid.NewString())
return ctx
}