import (
"bytes"
"crypto/md5"
+ "internal/testenv"
"os"
"os/exec"
"runtime"
}
runtime.GOMAXPROCS(threads)
argv := append(os.Args, "-test.run=^$")
- if err := syscall.Exec(os.Args[0], argv, os.Environ()); err != nil {
+ if err := syscall.Exec(testenv.Executable(t), argv, os.Environ()); err != nil {
t.Fatal(err)
}
}
args := append(append([]string(nil), os.Args[1:]...), "-test.run=^Test18146$")
for n := attempts; n > 0; n-- {
- cmd := exec.Command(os.Args[0], args...)
+ cmd := exec.Command(testenv.Executable(t), args...)
cmd.Env = append(os.Environ(), "test18146=exec")
buf := bytes.NewBuffer(nil)
cmd.Stdout = buf
// Some kinds of file locks are dropped when a duplicated or forked file
// descriptor is unlocked. Double-check that the approach used by os/exec does
// not accidentally drop locks.
- cmd := testenv.Command(t, os.Args[0], "-test.run=^$")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^$")
if err := cmd.Run(); err != nil {
t.Fatalf("exec failed: %v", err)
}
// P.2 unblocks and locks file B.
// P.2 unlocks file B.
- testenv.MustHaveExec(t)
-
dirVar := t.Name() + "DIR"
if dir := os.Getenv(dirVar); dir != "" {
t.Fatal(err)
}
- cmd := testenv.Command(t, os.Args[0], "-test.run=^"+t.Name()+"$")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^"+t.Name()+"$")
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", dirVar, dir))
qDone := make(chan struct{})
t.Skipf("skipping: %v", err)
}
- cmd := testenv.Command(t, os.Args[0], "-test.v", "-test.run=TestIntegrityCheck")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.v", "-test.run=TestIntegrityCheck")
cmd.Env = append(cmd.Environ(), "GODEBUG=fips140=on")
out, err := cmd.CombinedOutput()
if err != nil {
return
}
- cmd := testenv.Command(t, os.Args[0], "-test.v")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.v")
cmd.Env = append(os.Environ(), "GO_GETRANDOM_DISABLED=1")
out, err := cmd.CombinedOutput()
if err != nil {
return
}
- cmd := testenv.Command(t, os.Args[0], "-test.run=TestReadError")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=TestReadError")
cmd.Env = append(os.Environ(), "GO_TEST_READ_ERROR=1")
out, err := cmd.CombinedOutput()
if err == nil {
if testing.Short() {
t.Skip("skipping test in short mode")
}
- testenv.MustHaveExec(t)
// We run this test in a subprocess because it's expected to crash.
if os.Getenv("GO_TEST_READ_ERROR") == "1" {
return
}
- cmd := testenv.Command(t, os.Args[0], "-test.run=TestReadError")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=TestReadError")
cmd.Env = append(os.Environ(), "GO_TEST_READ_ERROR=1")
out, err := cmd.CombinedOutput()
if err == nil {
}
for _, test := range tests {
- cmd := exec.Command(os.Args[0], "-test.run=^TestExitCode$")
+ cmd := exec.Command(testenv.Executable(t), "-test.run=^TestExitCode$")
cmd.Env = append(
os.Environ(),
"GO_CHILD_FLAG="+test.flag,
. "internal/cpu"
"internal/godebug"
"internal/testenv"
- "os"
"os/exec"
"testing"
)
func runDebugOptionsTest(t *testing.T, test string, options string) {
MustHaveDebugOptionsSupport(t)
- testenv.MustHaveExec(t)
-
env := "GODEBUG=" + options
- cmd := exec.Command(os.Args[0], "-test.run=^"+test+"$")
+ cmd := exec.Command(testenv.Executable(t), "-test.run=^"+test+"$")
cmd.Env = append(cmd.Env, env)
output, err := cmd.CombinedOutput()
t.Skip("Skipping test intended for use with -race.")
}
if os.Getenv("GODEBUG") != "panicnil=1" {
- cmd := testenv.CleanCmdEnv(testenv.Command(t, os.Args[0], "-test.run=^TestPanicNilRace$", "-test.v", "-test.parallel=2", "-test.count=1"))
+ cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.Executable(t), "-test.run=^TestPanicNilRace$", "-test.v", "-test.parallel=2", "-test.count=1"))
cmd.Env = append(cmd.Env, "GODEBUG=panicnil=1")
out, err := cmd.CombinedOutput()
t.Logf("output:\n%s", out)
import (
"fmt"
"internal/syscall/windows"
+ "internal/testenv"
"os"
"os/exec"
"syscall"
return
}
- cmd := exec.Command(os.Args[0], "-test.run=^TestRunAtLowIntegrity$", "--")
+ cmd := exec.Command(testenv.Executable(t), "-test.run=^TestRunAtLowIntegrity$", "--")
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
token, err := getIntegrityLevelToken(sidWilLow)
return nil, err
}
- cmd := testenv.Command(t, os.Args[0])
+ cmd := testenv.Command(t, testenv.Executable(t))
cmd.Env = []string{
"GO_NET_TEST_TRANSFER=1",
"GO_NET_TEST_TRANSFER_OP=" + op,
defer ln.Close()
// Start child process that connects to our listener.
- cmd := exec.Command(os.Args[0], "-test.run=TestAcceptIgnoreSomeErrors")
+ cmd := exec.Command(testenv.Executable(t), "-test.run=TestAcceptIgnoreSomeErrors")
cmd.Env = append(os.Environ(), "GOTEST_DIAL_ADDR="+ln.Addr().String())
stdout, err := cmd.StdoutPipe()
if err != nil {
// all writes should fail with EPIPE and then exit 0.
for _, sig := range []bool{false, true} {
for dest := 1; dest < 4; dest++ {
- cmd := testenv.Command(t, os.Args[0], "-test.run", "TestStdPipe")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run", "TestStdPipe")
cmd.Stdout = w
cmd.Stderr = w
cmd.ExtraFiles = []*os.File{w}
}
// Test redirecting stdout but not stderr. Issue 40076.
- cmd := testenv.Command(t, os.Args[0], "-test.run", "TestStdPipe")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run", "TestStdPipe")
cmd.Stdout = w
var stderr bytes.Buffer
cmd.Stderr = &stderr
}
defer r.Close()
defer w.Close()
- cmd := testenv.Command(t, os.Args[0], "-test.run=^"+t.Name()+"$")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^"+t.Name()+"$")
cmd.Env = append(cmd.Environ(), "GO_WANT_READ_NONBLOCKING_FD=1")
cmd.Stdin = r
output, err := cmd.CombinedOutput()
// We have no intention of reading from c.
c := make(chan os.Signal, 1)
Notify(c, syscall.SIGHUP)
- if out, err := testenv.Command(t, os.Args[0], "-test.run=^TestDetectNohup$", "-check_sighup_ignored").CombinedOutput(); err == nil {
+ if out, err := testenv.Command(t, testenv.Executable(t), "-test.run=^TestDetectNohup$", "-check_sighup_ignored").CombinedOutput(); err == nil {
t.Errorf("ran test with -check_sighup_ignored and it succeeded: expected failure.\nOutput:\n%s", out)
}
Stop(c)
}
Ignore(syscall.SIGHUP)
os.Remove("nohup.out")
- out, err := testenv.Command(t, "/usr/bin/nohup", os.Args[0], "-test.run=^TestDetectNohup$", "-check_sighup_ignored").CombinedOutput()
+ out, err := testenv.Command(t, "/usr/bin/nohup", testenv.Executable(t), "-test.run=^TestDetectNohup$", "-check_sighup_ignored").CombinedOutput()
data, _ := os.ReadFile("nohup.out")
os.Remove("nohup.out")
if subTimeout != 0 {
args = append(args, fmt.Sprintf("-test.timeout=%v", subTimeout))
}
- out, err := testenv.Command(t, os.Args[0], args...).CombinedOutput()
+ out, err := testenv.Command(t, testenv.Executable(t), args...).CombinedOutput()
if err == nil {
t.Errorf("ran test with -send_uncaught_sighup=%d and it succeeded: expected failure.\nOutput:\n%s", i, out)
if deadline, ok := t.Deadline(); ok {
timeout = time.Until(deadline).String()
}
- cmd := testenv.Command(t, os.Args[0], "-test.run=^TestAtomicStop$", "-test.timeout="+timeout)
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestAtomicStop$", "-test.timeout="+timeout)
cmd.Env = append(os.Environ(), "GO_TEST_ATOMIC_STOP=1")
out, err := cmd.CombinedOutput()
if err == nil {
if subTimeout != 0 {
args = append(args, fmt.Sprintf("-test.timeout=%v", subTimeout))
}
- out, err := testenv.Command(t, os.Args[0], args...).CombinedOutput()
+ out, err := testenv.Command(t, testenv.Executable(t), args...).CombinedOutput()
if err != nil {
t.Errorf("ran test with -check_notify_ctx_notification and it failed with %v.\nOutput:\n%s", err, out)
}
func (*TintPointer) m() {}
func TestFinalizerRegisterABI(t *testing.T) {
- testenv.MustHaveExec(t)
-
// Actually run the test in a subprocess because we don't want
// finalizers from other tests interfering.
if os.Getenv("TEST_FINALIZER_REGABI") != "1" {
- cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=^TestFinalizerRegisterABI$", "-test.v"))
+ cmd := testenv.CleanCmdEnv(exec.Command(testenv.Executable(t), "-test.run=^TestFinalizerRegisterABI$", "-test.v"))
cmd.Env = append(cmd.Env, "TEST_FINALIZER_REGABI=1")
out, err := cmd.CombinedOutput()
if !strings.Contains(string(out), "PASS\n") || err != nil {
}
func TestRuntimePanic(t *testing.T) {
- testenv.MustHaveExec(t)
- cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=^TestRuntimePanic$"))
+ cmd := testenv.CleanCmdEnv(exec.Command(testenv.Executable(t), "-test.run=^TestRuntimePanic$"))
cmd.Env = append(cmd.Env, "GO_TEST_RUNTIME_PANIC=1")
out, err := cmd.CombinedOutput()
t.Logf("%s", out)
}
func TestTracebackRuntimeFunction(t *testing.T) {
- testenv.MustHaveExec(t)
- cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=TestTracebackRuntimeFunction"))
+ cmd := testenv.CleanCmdEnv(exec.Command(testenv.Executable(t), "-test.run=TestTracebackRuntimeFunction"))
cmd.Env = append(cmd.Env, "GO_TEST_RUNTIME_NPE_READMEMSTATS=1")
out, err := cmd.CombinedOutput()
t.Logf("%s", out)
}
func TestTracebackRuntimeMethod(t *testing.T) {
- testenv.MustHaveExec(t)
- cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=TestTracebackRuntimeMethod"))
+ cmd := testenv.CleanCmdEnv(exec.Command(testenv.Executable(t), "-test.run=TestTracebackRuntimeMethod"))
cmd.Env = append(cmd.Env, "GO_TEST_RUNTIME_NPE_FUNCMETHOD=1")
out, err := cmd.CombinedOutput()
t.Logf("%s", out)
// Test that g0 stack overflows are handled gracefully.
func TestG0StackOverflow(t *testing.T) {
- testenv.MustHaveExec(t)
-
if runtime.GOOS == "ios" {
testenv.SkipFlaky(t, 62671)
}
if os.Getenv("TEST_G0_STACK_OVERFLOW") != "1" {
- cmd := testenv.CleanCmdEnv(testenv.Command(t, os.Args[0], "-test.run=^TestG0StackOverflow$", "-test.v"))
+ cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.Executable(t), "-test.run=^TestG0StackOverflow$", "-test.v"))
cmd.Env = append(cmd.Env, "TEST_G0_STACK_OVERFLOW=1")
out, err := cmd.CombinedOutput()
t.Logf("output:\n%s", out)
func TestCrashWhileTracing(t *testing.T) {
testenv.MustHaveExec(t)
- cmd := testenv.CleanCmdEnv(testenv.Command(t, os.Args[0]))
+ cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.Executable(t)))
cmd.Env = append(cmd.Env, "TEST_CRASH_WHILE_TRACING=1")
stdOut, err := cmd.StdoutPipe()
var errOut bytes.Buffer
}
t.Parallel()
- cmd := exec.Command(os.Args[0], "testPanicSystemstackInternal")
+ cmd := exec.Command(testenv.Executable(t), "testPanicSystemstackInternal")
cmd = testenv.CleanCmdEnv(cmd)
cmd.Dir = t.TempDir() // put any core file in tempdir
cmd.Env = append(cmd.Env, "GOTRACEBACK=crash")
// initial (not current) environment. Spawn a subprocess to determine the
// real baked-in GOROOT.
t.Logf("found GOROOT %q from environment; checking embedded GOROOT value", envGoroot)
- testenv.MustHaveExec(t)
- exe, err := os.Executable()
- if err != nil {
- t.Fatal(err)
- }
- cmd := exec.Command(exe)
+ cmd := exec.Command(testenv.Executable(t))
cmd.Env = append(os.Environ(), "GOROOT=", "GO_RUNTIME_DEBUG_TEST_ENTRYPOINT=dumpgoroot")
out, err := cmd.Output()
if err != nil {
}
func TestSetCrashOutput(t *testing.T) {
- testenv.MustHaveExec(t)
- exe, err := os.Executable()
- if err != nil {
- t.Fatal(err)
- }
-
crashOutput := filepath.Join(t.TempDir(), "crash.out")
- cmd := exec.Command(exe)
+ cmd := exec.Command(testenv.Executable(t))
cmd.Stderr = new(strings.Builder)
cmd.Env = append(os.Environ(), "GO_RUNTIME_DEBUG_TEST_ENTRYPOINT=setcrashoutput", "CRASHOUTPUT="+crashOutput)
- err = cmd.Run()
+ err := cmd.Run()
stderr := fmt.Sprint(cmd.Stderr)
if err == nil {
t.Fatalf("child process succeeded unexpectedly (stderr: %s)", stderr)
}
func TestIssue66841(t *testing.T) {
- testenv.MustHaveExec(t)
if *UseAeshash && os.Getenv("TEST_ISSUE_66841") == "" {
// We want to test the backup hash, so if we're running on a machine
// that uses aeshash, exec ourselves while turning aes off.
- cmd := testenv.CleanCmdEnv(testenv.Command(t, os.Args[0], "-test.run=^TestIssue66841$"))
+ cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.Executable(t), "-test.run=^TestIssue66841$"))
cmd.Env = append(cmd.Env, "GODEBUG=cpu.aes=off", "TEST_ISSUE_66841=1")
out, err := cmd.CombinedOutput()
if err != nil {
var arenaCollisionSink []*acLink
func TestArenaCollision(t *testing.T) {
- testenv.MustHaveExec(t)
-
// Test that mheap.sysAlloc handles collisions with other
// memory mappings.
if os.Getenv("TEST_ARENA_COLLISION") != "1" {
- cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=^TestArenaCollision$", "-test.v"))
+ cmd := testenv.CleanCmdEnv(exec.Command(testenv.Executable(t), "-test.run=^TestArenaCollision$", "-test.v"))
cmd.Env = append(cmd.Env, "TEST_ARENA_COLLISION=1")
out, err := cmd.CombinedOutput()
if race.Enabled {
func subprocessHash(t *testing.T, env string) uintptr {
t.Helper()
- cmd := testenv.CleanCmdEnv(testenv.Command(t, os.Args[0], "-test.run=^TestMemHashGlobalSeed$"))
+ cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.Executable(t), "-test.run=^TestMemHashGlobalSeed$"))
cmd.Env = append(cmd.Env, "GO_TEST_SUBPROCESS_HASH=1")
if env != "" {
cmd.Env = append(cmd.Env, env)
_GetProcessAffinityMask := kernel32.MustFindProc("GetProcessAffinityMask")
_SetProcessAffinityMask := kernel32.MustFindProc("SetProcessAffinityMask")
- cmd := exec.Command(os.Args[0], "-test.run=TestNumCPU")
+ cmd := exec.Command(testenv.Executable(t), "-test.run=TestNumCPU")
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
var buf strings.Builder
cmd.Stdout = &buf
}
func TestMutexMisuse(t *testing.T) {
- testenv.MustHaveExec(t)
for _, test := range misuseTests {
- out, err := exec.Command(os.Args[0], "TESTMISUSE", test.name).CombinedOutput()
+ out, err := exec.Command(testenv.Executable(t), "TESTMISUSE", test.name).CombinedOutput()
if err == nil || !strings.Contains(string(out), "unlocked") {
t.Errorf("%s: did not find failure with message about unlocked lock: %s\n%s\n", test.name, err, out)
}
// TestExec is for issue #41702.
func TestExec(t *testing.T) {
- testenv.MustHaveExec(t)
- cmd := exec.Command(os.Args[0], "-test.run=^TestExecHelper$")
+ cmd := exec.Command(testenv.Executable(t), "-test.run=^TestExecHelper$")
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=2")
o, err := cmd.CombinedOutput()
if err != nil {
import (
"fmt"
+ "internal/testenv"
"os"
"os/exec"
"path/filepath"
// run parent process
- parent := exec.Command(os.Args[0], "-test.run=^TestChangingProcessParent$")
+ parent := exec.Command(testenv.Executable(t), "-test.run=^TestChangingProcessParent$")
parent.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=parent")
err := parent.Start()
if err != nil {
}
defer syscall.CloseHandle(ph)
- child := exec.Command(os.Args[0], "-test.run=^TestChangingProcessParent$")
+ child := exec.Command(testenv.Executable(t), "-test.run=^TestChangingProcessParent$")
child.Env = append(os.Environ(),
"GO_WANT_HELPER_PROCESS=child",
"GO_WANT_HELPER_PROCESS_FILE="+childDumpPath)
t.Fatalf("FcntlFlock(F_SETLK) failed: %v", err)
}
- cmd := exec.Command(os.Args[0], "-test.run=^TestFcntlFlock$")
+ cmd := exec.Command(testenv.Executable(t), "-test.run=^TestFcntlFlock$")
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
cmd.ExtraFiles = []*os.File{f}
out, err := cmd.CombinedOutput()
defer writeFile.Close()
defer readFile.Close()
- cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir)
+ cmd := exec.Command(testenv.Executable(t), "-test.run=^TestPassFD$", "--", tempDir)
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
cmd.ExtraFiles = []*os.File{writeFile}
}}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
- cmd := exec.Command(os.Args[0], "-test.run=^TestPanicHelper$")
+ cmd := exec.Command(testenv.Executable(t), "-test.run=^TestPanicHelper$")
cmd.Args = append(cmd.Args, tc.flags...)
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
b, _ := cmd.CombinedOutput()
}
for _, tc := range testCases {
- cmd := exec.Command(os.Args[0], tc.flags...)
+ cmd := exec.Command(testenv.Executable(t), tc.flags...)
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
b, _ := cmd.CombinedOutput()
got := string(b)
timeout := 10 * time.Millisecond
for {
- cmd := testenv.Command(t, os.Args[0], "-test.run=^"+t.Name()+"$", "-test.timeout="+timeout.String(), "-test.parallel=4")
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^"+t.Name()+"$", "-test.timeout="+timeout.String(), "-test.parallel=4")
cmd.Env = append(cmd.Environ(), "GO_WANT_HELPER_PROCESS=1")
out, err := cmd.CombinedOutput()
t.Logf("%v:\n%s", cmd, out)
timeout := 10 * time.Millisecond
for {
- cmd := testenv.Command(t, os.Args[0], "-test.run=^"+t.Name()+"$", "-test.timeout="+timeout.String())
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^"+t.Name()+"$", "-test.timeout="+timeout.String())
cmd.Env = append(cmd.Environ(), "GO_WANT_HELPER_PROCESS=1")
out, err := cmd.CombinedOutput()
t.Logf("%v:\n%s", cmd, out)