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 <bircni@icloud.com>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1306
Reviewed-by: bircni <bircni@icloud.com>
This commit is contained in:
Lunny Xiao
2026-10-12 21:11:17 +00:00
parent 160e18e705
commit c3c647ef83
38 changed files with 288 additions and 301 deletions

View File

@@ -4,7 +4,6 @@
package process
import (
"context"
"os"
"os/exec"
"path/filepath"
@@ -70,7 +69,7 @@ func TestKillProcessesWithCWDUnderTerminatesMatch(t *testing.T) {
cmd := startSleeperIn(t, dir)
awaitCWD(t, cmd.Process.Pid)
killed, err := KillProcessesWithCWDUnder(context.Background(), []string{dir})
killed, err := KillProcessesWithCWDUnder(t.Context(), []string{dir})
require.NoError(t, err)
require.GreaterOrEqual(t, killed, 1)
@@ -85,7 +84,7 @@ func TestKillProcessesWithCWDUnderSparesUnrelated(t *testing.T) {
cmd := startSleeperIn(t, elsewhere)
awaitCWD(t, cmd.Process.Pid)
_, err := KillProcessesWithCWDUnder(context.Background(), []string{workspace})
_, err := KillProcessesWithCWDUnder(t.Context(), []string{workspace})
require.NoError(t, err)
require.True(t, processAlive(cmd.Process.Pid), "process outside owned dirs must be spared")
}

View File

@@ -4,7 +4,6 @@
package process
import (
"context"
"os/exec"
"testing"
@@ -12,7 +11,7 @@ import (
)
func TestNewTreeKillConfiguresCommand(t *testing.T) {
cmd := exec.CommandContext(context.Background(), "sleep", "1")
cmd := exec.CommandContext(t.Context(), "sleep", "1")
tk := NewTreeKill(cmd)
require.NotNil(t, tk)
@@ -22,7 +21,7 @@ func TestNewTreeKillConfiguresCommand(t *testing.T) {
}
func TestTreeKillCaptureStoresKiller(t *testing.T) {
cmd := exec.CommandContext(context.Background(), "sleep", "10")
cmd := exec.CommandContext(t.Context(), "sleep", "10")
cmd.SysProcAttr = SysProcAttr("", false)
tk := NewTreeKill(cmd)
require.NoError(t, cmd.Start())