test: use t.Context() instead of context.Background() (#1306)

The test context is cancelled when the test ends, so anything still holding it stops instead of leaking into the next test. Uses inside `t.Cleanup`, `defer` and `go func` blocks are left alone, as the context is already cancelled there.

Stacked on https://gitea.com/gitea/runner/pulls/1305.

Assisted-by: Codet
Co-authored-by: bircni <[email protected]>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1306
Reviewed-by: bircni <[email protected]>
This commit is contained in:
Lunny Xiaoandbircni committed 2026-10-12 21:11:17 +00:00
1 parent 160e18e705
commit c3c647ef83
38 files changed
+288 -301

No files matched your search

+4 -4
View File
@@ -105,7 +105,7 @@ func TestSetRef(t *testing.T) {
Event: table.event,
}
SetRef(context.Background(), ghc, "/some/dir")
SetRef(t.Context(), ghc, "/some/dir")
ghc.SetRefTypeAndName()
assert.Equal(t, table.ref, ghc.Ref)
@@ -123,7 +123,7 @@ func TestSetRef(t *testing.T) {
Event: map[string]any{},
}
SetRef(context.Background(), ghc, "/some/dir")
SetRef(t.Context(), ghc, "/some/dir")
assert.Equal(t, "refs/heads/master", ghc.Ref)
})
@@ -210,14 +210,14 @@ func TestSetSha(t *testing.T) {
Event: table.event,
}
SetSha(context.Background(), ghc, "/some/dir")
SetSha(t.Context(), ghc, "/some/dir")
assert.Equal(t, table.sha, ghc.Sha)
})
}
t.Run("pinned checkout keeps the first sha and ref", func(t *testing.T) {
ctx := WithPinnedCheckout(context.Background())
ctx := WithPinnedCheckout(t.Context())
for _, head := range []string{"first", "moved"} {
findGitRevision = func(context.Context, string) (string, string, error) { return "", head, nil }
findGitRef = func(context.Context, string) (string, error) { return "refs/heads/" + head, nil }