Skip to content

Commit 1b39a8b

Browse files
committed
internal/testenv: always Cleanup to appease go vet
vet from Go 1.22 complains about cancelCtx being potentially leaked: exec.go:140:4: the cancelCtx function is not used on all paths (possible context leak) exec.go:192:2: this return statement may be reached without using the cancelCtx var defined on line 140 The code was fine with that, and in practice the leak does not happen, but having the vet warning around is still distracting. Thankfully, Go 1.14 is very old at this point, and per go.mod, this module now requires Go 1.18 or later, so we can clean this up. Change-Id: I4c402ad233e9b33a6d51c98ba0833c67fe01b209 Reviewed-on: https://go-review.googlesource.com/c/tools/+/563675 Reviewed-by: Than McIntosh <[email protected]> Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent afd8428 commit 1b39a8b

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

internal/testenv/exec.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ func NeedsExec(t testing.TB) {
9292
// for an arbitrary grace period before the test's deadline expires,
9393
// - if Cmd has the Cancel field, fails the test if the command is canceled
9494
// due to the test's deadline, and
95-
// - if supported, sets a Cleanup function that verifies that the test did not
96-
// leak a subprocess.
95+
// - sets a Cleanup function that verifies that the test did not leak a subprocess.
9796
func CommandContext(t testing.TB, ctx context.Context, name string, args ...string) *exec.Cmd {
9897
t.Helper()
9998
NeedsExec(t)
@@ -173,21 +172,14 @@ func CommandContext(t testing.TB, ctx context.Context, name string, args ...stri
173172
rWaitDelay.Set(reflect.ValueOf(gracePeriod))
174173
}
175174

176-
// t.Cleanup was added in Go 1.14; for earlier Go versions,
177-
// we just let the Context leak.
178-
type Cleanupper interface {
179-
Cleanup(func())
180-
}
181-
if ct, ok := t.(Cleanupper); ok {
182-
ct.Cleanup(func() {
183-
if cancelCtx != nil {
184-
cancelCtx()
185-
}
186-
if cmd.Process != nil && cmd.ProcessState == nil {
187-
t.Errorf("command was started, but test did not wait for it to complete: %v", cmd)
188-
}
189-
})
190-
}
175+
t.Cleanup(func() {
176+
if cancelCtx != nil {
177+
cancelCtx()
178+
}
179+
if cmd.Process != nil && cmd.ProcessState == nil {
180+
t.Errorf("command was started, but test did not wait for it to complete: %v", cmd)
181+
}
182+
})
191183

192184
return cmd
193185
}

0 commit comments

Comments
 (0)