Debian Codegraph @

internal/testenv/exec.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/
6
7 import (
8 "contextscip-go gomod github.com/golang/go/src go1.25.0 context/"
9 "flagscip-go gomod github.com/golang/go/src go1.25.0 flag/"
10 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
11 "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/"
12 "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/"
13 "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/"
14 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
15 "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/"
16 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
17 "timescip-go gomod github.com/golang/go/src go1.25.0 time/"
18 )
19
20 // HasExec reports whether the current system can start new processes
21 // using os.StartProcess or (more commonly) exec.Command.
22 func HasExecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/HasExec().() bool {
23 switch runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. {
24 case "aix",
25 "android",
26 "darwin",
27 "dragonfly",
28 "freebsd",
29 "illumos",
30 "linux",
31 "netbsd",
32 "openbsd",
33 "plan9",
34 "solaris",
35 "windows":
36 // Known OS that isn't ios or wasm; assume that exec works.
37 return true
38
39 case "ios", "js", "wasip1":
40 // ios has an exec syscall but on real iOS devices it might return a
41 // permission error. In an emulated environment (such as a Corellium host)
42 // it might succeed, so try it and find out.
43 //
44 // As of 2023-04-19 wasip1 and js don't have exec syscalls at all, but we
45 // may as well use the same path so that this branch can be tested without
46 // an ios environment.
47 fallthrough
48
49 default:
50 tryExecOncescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/tryExecOnce..Doscip-go gomod github.com/golang/go/src go1.25.0 sync/Once#Do().(func() {
51 exe, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Executablescip-go gomod github.com/golang/go/src go1.25.0 os/Executable().()
52 if err != nil {
53 return
54 }
55 if flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Lookupscip-go gomod github.com/golang/go/src go1.25.0 flag/Lookup().("test.list") == nil {
56 // We found the executable, but we don't know how to run it in a way
57 // that should succeed without side-effects. Just forget it.
58 return
59 }
60 // We know that a test executable exists and can run, because we're
61 // running it now. Use it to check for overall exec support, but be sure
62 // to remove any environment variables that might trigger non-default
63 // behavior in a custom TestMain.
64 cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(exe, "-test.list=^$")
65 cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env. = []string{}
66 if err := cmd.Runscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Run().(); err == nil {
67 tryExecOkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/tryExecOk. = true
68 }
69 })
70 return tryExecOkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/tryExecOk.
71 }
72 }
73
74 var (
75 tryExecOncescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/tryExecOnce. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Oncescip-go gomod github.com/golang/go/src go1.25.0 sync/Once#
76 tryExecOkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/tryExecOk. bool
77 )
78
79 // NeedsExec checks that the current system can start new processes
80 // using os.StartProcess or (more commonly) exec.Command.
81 // If not, NeedsExec calls t.Skip with an explanation.
82 func NeedsExecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsExec().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#) {
83 if !HasExecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/HasExec().() {
84 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Skipf().("skipping test: cannot exec subprocess on %s/%s", runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS., runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOARCHscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOARCH.)
85 }
86 }
87
88 // CommandContext is like exec.CommandContext, but:
89 // - skips t if the platform does not support os/exec,
90 // - if supported, sends SIGQUIT instead of SIGKILL in its Cancel function
91 // - if the test has a deadline, adds a Context timeout and (if supported) WaitDelay
92 // for an arbitrary grace period before the test's deadline expires,
93 // - if Cmd has the Cancel field, fails the test if the command is canceled
94 // due to the test's deadline, and
95 // - sets a Cleanup function that verifies that the test did not leak a subprocess.
96 func CommandContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/CommandContext().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#, ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, name string, args ...string) *execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Cmdscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd# {
97 t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Helper().()
98 NeedsExecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsExec().(t)
99
100 var (
101 cancelCtx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.CancelFuncscip-go gomod github.com/golang/go/src go1.25.0 context/CancelFunc#
102 gracePeriod timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration# // unlimited unless the test has a deadline (to allow for interactive debugging)
103 )
104
105 if td, ok := Deadlinescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Deadline().(t); ok {
106 // Start with a minimum grace period, just long enough to consume the
107 // output of a reasonable program after it terminates.
108 gracePeriod = 100 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond.
109 if s := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("GO_TEST_TIMEOUT_SCALE"); s != "" {
110 scale, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(s)
111 if err != nil {
112 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Fatalf().("invalid GO_TEST_TIMEOUT_SCALE: %v", err)
113 }
114 gracePeriod *= timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#(scale)
115 }
116
117 // If time allows, increase the termination grace period to 5% of the
118 // test's remaining time.
119 testTimeout := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Untilscip-go gomod github.com/golang/go/src go1.25.0 time/Until().(td)
120 if gp := testTimeout / 20; gp > gracePeriod {
121 gracePeriod = gp
122 }
123
124 // When we run commands that execute subprocesses, we want to reserve two
125 // grace periods to clean up: one for the delay between the first
126 // termination signal being sent (via the Cancel callback when the Context
127 // expires) and the process being forcibly terminated (via the WaitDelay
128 // field), and a second one for the delay between the process being
129 // terminated and the test logging its output for debugging.
130 //
131 // (We want to ensure that the test process itself has enough time to
132 // log the output before it is also terminated.)
133 cmdTimeout := testTimeout - 2*gracePeriod
134
135 if cd, ok := ctx.Deadlinescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Deadline().(); !ok || timescip-go gomod github.com/golang/go/src go1.25.0 time/.Untilscip-go gomod github.com/golang/go/src go1.25.0 time/Until().(cd) > cmdTimeout {
136 // Either ctx doesn't have a deadline, or its deadline would expire
137 // after (or too close before) the test has already timed out.
138 // Add a shorter timeout so that the test will produce useful output.
139 ctx, cancelCtx = contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.25.0 context/WithTimeout().(ctx, cmdTimeout)
140 }
141 }
142
143 cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.CommandContextscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/CommandContext().(ctx, name, args...)
144
145 // Use reflection to set the Cancel and WaitDelay fields, if present.
146 // TODO(bcmills): When we no longer support Go versions below 1.20,
147 // remove the use of reflect and assume that the fields are always present.
148 rc := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(cmd).Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().()
149
150 if rCancel := rc.FieldByNamescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#FieldByName().("Cancel"); rCancel.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() {
151 rCancel.Setscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Set().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(func() error {
152 if cancelCtx != nil && ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() == contextscip-go gomod github.com/golang/go/src go1.25.0 context/.DeadlineExceededscip-go gomod github.com/golang/go/src go1.25.0 context/DeadlineExceeded. {
153 // The command timed out due to running too close to the test's deadline
154 // (because we specifically set a shorter Context deadline for that
155 // above). There is no way the test did that intentionally — it's too
156 // close to the wire! — so mark it as a test failure. That way, if the
157 // test expects the command to fail for some other reason, it doesn't
158 // have to distinguish between that reason and a timeout.
159 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Errorf().("test timed out while running command: %v", cmd)
160 } else {
161 // The command is being terminated due to ctx being canceled, but
162 // apparently not due to an explicit test deadline that we added.
163 // Log that information in case it is useful for diagnosing a failure,
164 // but don't actually fail the test because of it.
165 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Logf().("%v: terminating command: %v", ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().(), cmd)
166 }
167 return cmd.Processscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Process..Signalscip-go gomod github.com/golang/go/src go1.25.0 os/Process#Signal().(Sigquitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Sigquit.)
168 }))
169 }
170
171 if rWaitDelay := rc.FieldByNamescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#FieldByName().("WaitDelay"); rWaitDelay.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() {
172 rWaitDelay.Setscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Set().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(gracePeriod))
173 }
174
175 t.Cleanupscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Cleanup().(func() {
176 if cancelCtx != nil {
177 cancelCtx()
178 }
179 if cmd.Processscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Process. != nil && cmd.ProcessStatescip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#ProcessState. == nil {
180 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Errorf().("command was started, but test did not wait for it to complete: %v", cmd)
181 }
182 })
183
184 return cmd
185 }
186
187 // Command is like exec.Command, but applies the same changes as
188 // testenv.CommandContext (with a default Context).
189 func Commandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Command().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#, name string, args ...string) *execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Cmdscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd# {
190 t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Helper().()
191 return CommandContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/CommandContext().(t, contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().(), name, args...)
192 }