Skip to content

Commit 6478ca5

Browse files
committed
ktesting: fix per-test logging in TContext.Run and WithTB
WithTB was originally defined as "uses the existing logger". But what we want there and in the newer TContext.Run is the usual per-test logging, now for the sub-test.
1 parent 939c9c0 commit 6478ca5

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

test/utils/ktesting/tcontext.go

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -259,45 +259,8 @@ func Init(tb TB, opts ...InitOption) TContext {
259259

260260
ctx := interruptCtx
261261
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)
299263
ctx = klog.NewContext(interruptCtx, logger)
300-
301264
tb = withKlogHeader(tb)
302265
}
303266

@@ -317,6 +280,47 @@ func Init(tb TB, opts ...InitOption) TContext {
317280
return WithCancel(InitCtx(ctx, tb))
318281
}
319282

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+
320324
type InitOption = initoption.InitOption
321325

322326
// 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 {
345349
// ...
346350
// })
347351
//
348-
// WithTB sets up cancellation for the sub-test.
352+
// WithTB sets up cancellation for the sub-test and uses per-test output.
349353
//
350354
// A simpler API is to use TContext.Run as replacement
351355
// for [testing.T.Run].
352356
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+
354359
tCtx = WithCancel(tCtx)
355360
tCtx = WithClients(tCtx,
356361
parentCtx.RESTConfig(),

0 commit comments

Comments
 (0)