From 171620765e7a319888eb1b324ada7f64bfc69b17 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Sun, 28 Apr 2024 14:38:29 +0200 Subject: [PATCH] ktesting: add Step Step simplifies using WithStep because it creates a local scope where the same tCtx variable is the one with the step name. --- test/utils/ktesting/stepcontext.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/utils/ktesting/stepcontext.go b/test/utils/ktesting/stepcontext.go index 7271f3da789..b9ff6b1fc3d 100644 --- a/test/utils/ktesting/stepcontext.go +++ b/test/utils/ktesting/stepcontext.go @@ -41,6 +41,24 @@ func WithStep(tCtx TContext, what string) TContext { return WithLogger(sCtx, klog.LoggerWithName(sCtx.Logger(), what)) } +// Step is useful when the context with the step information is +// used more than once: +// +// ktesting.Step(tCtx, "step 1", func(tCtx ktesting.TContext) { +// tCtx.Log(...) +// if (... ) { +// tCtx.Failf(...) +// } +// )} +// +// Inside the callback, the tCtx variable is the one where the step +// has been added. This avoids the need to introduce multiple different +// context variables and risk of using the wrong one. +func Step(tCtx TContext, what string, cb func(tCtx TContext)) { + tCtx.Helper() + cb(WithStep(tCtx, what)) +} + type stepContext struct { TContext what string