From 04b4a832dc48d1e4a9fa5793a6725cb3320c691d Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 20 Jan 2026 10:56:25 +0100 Subject: [PATCH] chore(lint): add forbidigo rules to enforce t.Context() in tests Add linter rules to prevent usage of context.Background() and context.TODO() in test files - t.Context() should be used instead. The rules only apply to *_test.go files, not production code. Note: os.Setenv is not covered by forbidigo due to a limitation where it only catches calls when the return value is assigned. However, errcheck will flag unchecked os.Setenv calls. Assisted-By: cagent Signed-off-by: David Gageot --- .golangci.yml | 14 ++++++++++++++ pkg/compose/kill_test.go | 1 + 2 files changed, 15 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 48898c457..7d75550fe 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,6 +8,7 @@ linters: - depguard - errcheck - errorlint + - forbidigo - gocritic - gocyclo - gomodguard @@ -38,6 +39,15 @@ linters: desc: use stdlib slices package - pkg: gopkg.in/yaml.v2 desc: compose-go uses yaml.v3 + forbidigo: + analyze-types: true + forbid: + - pattern: 'context\.Background' + pkg: '^context$' + msg: "in tests, use t.Context() instead of context.Background()" + - pattern: 'context\.TODO' + pkg: '^context$' + msg: "in tests, use t.Context() instead of context.TODO()" gocritic: disabled-checks: - paramTypeCombine @@ -74,6 +84,10 @@ linters: - third_party$ - builtin$ - examples$ + rules: + - path-except: '_test\.go' + linters: + - forbidigo issues: max-issues-per-linter: 0 max-same-issues: 0 diff --git a/pkg/compose/kill_test.go b/pkg/compose/kill_test.go index a70228a65..f27606b7f 100644 --- a/pkg/compose/kill_test.go +++ b/pkg/compose/kill_test.go @@ -127,6 +127,7 @@ func containerLabels(service string, oneOff bool) map[string]string { } func anyCancellableContext() gomock.Matcher { + //nolint:forbidigo // This creates a context type for gomock matching, not for actual test usage ctxWithCancel, cancel := context.WithCancel(context.Background()) cancel() return gomock.AssignableToTypeOf(ctxWithCancel)