|
| 1 | +// Package reexectest provides small helpers for subprocess tests that re-exec |
| 2 | +// the current test binary. The child process is selected by setting argv0 to a |
| 3 | +// deterministic token derived from (t.Name(), name), while -test.run is used to |
| 4 | +// run only the current test/subtest. |
| 5 | +// |
| 6 | +// Typical usage: |
| 7 | +// |
| 8 | +// func TestSomething(t *testing.T) { |
| 9 | +// if reexectest.Run(t, "child", func(t *testing.T) { |
| 10 | +// // child branch |
| 11 | +// }) { |
| 12 | +// return |
| 13 | +// } |
| 14 | +// |
| 15 | +// // parent branch |
| 16 | +// cmd := reexectest.CommandContext(t, t.Context(), "child", "arg1") |
| 17 | +// out, err := cmd.CombinedOutput() |
| 18 | +// if err != nil { |
| 19 | +// t.Fatalf("child failed: %v\n%s", err, out) |
| 20 | +// } |
| 21 | +// } |
| 22 | +package reexectest |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "crypto/sha256" |
| 27 | + "encoding/hex" |
| 28 | + "os" |
| 29 | + "os/exec" |
| 30 | + "regexp" |
| 31 | + "strings" |
| 32 | + "testing" |
| 33 | +) |
| 34 | + |
| 35 | +const argv0Prefix = "reexectest-" |
| 36 | + |
| 37 | +// argv0Token returns a short (16 hex chars) deterministic argv0 token |
| 38 | +// based on the test's name to prevent collisions. |
| 39 | +func argv0Token(t *testing.T, name string) string { |
| 40 | + sum := sha256.Sum256([]byte(t.Name() + "\x00" + name)) |
| 41 | + return argv0Prefix + hex.EncodeToString(sum[:8]) |
| 42 | +} |
| 43 | + |
| 44 | +// Run runs f in the current process iff it is the matching child process for |
| 45 | +// (t, name). It returns true if f ran (i.e., we are the child). |
| 46 | +// |
| 47 | +// When Run returns true, callers should return from the test to avoid running |
| 48 | +// the parent branch in the child process. |
| 49 | +func Run(t *testing.T, name string, f func(t *testing.T)) bool { |
| 50 | + t.Helper() |
| 51 | + |
| 52 | + if os.Args[0] != argv0Token(t, name) { |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + // Scrub the "-test.run=<pattern>" that was injected by CommandContext |
| 57 | + origArgs := os.Args |
| 58 | + if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "-test.run=") { |
| 59 | + os.Args = append(os.Args[:1], os.Args[2:]...) |
| 60 | + defer func() { os.Args = origArgs }() |
| 61 | + } |
| 62 | + |
| 63 | + f(t) |
| 64 | + return true |
| 65 | +} |
| 66 | + |
| 67 | +// Command returns an [*exec.Cmd] configured to re-exec the current test binary |
| 68 | +// as a subprocess for the given test and name. |
| 69 | +// |
| 70 | +// The child process is restricted to run only the current test or subtest |
| 71 | +// (via -test.run). Its argv[0] is set to a deterministic token derived from |
| 72 | +// (t.Name(), name), which is used by [Run] to select the child execution path. |
| 73 | +// |
| 74 | +// On Linux, the returned command sets [syscall.SysProcAttr.Pdeathsig] to |
| 75 | +// SIGTERM, so the child receives SIGTERM if the creating thread dies. |
| 76 | +// Callers may modify SysProcAttr before starting the command. |
| 77 | +// |
| 78 | +// It is analogous to [exec.Command], but targets the current test binary. |
| 79 | +func Command(t *testing.T, name string, args ...string) *exec.Cmd { |
| 80 | + return commandContext(t, t.Context(), name, args...) |
| 81 | +} |
| 82 | + |
| 83 | +// CommandContext is like [Command] but includes a context. It uses |
| 84 | +// [exec.CommandContext] under the hood. |
| 85 | +// |
| 86 | +// The provided context controls cancellation of the subprocess in the same |
| 87 | +// way as [exec.CommandContext]. |
| 88 | +// |
| 89 | +// On Linux, the returned command sets [syscall.SysProcAttr.Pdeathsig] to |
| 90 | +// SIGTERM. Callers may modify SysProcAttr before starting the command. |
| 91 | +func CommandContext(t *testing.T, ctx context.Context, name string, args ...string) *exec.Cmd { |
| 92 | + return commandContext(t, ctx, name, args...) |
| 93 | +} |
| 94 | + |
| 95 | +func commandContext(t *testing.T, ctx context.Context, name string, args ...string) *exec.Cmd { |
| 96 | + t.Helper() |
| 97 | + exe, err := os.Executable() |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("os.Executable(): %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + argv0 := argv0Token(t, name) |
| 103 | + pattern := "^" + regexp.QuoteMeta(t.Name()) + "$" |
| 104 | + |
| 105 | + cmd := exec.CommandContext(ctx, exe) |
| 106 | + cmd.Path = exe |
| 107 | + cmd.Args = append([]string{argv0, "-test.run=" + pattern}, args...) |
| 108 | + setPdeathsig(cmd) |
| 109 | + return cmd |
| 110 | +} |
0 commit comments