mirror of
https://github.com/golang/go
synced 2024-11-22 04:24:39 -07:00
math,os,os/*: use testenv.Executable
As some callers don't have a testing context, modify testenv.Executable to accept nil (similar to how testenv.GOROOT works). Change-Id: I39112a7869933785a26b5cb6520055b3cc42b847 Reviewed-on: https://go-review.googlesource.com/c/go/+/609835 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
parent
76a42d7435
commit
c3f346a485
@ -32,8 +32,12 @@ import (
|
||||
// for the resulting error.
|
||||
func MustHaveExec(t testing.TB) {
|
||||
if err := tryExec(); err != nil {
|
||||
msg := fmt.Sprintf("cannot exec subprocess on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
|
||||
if t == nil {
|
||||
panic(msg)
|
||||
}
|
||||
t.Helper()
|
||||
t.Skipf("skipping test: cannot exec subprocess on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
|
||||
t.Skip("skipping test:", msg)
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,7 +85,11 @@ func Executable(t testing.TB) string {
|
||||
|
||||
exe, err := exePath()
|
||||
if err != nil {
|
||||
t.Fatalf("os.Executable error: %v", err)
|
||||
msg := fmt.Sprintf("os.Executable error: %v", err)
|
||||
if t == nil {
|
||||
panic(msg)
|
||||
}
|
||||
t.Fatal(msg)
|
||||
}
|
||||
return exe
|
||||
}
|
||||
|
@ -37,11 +37,7 @@ func TestDefaultRace(t *testing.T) {
|
||||
i := i
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
exe = os.Args[0]
|
||||
}
|
||||
cmd := testenv.Command(t, exe, "-test.run=TestDefaultRace")
|
||||
cmd := testenv.Command(t, testenv.Executable(t), "-test.run=TestDefaultRace")
|
||||
cmd = testenv.CleanCmdEnv(cmd)
|
||||
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_RAND_TEST_HELPER_CODE=%d", i/2))
|
||||
if i%2 != 0 {
|
||||
|
@ -159,42 +159,16 @@ func helperCommandContext(t *testing.T, ctx context.Context, name string, args .
|
||||
helperCommandUsed.LoadOrStore(name, true)
|
||||
|
||||
t.Helper()
|
||||
testenv.MustHaveExec(t)
|
||||
|
||||
exe := testenv.Executable(t)
|
||||
cs := append([]string{name}, args...)
|
||||
if ctx != nil {
|
||||
cmd = exec.CommandContext(ctx, exePath(t), cs...)
|
||||
cmd = exec.CommandContext(ctx, exe, cs...)
|
||||
} else {
|
||||
cmd = exec.Command(exePath(t), cs...)
|
||||
cmd = exec.Command(exe, cs...)
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
// exePath returns the path to the running executable.
|
||||
func exePath(t testing.TB) string {
|
||||
exeOnce.Do(func() {
|
||||
// Use os.Executable instead of os.Args[0] in case the caller modifies
|
||||
// cmd.Dir: if the test binary is invoked like "./exec.test", it should
|
||||
// not fail spuriously.
|
||||
exeOnce.path, exeOnce.err = os.Executable()
|
||||
})
|
||||
|
||||
if exeOnce.err != nil {
|
||||
if t == nil {
|
||||
panic(exeOnce.err)
|
||||
}
|
||||
t.Fatal(exeOnce.err)
|
||||
}
|
||||
|
||||
return exeOnce.path
|
||||
}
|
||||
|
||||
var exeOnce struct {
|
||||
path string
|
||||
err error
|
||||
sync.Once
|
||||
}
|
||||
|
||||
func chdir(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
|
||||
@ -1201,7 +1175,7 @@ func cmdHang(args ...string) {
|
||||
pid := os.Getpid()
|
||||
|
||||
if *subsleep != 0 {
|
||||
cmd := exec.Command(exePath(nil), "hang", subsleep.String(), "-read=true", "-probe="+probe.String())
|
||||
cmd := exec.Command(testenv.Executable(nil), "hang", subsleep.String(), "-read=true", "-probe="+probe.String())
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stderr = os.Stderr
|
||||
out, err := cmd.StdoutPipe()
|
||||
|
@ -25,13 +25,8 @@ func init() {
|
||||
registerHelperCommand("printpath", cmdPrintPath)
|
||||
}
|
||||
|
||||
func cmdPrintPath(args ...string) {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Executable: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(exe)
|
||||
func cmdPrintPath(_ ...string) {
|
||||
fmt.Println(testenv.Executable(nil))
|
||||
}
|
||||
|
||||
// makePATH returns a PATH variable referring to the
|
||||
@ -82,7 +77,7 @@ func installProgs(t *testing.T, root string, files []string) {
|
||||
// (We use a copy instead of just a symlink to ensure that os.Executable
|
||||
// always reports an unambiguous path, regardless of how it is implemented.)
|
||||
func installExe(t *testing.T, dstPath string) {
|
||||
src, err := os.Open(exePath(t))
|
||||
src, err := os.Open(testenv.Executable(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -16,13 +16,9 @@ import (
|
||||
const executable_EnvVar = "OSTEST_OUTPUT_EXECPATH"
|
||||
|
||||
func TestExecutable(t *testing.T) {
|
||||
testenv.MustHaveExec(t)
|
||||
t.Parallel()
|
||||
|
||||
ep, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatalf("Executable failed: %v", err)
|
||||
}
|
||||
ep := testenv.Executable(t)
|
||||
// we want fn to be of the form "dir/prog"
|
||||
dir := filepath.Dir(filepath.Dir(ep))
|
||||
fn, err := filepath.Rel(dir, ep)
|
||||
|
@ -161,10 +161,7 @@ func TestNonPollable(t *testing.T) {
|
||||
|
||||
// Issue 60211.
|
||||
func TestOpenFileNonBlocking(t *testing.T) {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Skipf("can't find executable: %v", err)
|
||||
}
|
||||
exe := testenv.Executable(t)
|
||||
f, err := os.OpenFile(exe, os.O_RDONLY|syscall.O_NONBLOCK, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -2336,13 +2336,8 @@ func TestStatStdin(t *testing.T) {
|
||||
Exit(0)
|
||||
}
|
||||
|
||||
exe, err := Executable()
|
||||
if err != nil {
|
||||
t.Skipf("can't find executable: %v", err)
|
||||
}
|
||||
|
||||
testenv.MustHaveExec(t)
|
||||
t.Parallel()
|
||||
exe := testenv.Executable(t)
|
||||
|
||||
fi, err := Stdin.Stat()
|
||||
if err != nil {
|
||||
@ -2508,11 +2503,10 @@ func TestLongPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func testKillProcess(t *testing.T, processKiller func(p *Process)) {
|
||||
testenv.MustHaveExec(t)
|
||||
t.Parallel()
|
||||
|
||||
// Re-exec the test binary to start a process that hangs until stdin is closed.
|
||||
cmd := testenv.Command(t, Args[0])
|
||||
cmd := testenv.Command(t, testenv.Executable(t))
|
||||
cmd.Env = append(cmd.Environ(), "GO_OS_TEST_DRAIN_STDIN=1")
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
@ -2561,10 +2555,9 @@ func TestGetppid(t *testing.T) {
|
||||
Exit(0)
|
||||
}
|
||||
|
||||
testenv.MustHaveExec(t)
|
||||
t.Parallel()
|
||||
|
||||
cmd := testenv.Command(t, Args[0], "-test.run=^TestGetppid$")
|
||||
cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestGetppid$")
|
||||
cmd.Env = append(Environ(), "GO_WANT_HELPER_PROCESS=1")
|
||||
|
||||
// verify that Getppid() from the forked process reports our process id
|
||||
|
@ -1221,10 +1221,7 @@ func TestRootDirAsTemp(t *testing.T) {
|
||||
testenv.MustHaveExec(t)
|
||||
t.Parallel()
|
||||
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
exe := testenv.Executable(t)
|
||||
|
||||
newtmp, err := findUnusedDriveLetter()
|
||||
if err != nil {
|
||||
|
@ -170,10 +170,7 @@ func TestCurrentNetapi32(t *testing.T) {
|
||||
os.Exit(0)
|
||||
return
|
||||
}
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
exe := testenv.Executable(t)
|
||||
cmd := testenv.CleanCmdEnv(exec.Command(exe, "-test.run=^TestCurrentNetapi32$"))
|
||||
cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
Loading…
Reference in New Issue
Block a user