From e190e55f4f7a44254139ba212bdb6146f6f2ab07 Mon Sep 17 00:00:00 2001 From: Mark Hughes Date: Mon, 14 Aug 2023 11:50:51 +0100 Subject: [PATCH] Provide access to the testing.T log function in the context --- suite.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/suite.go b/suite.go index 16bfd15e..b6af9179 100644 --- a/suite.go +++ b/suite.go @@ -522,6 +522,7 @@ func (s *suite) runPickle(pickle *messages.Pickle) (err error) { if s.testingT != nil { // Running scenario as a subtest. s.testingT.Run(pickle.Name, func(t *testing.T) { + ctx = setContextLogger(ctx, t) ctx, err = s.runSteps(ctx, pickle, pickle.Steps) if s.shouldFail(err) { t.Errorf("%+v", err) @@ -536,3 +537,22 @@ func (s *suite) runPickle(pickle *messages.Pickle) (err error) { return err } + +type TestLogger interface { + Log(args ...interface{}) + Logf(format string, args ...interface{}) +} + +type logCtxVal struct{} + +func setContextLogger(ctx context.Context, t TestLogger) context.Context { + return context.WithValue(ctx, logCtxVal{}, t) +} + +func GetTestLogger(ctx context.Context) TestLogger { + t, ok := ctx.Value(logCtxVal{}).(TestLogger) + if !ok { + return nil + } + return t +}