cmd/compile: use testenv.Command instead of exec.Command in tests

testenv.Command sets a default timeout based on the test's deadline
and sends SIGQUIT (where supported) in case of a hang.

Change-Id: I084b324a20d5ecf733b2cb95f160947a7410a805
Reviewed-on: https://go-review.googlesource.com/c/go/+/450696
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
This commit is contained in:
Bryan C. Mills 2022-11-15 09:57:01 -05:00 committed by Gopher Robot
parent 9754bc7bb7
commit 2cb103ff58
18 changed files with 49 additions and 55 deletions

View file

@ -72,7 +72,7 @@ func TestGoAMD64v1(t *testing.T) {
} }
// Run the resulting binary. // Run the resulting binary.
cmd := exec.Command(dst.Name()) cmd := testenv.Command(t, dst.Name())
testenv.CleanCmdEnv(cmd) testenv.CleanCmdEnv(cmd)
cmd.Env = append(cmd.Env, "TESTGOAMD64V1=yes") cmd.Env = append(cmd.Env, "TESTGOAMD64V1=yes")
cmd.Env = append(cmd.Env, fmt.Sprintf("GODEBUG=%s", strings.Join(features, ","))) cmd.Env = append(cmd.Env, fmt.Sprintf("GODEBUG=%s", strings.Join(features, ",")))
@ -104,7 +104,7 @@ func clobber(t *testing.T, src string, dst *os.File, opcodes map[string]bool) {
if false { if false {
// TODO: go tool objdump doesn't disassemble the bmi1 instructions // TODO: go tool objdump doesn't disassemble the bmi1 instructions
// in question correctly. See issue 48584. // in question correctly. See issue 48584.
cmd := exec.Command("go", "tool", "objdump", src) cmd := testenv.Command(t, "go", "tool", "objdump", src)
var err error var err error
disasm, err = cmd.StdoutPipe() disasm, err = cmd.StdoutPipe()
if err != nil { if err != nil {
@ -113,11 +113,16 @@ func clobber(t *testing.T, src string, dst *os.File, opcodes map[string]bool) {
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Cleanup(func() {
if err := cmd.Wait(); err != nil {
t.Error(err)
}
})
re = regexp.MustCompile(`^[^:]*:[-\d]+\s+0x([\da-f]+)\s+([\da-f]+)\s+([A-Z]+)`) re = regexp.MustCompile(`^[^:]*:[-\d]+\s+0x([\da-f]+)\s+([\da-f]+)\s+([A-Z]+)`)
} else { } else {
// TODO: we're depending on platform-native objdump here. Hence the Skipf // TODO: we're depending on platform-native objdump here. Hence the Skipf
// below if it doesn't run for some reason. // below if it doesn't run for some reason.
cmd := exec.Command("objdump", "-d", src) cmd := testenv.Command(t, "objdump", "-d", src)
var err error var err error
disasm, err = cmd.StdoutPipe() disasm, err = cmd.StdoutPipe()
if err != nil { if err != nil {
@ -129,6 +134,11 @@ func clobber(t *testing.T, src string, dst *os.File, opcodes map[string]bool) {
} }
t.Fatal(err) t.Fatal(err)
} }
t.Cleanup(func() {
if err := cmd.Wait(); err != nil {
t.Error(err)
}
})
re = regexp.MustCompile(`^\s*([\da-f]+):\s*((?:[\da-f][\da-f] )+)\s*([a-z\d]+)`) re = regexp.MustCompile(`^\s*([\da-f]+):\s*((?:[\da-f][\da-f] )+)\s*([a-z\d]+)`)
} }

View file

@ -9,7 +9,6 @@ import (
"fmt" "fmt"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort" "sort"
@ -474,7 +473,7 @@ func gobuild(t *testing.T, dir string, optimized bool, testfile []testline) (str
} }
args = append(args, "-o", dst, src) args = append(args, "-o", dst, src)
cmd := exec.Command(testenv.GoToolPath(t), args...) cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
if b, err := cmd.CombinedOutput(); err != nil { if b, err := cmd.CombinedOutput(); err != nil {
t.Logf("build: %s\n", string(b)) t.Logf("build: %s\n", string(b))
t.Fatal(err) t.Fatal(err)

View file

@ -12,7 +12,6 @@ import (
"internal/goexperiment" "internal/goexperiment"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -40,7 +39,7 @@ func compile(t *testing.T, dirname, filename, outdirname string, packagefiles ma
importcfgfile := filepath.Join(outdirname, basename) + ".importcfg" importcfgfile := filepath.Join(outdirname, basename) + ".importcfg"
testenv.WriteImportcfg(t, importcfgfile, packagefiles) testenv.WriteImportcfg(t, importcfgfile, packagefiles)
pkgpath := path.Join("testdata", basename) pkgpath := path.Join("testdata", basename)
cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p", pkgpath, "-D", "testdata", "-importcfg", importcfgfile, "-o", outname, filename) cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p", pkgpath, "-D", "testdata", "-importcfg", importcfgfile, "-o", outname, filename)
cmd.Dir = dirname cmd.Dir = dirname
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {

View file

@ -7,7 +7,6 @@ package logopt
import ( import (
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -227,7 +226,7 @@ func s15a8(x *[15]int64) [15]int64 {
func testLogOpt(t *testing.T, flag, src, outfile string) (string, error) { func testLogOpt(t *testing.T, flag, src, outfile string) (string, error) {
run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=p", flag, "-o", outfile, src} run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=p", flag, "-o", outfile, src}
t.Log(run) t.Log(run)
cmd := exec.Command(run[0], run[1:]...) cmd := testenv.Command(t, run[0], run[1:]...)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
t.Logf("%s", out) t.Logf("%s", out)
return string(out), err return string(out), err
@ -237,7 +236,7 @@ func testLogOptDir(t *testing.T, dir, flag, src, outfile string) (string, error)
// Notice the specified import path "x" // Notice the specified import path "x"
run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=x", flag, "-o", outfile, src} run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=x", flag, "-o", outfile, src}
t.Log(run) t.Log(run)
cmd := exec.Command(run[0], run[1:]...) cmd := testenv.Command(t, run[0], run[1:]...)
cmd.Dir = dir cmd.Dir = dir
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
t.Logf("%s", out) t.Logf("%s", out)
@ -248,7 +247,7 @@ func testCopy(t *testing.T, dir, goarch, goos, src, outfile string) (string, err
// Notice the specified import path "x" // Notice the specified import path "x"
run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=x", "-json=0,file://log/opt", "-o", outfile, src} run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=x", "-json=0,file://log/opt", "-o", outfile, src}
t.Log(run) t.Log(run)
cmd := exec.Command(run[0], run[1:]...) cmd := testenv.Command(t, run[0], run[1:]...)
cmd.Dir = dir cmd.Dir = dir
cmd.Env = append(os.Environ(), "GOARCH="+goarch, "GOOS="+goos) cmd.Env = append(os.Environ(), "GOARCH="+goarch, "GOOS="+goos)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()

View file

@ -12,7 +12,6 @@ import (
"internal/buildcfg" "internal/buildcfg"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp" "regexp"
@ -142,7 +141,7 @@ func compileAndDump(t *testing.T, file, function, moreGCFlags string) []byte {
panic(fmt.Sprintf("Could not get abspath of testdata directory and file, %v", err)) panic(fmt.Sprintf("Could not get abspath of testdata directory and file, %v", err))
} }
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "foo.o", "-gcflags=-d=ssa/genssa/dump="+function+" "+moreGCFlags, source) cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "foo.o", "-gcflags=-d=ssa/genssa/dump="+function+" "+moreGCFlags, source)
cmd.Dir = tmpdir cmd.Dir = tmpdir
cmd.Env = replaceEnv(cmd.Env, "GOSSADIR", tmpdir) cmd.Env = replaceEnv(cmd.Env, "GOSSADIR", tmpdir)
testGoos := "linux" // default to linux testGoos := "linux" // default to linux

View file

@ -244,9 +244,9 @@ func testNexting(t *testing.T, base, tag, gcflags string, count int, moreArgs ..
tmplog := tmpbase + ".nexts" tmplog := tmpbase + ".nexts"
var dbg dbgr var dbg dbgr
if *useGdb { if *useGdb {
dbg = newGdb(tag, exe) dbg = newGdb(t, tag, exe)
} else { } else {
dbg = newDelve(tag, exe) dbg = newDelve(t, tag, exe)
} }
h1 := runDbgr(dbg, count) h1 := runDbgr(dbg, count)
if *dryrun { if *dryrun {
@ -261,7 +261,7 @@ func testNexting(t *testing.T, base, tag, gcflags string, count int, moreArgs ..
if !h0.equals(h1) { if !h0.equals(h1) {
// Be very noisy about exactly what's wrong to simplify debugging. // Be very noisy about exactly what's wrong to simplify debugging.
h1.write(tmplog) h1.write(tmplog)
cmd := exec.Command("diff", "-u", nextlog, tmplog) cmd := testenv.Command(t, "diff", "-u", nextlog, tmplog)
line := asCommandLine("", cmd) line := asCommandLine("", cmd)
bytes, err := cmd.CombinedOutput() bytes, err := cmd.CombinedOutput()
if err != nil && len(bytes) == 0 { if err != nil && len(bytes) == 0 {
@ -297,7 +297,7 @@ func runDbgr(dbg dbgr, maxNext int) *nextHist {
func runGo(t *testing.T, dir string, args ...string) string { func runGo(t *testing.T, dir string, args ...string) string {
var stdout, stderr strings.Builder var stdout, stderr strings.Builder
cmd := exec.Command(testenv.GoToolPath(t), args...) cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
cmd.Dir = dir cmd.Dir = dir
if *dryrun { if *dryrun {
fmt.Printf("%s\n", asCommandLine("", cmd)) fmt.Printf("%s\n", asCommandLine("", cmd))
@ -501,8 +501,8 @@ type delveState struct {
function string function string
} }
func newDelve(tag, executable string, args ...string) dbgr { func newDelve(t testing.TB, tag, executable string, args ...string) dbgr {
cmd := exec.Command("dlv", "exec", executable) cmd := testenv.Command(t, "dlv", "exec", executable)
cmd.Env = replaceEnv(cmd.Env, "TERM", "dumb") cmd.Env = replaceEnv(cmd.Env, "TERM", "dumb")
if len(args) > 0 { if len(args) > 0 {
cmd.Args = append(cmd.Args, "--") cmd.Args = append(cmd.Args, "--")
@ -586,9 +586,9 @@ type gdbState struct {
function string function string
} }
func newGdb(tag, executable string, args ...string) dbgr { func newGdb(t testing.TB, tag, executable string, args ...string) dbgr {
// Turn off shell, necessary for Darwin apparently // Turn off shell, necessary for Darwin apparently
cmd := exec.Command(gdb, "-nx", cmd := testenv.Command(t, gdb, "-nx",
"-iex", fmt.Sprintf("add-auto-load-safe-path %s/src/runtime", runtime.GOROOT()), "-iex", fmt.Sprintf("add-auto-load-safe-path %s/src/runtime", runtime.GOROOT()),
"-ex", "set startup-with-shell off", executable) "-ex", "set startup-with-shell off", executable)
cmd.Env = replaceEnv(cmd.Env, "TERM", "dumb") cmd.Env = replaceEnv(cmd.Env, "TERM", "dumb")

View file

@ -7,7 +7,6 @@ package ssa_test
import ( import (
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
@ -39,7 +38,7 @@ func TestFmaHash(t *testing.T) {
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
source := filepath.Join("testdata", "fma.go") source := filepath.Join("testdata", "fma.go")
output := filepath.Join(tmpdir, "fma.exe") output := filepath.Join(tmpdir, "fma.exe")
cmd := exec.Command(gocmd, "build", "-o", output, source) cmd := testenv.Command(t, gocmd, "build", "-o", output, source)
// The hash-dependence on file path name is dodged by specifying "all hashes ending in 1" plus "all hashes ending in 0" // The hash-dependence on file path name is dodged by specifying "all hashes ending in 1" plus "all hashes ending in 0"
// i.e., all hashes. This will print all the FMAs; this test is only interested in one of them (that should appear near the end). // i.e., all hashes. This will print all the FMAs; this test is only interested in one of them (that should appear near the end).
cmd.Env = append(cmd.Env, "GOCOMPILEDEBUG=fmahash=1/0", "GOOS=linux", "GOARCH=arm64", "HOME="+tmpdir) cmd.Env = append(cmd.Env, "GOCOMPILEDEBUG=fmahash=1/0", "GOOS=linux", "GOARCH=arm64", "HOME="+tmpdir)

View file

@ -7,7 +7,6 @@ package test
import ( import (
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"testing" "testing"
) )
@ -44,7 +43,7 @@ func runHello(t *testing.T, flag string) {
t.Fatalf("write file failed: %v", err) t.Fatalf("write file failed: %v", err)
} }
cmd := exec.Command(testenv.GoToolPath(t), "run", "-gcflags=all="+flag, src) cmd := testenv.Command(t, testenv.GoToolPath(t), "run", "-gcflags=all="+flag, src)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("go run failed: %v\n%s", err, out) t.Fatalf("go run failed: %v\n%s", err, out)

View file

@ -6,13 +6,12 @@ package test
import ( import (
"internal/testenv" "internal/testenv"
"os/exec"
"strings" "strings"
"testing" "testing"
) )
func TestDeps(t *testing.T) { func TestDeps(t *testing.T) {
out, err := exec.Command(testenv.GoToolPath(t), "list", "-f", "{{.Deps}}", "cmd/compile/internal/gc").Output() out, err := testenv.Command(t, testenv.GoToolPath(t), "list", "-f", "{{.Deps}}", "cmd/compile/internal/gc").Output()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -7,7 +7,6 @@ package test
import ( import (
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -71,7 +70,7 @@ func TestIssue16214(t *testing.T) {
t.Fatalf("could not write file: %v", err) t.Fatalf("could not write file: %v", err)
} }
cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=main", "-S", "-o", filepath.Join(dir, "out.o"), src) cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=main", "-S", "-o", filepath.Join(dir, "out.o"), src)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("go tool compile: %v\n%s", err, out) t.Fatalf("go tool compile: %v\n%s", err, out)

View file

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -46,14 +45,14 @@ func main() {
dst := filepath.Join(dir, "test") dst := filepath.Join(dir, "test")
// Compile source. // Compile source.
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dst, src) cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", dst, src)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("could not build target: %v\n%s", err, out) t.Fatalf("could not build target: %v\n%s", err, out)
} }
// Check destination to see if scanf code was included. // Check destination to see if scanf code was included.
cmd = exec.Command(testenv.GoToolPath(t), "tool", "nm", dst) cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", dst)
out, err = cmd.CombinedOutput() out, err = cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("could not read target: %v", err) t.Fatalf("could not read target: %v", err)
@ -91,7 +90,7 @@ func main() {
f.Close() f.Close()
// Compile source. // Compile source.
cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src) cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("could not build target: %v\n%s", err, out) t.Fatalf("could not build target: %v\n%s", err, out)

View file

@ -11,7 +11,6 @@ import (
"internal/testenv" "internal/testenv"
"io" "io"
"math/bits" "math/bits"
"os/exec"
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
@ -279,7 +278,7 @@ func TestIntendedInlining(t *testing.T) {
} }
args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...) args := append([]string{"build", "-gcflags=-m -m", "-tags=math_big_pure_go"}, pkgs...)
cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...)) cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), args...))
pr, pw := io.Pipe() pr, pw := io.Pipe()
cmd.Stdout = pw cmd.Stdout = pw
cmd.Stderr = pw cmd.Stderr = pw
@ -362,7 +361,7 @@ func TestIssue56044(t *testing.T) {
for _, mode := range modes { for _, mode := range modes {
// Build the Go runtime with "-m", capturing output. // Build the Go runtime with "-m", capturing output.
args := []string{"build", "-gcflags=runtime=-m", "runtime"} args := []string{"build", "-gcflags=runtime=-m", "runtime"}
cmd := exec.Command(testenv.GoToolPath(t), args...) cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
b, err := cmd.CombinedOutput() b, err := cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("build failed (%v): %s", err, b) t.Fatalf("build failed (%v): %s", err, b)
@ -371,7 +370,7 @@ func TestIssue56044(t *testing.T) {
// Redo the build with -cover, also with "-m". // Redo the build with -cover, also with "-m".
args = []string{"build", "-gcflags=runtime=-m", mode, "runtime"} args = []string{"build", "-gcflags=runtime=-m", mode, "runtime"}
cmd = exec.Command(testenv.GoToolPath(t), args...) cmd = testenv.Command(t, testenv.GoToolPath(t), args...)
b, err = cmd.CombinedOutput() b, err = cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("build failed (%v): %s", err, b) t.Fatalf("build failed (%v): %s", err, b)

View file

@ -7,7 +7,6 @@ package test
import ( import (
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"testing" "testing"
@ -34,14 +33,14 @@ func TestInst(t *testing.T) {
outname := "ptrsort.out" outname := "ptrsort.out"
gotool := testenv.GoToolPath(t) gotool := testenv.GoToolPath(t)
dest := filepath.Join(tmpdir, exename) dest := filepath.Join(tmpdir, exename)
cmd := exec.Command(gotool, "build", "-o", dest, filepath.Join("testdata", filename)) cmd := testenv.Command(t, gotool, "build", "-o", dest, filepath.Join("testdata", filename))
if output, err = cmd.CombinedOutput(); err != nil { if output, err = cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed: %v:\nOutput: %s\n", err, output) t.Fatalf("Failed: %v:\nOutput: %s\n", err, output)
} }
// Test that there is exactly one shape-based instantiation of Sort in // Test that there is exactly one shape-based instantiation of Sort in
// the executable. // the executable.
cmd = exec.Command(gotool, "tool", "nm", dest) cmd = testenv.Command(t, gotool, "tool", "nm", dest)
if output, err = cmd.CombinedOutput(); err != nil { if output, err = cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed: %v:\nOut: %s\n", err, output) t.Fatalf("Failed: %v:\nOut: %s\n", err, output)
} }
@ -54,7 +53,7 @@ func TestInst(t *testing.T) {
} }
// Actually run the test and make sure output is correct. // Actually run the test and make sure output is correct.
cmd = exec.Command(gotool, "run", filepath.Join("testdata", filename)) cmd = testenv.Command(t, gotool, "run", filepath.Join("testdata", filename))
if output, err = cmd.CombinedOutput(); err != nil { if output, err = cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed: %v:\nOut: %s\n", err, output) t.Fatalf("Failed: %v:\nOut: %s\n", err, output)
} }

View file

@ -7,7 +7,6 @@ package test
import ( import (
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"testing" "testing"
) )
@ -57,7 +56,7 @@ func TestInvalidLang(t *testing.T) {
func testLang(t *testing.T, lang, src, outfile string) error { func testLang(t *testing.T, lang, src, outfile string) error {
run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=p", "-lang", lang, "-o", outfile, src} run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=p", "-lang", lang, "-o", outfile, src}
t.Log(run) t.Log(run)
out, err := exec.Command(run[0], run[1:]...).CombinedOutput() out, err := testenv.Command(t, run[0], run[1:]...).CombinedOutput()
t.Logf("%s", out) t.Logf("%s", out)
return err return err
} }

View file

@ -10,7 +10,6 @@ import (
"internal/testenv" "internal/testenv"
"io" "io"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -72,7 +71,7 @@ go 1.19
pprof := filepath.Join(dir, "inline_hot.pprof") pprof := filepath.Join(dir, "inline_hot.pprof")
gcflag := fmt.Sprintf("-gcflags=-m -m -pgoprofile=%s -d=pgoinlinebudget=160,pgoinlinecdfthreshold=90", pprof) gcflag := fmt.Sprintf("-gcflags=-m -m -pgoprofile=%s -d=pgoinlinebudget=160,pgoinlinecdfthreshold=90", pprof)
out := filepath.Join(dir, "test.exe") out := filepath.Join(dir, "test.exe")
cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "test", "-c", "-o", out, gcflag, ".")) cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), "test", "-c", "-o", out, gcflag, "."))
cmd.Dir = dir cmd.Dir = dir
pr, pw, err := os.Pipe() pr, pw, err := os.Pipe()

View file

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"testing" "testing"
) )
@ -40,7 +39,7 @@ func TestReproducibleBuilds(t *testing.T) {
for i := 0; i < iters; i++ { for i := 0; i < iters; i++ {
// Note: use -c 2 to expose any nondeterminism which is the result // Note: use -c 2 to expose any nondeterminism which is the result
// of the runtime scheduler. // of the runtime scheduler.
out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=p", "-c", "2", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", test)).CombinedOutput() out, err := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=p", "-c", "2", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", test)).CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("failed to compile: %v\n%s", err, out) t.Fatalf("failed to compile: %v\n%s", err, out)
} }
@ -88,7 +87,7 @@ func TestIssue38068(t *testing.T) {
s := &scenarios[i] s := &scenarios[i]
s.libpath = filepath.Join(tmpdir, s.tag+".a") s.libpath = filepath.Join(tmpdir, s.tag+".a")
// Note: use of "-p" required in order for DWARF to be generated. // Note: use of "-p" required in order for DWARF to be generated.
cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=issue38068", "-buildid=", s.args, "-o", s.libpath, src) cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=issue38068", "-buildid=", s.args, "-o", s.libpath, src)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) t.Fatalf("%v: %v:\n%s", cmd.Args, err, out)

View file

@ -12,7 +12,6 @@ import (
"go/token" "go/token"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -27,7 +26,7 @@ func runGenTest(t *testing.T, filename, tmpname string, ev ...string) {
testenv.MustHaveGoRun(t) testenv.MustHaveGoRun(t)
gotool := testenv.GoToolPath(t) gotool := testenv.GoToolPath(t)
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd := exec.Command(gotool, "run", filepath.Join("testdata", filename)) cmd := testenv.Command(t, gotool, "run", filepath.Join("testdata", filename))
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stderr cmd.Stderr = &stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
@ -48,7 +47,7 @@ func runGenTest(t *testing.T, filename, tmpname string, ev ...string) {
stdout.Reset() stdout.Reset()
stderr.Reset() stderr.Reset()
cmd = exec.Command(gotool, "run", "-gcflags=-d=ssa/check/on", rungo) cmd = testenv.Command(t, gotool, "run", "-gcflags=-d=ssa/check/on", rungo)
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stderr cmd.Stderr = &stderr
cmd.Env = append(cmd.Env, ev...) cmd.Env = append(cmd.Env, ev...)
@ -167,7 +166,7 @@ func TestCode(t *testing.T) {
for _, flag := range flags { for _, flag := range flags {
args := []string{"test", "-c", "-gcflags=-d=ssa/check/on" + flag, "-o", filepath.Join(tmpdir, "code.test")} args := []string{"test", "-c", "-gcflags=-d=ssa/check/on" + flag, "-o", filepath.Join(tmpdir, "code.test")}
args = append(args, srcs...) args = append(args, srcs...)
out, err := exec.Command(gotool, args...).CombinedOutput() out, err := testenv.Command(t, gotool, args...).CombinedOutput()
if err != nil || len(out) != 0 { if err != nil || len(out) != 0 {
t.Fatalf("Build failed: %v\n%s\n", err, out) t.Fatalf("Build failed: %v\n%s\n", err, out)
} }
@ -180,7 +179,7 @@ func TestCode(t *testing.T) {
continue continue
} }
t.Run(fmt.Sprintf("%s%s", test.name[4:], flag), func(t *testing.T) { t.Run(fmt.Sprintf("%s%s", test.name[4:], flag), func(t *testing.T) {
out, err := exec.Command(filepath.Join(tmpdir, "code.test"), "-test.run="+test.name).CombinedOutput() out, err := testenv.Command(t, filepath.Join(tmpdir, "code.test"), "-test.run="+test.name).CombinedOutput()
if err != nil || string(out) != "PASS\n" { if err != nil || string(out) != "PASS\n" {
t.Errorf("Failed:\n%s\n", out) t.Errorf("Failed:\n%s\n", out)
} }

View file

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"internal/testenv" "internal/testenv"
"os" "os"
"os/exec"
"testing" "testing"
) )
@ -21,7 +20,7 @@ func TestBuiltin(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
new, err := exec.Command(testenv.GoToolPath(t), "run", "mkbuiltin.go", "-stdout").Output() new, err := testenv.Command(t, testenv.GoToolPath(t), "run", "mkbuiltin.go", "-stdout").Output()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }