@@ -259,45 +259,8 @@ func Init(tb TB, opts ...InitOption) TContext {
259
259
260
260
ctx := interruptCtx
261
261
if c .PerTestOutput {
262
- config := ktesting .NewConfig (
263
- ktesting .AnyToString (func (v interface {}) string {
264
- // For basic types where the string
265
- // representation is "obvious" we use
266
- // fmt.Sprintf because format.Object always
267
- // adds a <"type"> prefix, which is too long
268
- // for simple values.
269
- switch v := v .(type ) {
270
- case int , int32 , int64 , uint , uint32 , uint64 , float32 , float64 , bool :
271
- return fmt .Sprintf ("%v" , v )
272
- case string :
273
- return v
274
- default :
275
- return strings .TrimSpace (format .Object (v , 1 ))
276
- }
277
- }),
278
- ktesting .VerbosityFlagName ("v" ),
279
- ktesting .VModuleFlagName ("vmodule" ),
280
- ktesting .BufferLogs (c .BufferLogs ),
281
- )
282
-
283
- // Copy klog settings instead of making the ktesting logger
284
- // configurable directly.
285
- var fs flag.FlagSet
286
- config .AddFlags (& fs )
287
- for _ , name := range []string {"v" , "vmodule" } {
288
- from := flag .CommandLine .Lookup (name )
289
- to := fs .Lookup (name )
290
- if err := to .Value .Set (from .Value .String ()); err != nil {
291
- panic (err )
292
- }
293
- }
294
-
295
- // Ensure consistent logging: this klog.Logger writes to tb, adding the
296
- // date/time header, and our own wrapper emulates that behavior for
297
- // Log/Logf/...
298
- logger := ktesting .NewLogger (tb , config )
262
+ logger := newLogger (tb , c .BufferLogs )
299
263
ctx = klog .NewContext (interruptCtx , logger )
300
-
301
264
tb = withKlogHeader (tb )
302
265
}
303
266
@@ -317,6 +280,47 @@ func Init(tb TB, opts ...InitOption) TContext {
317
280
return WithCancel (InitCtx (ctx , tb ))
318
281
}
319
282
283
+ func newLogger (tb TB , bufferLogs bool ) klog.Logger {
284
+ config := ktesting .NewConfig (
285
+ ktesting .AnyToString (func (v interface {}) string {
286
+ // For basic types where the string
287
+ // representation is "obvious" we use
288
+ // fmt.Sprintf because format.Object always
289
+ // adds a <"type"> prefix, which is too long
290
+ // for simple values.
291
+ switch v := v .(type ) {
292
+ case int , int32 , int64 , uint , uint32 , uint64 , float32 , float64 , bool :
293
+ return fmt .Sprintf ("%v" , v )
294
+ case string :
295
+ return v
296
+ default :
297
+ return strings .TrimSpace (format .Object (v , 1 ))
298
+ }
299
+ }),
300
+ ktesting .VerbosityFlagName ("v" ),
301
+ ktesting .VModuleFlagName ("vmodule" ),
302
+ ktesting .BufferLogs (bufferLogs ),
303
+ )
304
+
305
+ // Copy klog settings instead of making the ktesting logger
306
+ // configurable directly.
307
+ var fs flag.FlagSet
308
+ config .AddFlags (& fs )
309
+ for _ , name := range []string {"v" , "vmodule" } {
310
+ from := flag .CommandLine .Lookup (name )
311
+ to := fs .Lookup (name )
312
+ if err := to .Value .Set (from .Value .String ()); err != nil {
313
+ panic (err )
314
+ }
315
+ }
316
+
317
+ // Ensure consistent logging: this klog.Logger writes to tb, adding the
318
+ // date/time header, and our own wrapper emulates that behavior for
319
+ // Log/Logf/...
320
+ logger := ktesting .NewLogger (tb , config )
321
+ return logger
322
+ }
323
+
320
324
type InitOption = initoption.InitOption
321
325
322
326
// InitCtx is a variant of [Init] which uses an already existing context and
@@ -345,12 +349,13 @@ func InitCtx(ctx context.Context, tb TB, _ ...InitOption) TContext {
345
349
// ...
346
350
// })
347
351
//
348
- // WithTB sets up cancellation for the sub-test.
352
+ // WithTB sets up cancellation for the sub-test and uses per-test output .
349
353
//
350
354
// A simpler API is to use TContext.Run as replacement
351
355
// for [testing.T.Run].
352
356
func WithTB (parentCtx TContext , tb TB ) TContext {
353
- tCtx := InitCtx (parentCtx , tb )
357
+ tCtx := InitCtx (klog .NewContext (parentCtx , newLogger (tb , false /* don't buffer log output */ )), tb )
358
+
354
359
tCtx = WithCancel (tCtx )
355
360
tCtx = WithClients (tCtx ,
356
361
parentCtx .RESTConfig (),
0 commit comments