Skip to content

Commit 2807721

Browse files
matejvasekclaude
andauthored
test: use t.Context() instead of context.Background() (#3525)
Replace context.Background() and context.TODO() with the testing context t.Context() (available since Go 1.24) across all test files. This ties context lifecycle to the test lifecycle, providing automatic cancellation when tests end. Cleanup functions (t.Cleanup) retain context.Background() since t.Context() is cancelled before cleanup runs. Unused "context" imports are removed where applicable. Signed-off-by: Matej Vašek <matejvasek@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 28963a1 commit 2807721

47 files changed

Lines changed: 215 additions & 235 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/client_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"testing"
65

76
fn "knative.dev/func/pkg/functions"
@@ -28,11 +27,11 @@ func Test_NewTestClient(t *testing.T) {
2827

2928
// Trigger an invocation of the mocks by running the associated client
3029
// methods which depend on them
31-
err := client.Remove(context.Background(), "myfunc", "myns", fn.Function{}, true)
30+
err := client.Remove(t.Context(), "myfunc", "myns", fn.Function{}, true)
3231
if err != nil {
3332
t.Fatal(err)
3433
}
35-
_, err = client.Describe(context.Background(), "myfunc", "myns", fn.Function{})
34+
_, err = client.Describe(t.Context(), "myfunc", "myns", fn.Function{})
3635
if err != nil {
3736
t.Fatal(err)
3837
}

cmd/config_labels_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package cmd
55

66
import (
7-
"context"
87
"os"
98
"reflect"
109
"sync"
@@ -42,7 +41,7 @@ func assertLabelEq(t *testing.T, actual []fn.Label, want []fn.Label) {
4241
func createRunFunc(cmd *cobra.Command, t *testing.T) func(subcmd string, input ...string) {
4342
return func(subcmd string, input ...string) {
4443

45-
ctx := context.Background()
44+
ctx := t.Context()
4645

4746
ptm, pts, err := pty.Open()
4847
if err != nil {
@@ -154,7 +153,7 @@ func TestListLabels(t *testing.T) {
154153
cmd := NewConfigLabelsCmd(&loaderSaver)
155154
cmd.SetArgs([]string{})
156155

157-
ctx := context.Background()
156+
ctx := t.Context()
158157
c, err := expect.NewConsole()
159158
if err != nil {
160159
t.Fatal(err)

cmd/delete_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"bytes"
5-
"context"
65
"os"
76
"strings"
87
"testing"
@@ -22,7 +21,7 @@ func TestDelete_Default(t *testing.T) {
2221
name = "myfunc"
2322
namespace = "testns"
2423
remover = mock.NewRemover()
25-
ctx = context.Background()
24+
ctx = t.Context()
2625
)
2726

2827
// Remover which confirms the name and namespace received are those
@@ -172,7 +171,7 @@ func TestDelete_NamespaceFlagPriority(t *testing.T) {
172171
Namespace: namespace,
173172
}
174173
client := fn.New()
175-
_, _, err = client.New(context.Background(), f)
174+
_, _, err = client.New(t.Context(), f)
176175
if err != nil {
177176
t.Fatal(err)
178177
}

cmd/deploy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ func TestReDeploy_OnRegistryChangeWithBuildFalse(t *testing.T) {
19111911
Registry: TestRegistry,
19121912
Namespace: "default",
19131913
}
1914-
_, f, err := fn.New(fn.WithDeployer(mock.NewDeployer())).New(context.Background(), f)
1914+
_, f, err := fn.New(fn.WithDeployer(mock.NewDeployer())).New(t.Context(), f)
19151915
if err != nil {
19161916
t.Fatal(err)
19171917
}

cmd/invoke_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestInvoke(t *testing.T) {
6060
t.Fatal(err)
6161
}
6262
client := fn.New(fn.WithRunner(runner))
63-
job, err := client.Run(context.Background(), f)
63+
job, err := client.Run(t.Context(), f)
6464
if err != nil {
6565
t.Fatal(err)
6666
}

cmd/run_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestRun_Run(t *testing.T) {
145145
}
146146
}
147147

148-
ctx, cancel := context.WithCancel(context.Background())
148+
ctx, cancel := context.WithCancel(t.Context())
149149
runErrCh := make(chan error, 1)
150150
go func() {
151151
t0 := tt // capture tt into closure
@@ -252,7 +252,7 @@ func TestRun_Images(t *testing.T) {
252252
if err != nil {
253253
t.Fatal(err)
254254
}
255-
ctx, cancel := context.WithCancel(context.Background())
255+
ctx, cancel := context.WithCancel(t.Context())
256256
runErrCh := make(chan error, 1)
257257
go func() {
258258
t0 := tt // capture tt into closure
@@ -362,7 +362,7 @@ func TestRun_CorrectImage(t *testing.T) {
362362
if err != nil {
363363
t.Fatal(err)
364364
}
365-
ctx, cancel := context.WithCancel(context.Background())
365+
ctx, cancel := context.WithCancel(t.Context())
366366
runErrCh := make(chan error, 1)
367367
go func() {
368368
t0 := tt // capture tt into closure
@@ -450,7 +450,7 @@ func TestRun_DirectOverride(t *testing.T) {
450450
cmd.SetArgs([]string{fmt.Sprintf("--image=%s", overrideImage)})
451451

452452
// run function with above argument
453-
ctx, cancel := context.WithCancel(context.Background())
453+
ctx, cancel := context.WithCancel(t.Context())
454454
runErrCh := make(chan error, 1)
455455
go func() {
456456
_, err := cmd.ExecuteContextC(ctx)
@@ -501,7 +501,7 @@ func TestRun_Address(t *testing.T) {
501501
))
502502
cmd.SetArgs([]string{"--address", testAddr})
503503

504-
ctx, cancel := context.WithCancel(context.Background())
504+
ctx, cancel := context.WithCancel(t.Context())
505505
runErrCh := make(chan error, 1)
506506
go func() {
507507
_, err := cmd.ExecuteContextC(ctx)
@@ -574,7 +574,7 @@ func TestRun_BaseImage(t *testing.T) {
574574
if err != nil {
575575
t.Fatal(err)
576576
}
577-
ctx, cancel := context.WithCancel(context.Background())
577+
ctx, cancel := context.WithCancel(t.Context())
578578
runErrCh := make(chan error, 1)
579579
go func() {
580580
t0 := tt // capture tt into closure

e2e/e2e_core_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package e2e
55

66
import (
77
"bytes"
8-
"context"
98
"encoding/json"
109
"fmt"
1110
"os"
@@ -401,7 +400,7 @@ func TestCore_Delete(t *testing.T) {
401400

402401
// Check it appears in the list
403402
client := fn.New(fn.WithListers(knative.NewLister(false)))
404-
list, err := client.List(context.Background(), Namespace)
403+
list, err := client.List(t.Context(), Namespace)
405404
if err != nil {
406405
t.Fatal(err)
407406
}
@@ -416,7 +415,7 @@ func TestCore_Delete(t *testing.T) {
416415
t.Logf("Error deleting function. %v", err)
417416
}
418417

419-
list, err = client.List(context.Background(), Namespace)
418+
list, err = client.List(t.Context(), Namespace)
420419
if err != nil {
421420
t.Fatal(err)
422421
}

e2e/e2e_metadata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ func waitForEvent(t *testing.T, functionName, eventId string) <-chan string {
730730

731731
eventReceived := make(chan string, 10)
732732

733-
ctx, cancel := context.WithCancel(context.Background())
733+
ctx, cancel := context.WithCancel(t.Context())
734734
t.Cleanup(cancel)
735735

736736
pr, pw := io.Pipe()

e2e/e2e_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package e2e
1111
import (
1212
"bufio"
1313
"bytes"
14-
"context"
1514
"encoding/json"
1615
"fmt"
1716
"io"
@@ -859,7 +858,7 @@ func isAbnormalExit(t *testing.T, err error) bool {
859858
// setSecret creates or replaces a secret.
860859
func setSecret(t *testing.T, name, ns string, data map[string][]byte) {
861860
t.Helper()
862-
ctx := context.Background()
861+
ctx := t.Context()
863862
config, err := k8s.GetClientConfig().ClientConfig()
864863
if err != nil {
865864
t.Fatal(err)
@@ -882,7 +881,7 @@ func setSecret(t *testing.T, name, ns string, data map[string][]byte) {
882881
// setConfigMap creates or replaces a configMap
883882
func setConfigMap(t *testing.T, name, ns string, data map[string]string) {
884883
t.Helper()
885-
ctx := context.Background()
884+
ctx := t.Context()
886885
config, err := k8s.GetClientConfig().ClientConfig()
887886
if err != nil {
888887
t.Fatal(err)

pkg/builders/builders_int_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestInt_PrivateGitRepository(t *testing.T) {
6969
t.Skip("Skipping TestPrivateGitRepository on non-Linux systems due to cluster networking limitations")
7070
}
7171

72-
ctx, cancel := context.WithCancel(context.Background())
72+
ctx, cancel := context.WithCancel(t.Context())
7373
defer cancel()
7474

7575
sigs := make(chan os.Signal, 1)

0 commit comments

Comments
 (0)