@@ -17,6 +17,12 @@ import (
1717type P interface {
1818 CommonT
1919
20+ // WithCtx makes a copy of P with a specific context.
21+ // The ctx must match the test-scope of the existing context.
22+ // This function is used to create a P with annotated context, e.g. a specific resource.
23+ // The logger may be annotated with additional arguments.
24+ WithCtx (ctx context.Context , args ... any ) P
25+
2026 // TempDir creates a temporary directory, and returns the file-path.
2127 // This directory is cleaned up at the end of the package,
2228 // and can be shared safely between tests that run in that package scope.
@@ -42,7 +48,7 @@ type implP struct {
4248 scopeName string
4349
4450 // logger is used for logging. Regular test errors will also be redirected to get logged here.
45- logger Logger
51+ logger log. Logger
4652
4753 // fail will be called to register a critical failure.
4854 // The implementer can choose to panic, crit-log, exit, etc. as preferred.
@@ -103,7 +109,7 @@ func (t *implP) Name() string {
103109 return t .scopeName
104110}
105111
106- func (t * implP ) Logger () Logger {
112+ func (t * implP ) Logger () log. Logger {
107113 return t .logger
108114}
109115
@@ -115,6 +121,34 @@ func (t *implP) Ctx() context.Context {
115121 return t .ctx
116122}
117123
124+ type wrapP struct {
125+ ctx context.Context
126+ logger log.Logger
127+ req * require.Assertions
128+ P
129+ }
130+
131+ var _ P = (* wrapP )(nil )
132+
133+ func (p * wrapP ) Ctx () context.Context {
134+ return p .ctx
135+ }
136+
137+ func (p * wrapP ) Logger () log.Logger {
138+ return p .logger
139+ }
140+
141+ func (t * implP ) WithCtx (ctx context.Context , args ... any ) P {
142+ expected := TestScope (t .ctx )
143+ got := TestScope (ctx )
144+ t .req .Equal (expected , got , "cannot replace context with different test-scope" )
145+ logger := t .logger .New (args ... )
146+ logger .SetContext (ctx )
147+ out := & wrapP {ctx : ctx , logger : logger , P : t }
148+ out .req = require .New (out )
149+ return out
150+ }
151+
118152func (t * implP ) Require () * require.Assertions {
119153 return t .req
120154}
@@ -166,9 +200,9 @@ func NewP(ctx context.Context, logger log.Logger, onFail func()) P {
166200 ctx , cancel := context .WithCancel (ctx )
167201 out := & implP {
168202 scopeName : "pkg" ,
169- logger : & pkgLogger { logger } ,
203+ logger : logger ,
170204 fail : onFail ,
171- ctx : ctx ,
205+ ctx : AddTestScope ( ctx , "pkg" ) ,
172206 cancel : cancel ,
173207 }
174208 out .req = require .New (out )
0 commit comments