Skip to content

Commit 3e35df5

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
internal/testenv: reduce init-time work for MustHaveExec
In CL 486275 I added a somewhat complex init function that sets up a callback to probe for exec support. A lot of the complexity was simply to avoid an unnecessary call to os.Environ during init. In CL 491660, I made the os.Environ call unconditional on all platforms anyway in order to make HasGoBuild more robust. Since the init-function indirection no longer serves a useful purpose, I would like to simplify it to a package-level function, avoiding the complexity of changing package variables at init time. Change-Id: Ie0041d52cbde06ff14540192c8fba869a851158e Reviewed-on: https://go-review.googlesource.com/c/go/+/492977 Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 05ca41d commit 3e35df5

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/internal/testenv/exec.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package testenv
66

77
import (
88
"context"
9+
"errors"
10+
"fmt"
911
"os"
1012
"os/exec"
1113
"runtime"
@@ -30,25 +32,24 @@ import (
3032
// for the resulting error.
3133
func MustHaveExec(t testing.TB) {
3234
tryExecOnce.Do(func() {
33-
tryExecOk = tryExec()
35+
tryExecErr = tryExec()
3436
})
35-
if !tryExecOk {
36-
t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH)
37+
if tryExecErr != nil {
38+
t.Skipf("skipping test: cannot exec subprocess on %s/%s: %v", runtime.GOOS, runtime.GOARCH, tryExecErr)
3739
}
3840
}
3941

4042
var (
41-
tryExec = func() bool { return true }
4243
tryExecOnce sync.Once
43-
tryExecOk bool
44+
tryExecErr error
4445
)
4546

46-
func init() {
47+
func tryExec() error {
4748
switch runtime.GOOS {
4849
case "wasip1", "js", "ios":
4950
default:
5051
// Assume that exec always works on non-mobile platforms and Android.
51-
return
52+
return nil
5253
}
5354

5455
// ios has an exec syscall but on real iOS devices it might return a
@@ -64,24 +65,18 @@ func init() {
6465
// This isn't a standard 'go test' binary, so we don't know how to
6566
// self-exec in a way that should succeed without side effects.
6667
// Just forget it.
67-
tryExec = func() bool { return false }
68-
return
68+
return errors.New("can't probe for exec support with a non-test executable")
6969
}
7070

7171
// We know that this is a test executable. We should be able to run it with a
7272
// no-op flag to check for overall exec support.
73-
tryExec = func() bool {
74-
exe, err := os.Executable()
75-
if err != nil {
76-
return false
77-
}
78-
cmd := exec.Command(exe, "-test.list=^$")
79-
cmd.Env = origEnv
80-
if err := cmd.Run(); err == nil {
81-
return true
82-
}
83-
return false
73+
exe, err := os.Executable()
74+
if err != nil {
75+
return fmt.Errorf("can't probe for exec support: %w", err)
8476
}
77+
cmd := exec.Command(exe, "-test.list=^$")
78+
cmd.Env = origEnv
79+
return cmd.Run()
8580
}
8681

8782
var execPaths sync.Map // path -> error

0 commit comments

Comments
 (0)