)
func loadSyms(t *testing.T) map[string]string {
- cmd := exec.Command("go", "tool", "nm", os.Args[0])
+ cmd := exec.Command(testenv.GoToolPath(t), "tool", "nm", os.Args[0])
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out))
defer os.RemoveAll(tmpDir)
exepath := filepath.Join(tmpDir, "testaddr2line.exe")
- out, err := exec.Command("go", "build", "-o", exepath, "cmd/addr2line").CombinedOutput()
+ out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exepath, "cmd/addr2line").CombinedOutput()
if err != nil {
t.Fatalf("go build -o %v cmd/addr2line: %v\n%s", exepath, err, string(out))
}
defer os.RemoveAll(dir)
for _, test := range asmTests {
- asm := compileToAsm(dir, test.arch, fmt.Sprintf(template, test.function))
+ asm := compileToAsm(t, dir, test.arch, fmt.Sprintf(template, test.function))
// Get rid of code for "".init. Also gets rid of type algorithms & other junk.
if i := strings.Index(asm, "\n\"\".init "); i >= 0 {
asm = asm[:i+1]
// compile compiles the package pkg for architecture arch and
// returns the generated assembly. dir is a scratch directory.
-func compileToAsm(dir, arch, pkg string) string {
+func compileToAsm(t *testing.T, dir, arch, pkg string) string {
// Create source.
src := filepath.Join(dir, "test.go")
f, err := os.Create(src)
f.Close()
var stdout, stderr bytes.Buffer
- cmd := exec.Command("go", "tool", "compile", "-S", "-o", filepath.Join(dir, "out.o"), src)
+ cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-S", "-o", filepath.Join(dir, "out.o"), src)
cmd.Env = mergeEnvLists([]string{"GOARCH=" + arch}, os.Environ())
cmd.Stdout = &stdout
cmd.Stderr = &stderr
t.Fatalf("could not write file: %v", err)
}
- cmd := exec.Command("go", "tool", "compile", "-S", "-o", filepath.Join(dir, "out.o"), src)
+ cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-S", "-o", filepath.Join(dir, "out.o"), src)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("fail to run go tool compile: %v", err)
t.Fatal(err)
}
- new, err := exec.Command("go", "run", "mkbuiltin.go", "-stdout").Output()
+ new, err := exec.Command(testenv.GoToolPath(t), "run", "mkbuiltin.go", "-stdout").Output()
if err != nil {
t.Fatal(err)
}
dst := filepath.Join(dir, "test")
// Compile source.
- cmd := exec.Command("go", "build", "-o", dst, src)
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dst, src)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("could not build target: %v", err)
}
// Check destination to see if scanf code was included.
- cmd = exec.Command("go", "tool", "nm", dst)
+ cmd = exec.Command(testenv.GoToolPath(t), "tool", "nm", dst)
out, err = cmd.CombinedOutput()
if err != nil {
log.Fatalf("could not read target: %v", err)
f.Close()
// Compile source.
- cmd := exec.Command("go", "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src)
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("could not build target: %v", err)
func doTest(t *testing.T, filename string, kind string) {
testenv.MustHaveGoBuild(t)
var stdout, stderr bytes.Buffer
- cmd := exec.Command("go", kind, filepath.Join("testdata", filename))
+ cmd := exec.Command(testenv.GoToolPath(t), kind, filepath.Join("testdata", filename))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
}
// go build -o testcover
- cmd := exec.Command("go", "build", "-o", testcover)
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", testcover)
run(cmd, t)
// defer removal of testcover
}
// go run ./testdata/main.go ./testdata/test.go
- cmd = exec.Command("go", "run", testMain, coverOutput)
+ cmd = exec.Command(testenv.GoToolPath(t), "run", testMain, coverOutput)
run(cmd, t)
}
package main
import (
+ "internal/testenv"
"io/ioutil"
"os"
"os/exec"
noVolume := file[len(filepath.VolumeName(file)):]
wrongPath := filepath.Join(dir, noVolume)
- output, err := exec.Command("go", "build", noVolume).CombinedOutput()
+ output, err := exec.Command(testenv.GoToolPath(t), "build", noVolume).CombinedOutput()
if err == nil {
t.Fatal("build should fail")
}
"bufio"
"bytes"
"fmt"
- "go/build"
"internal/testenv"
"io/ioutil"
"os"
if err != nil {
t.Fatal(err)
}
- gofolder := filepath.Join(build.Default.GOROOT, "bin")
- if gobin := os.Getenv("GOBIN"); len(gobin) != 0 {
- gofolder = gobin
- }
-
cmd := exec.Command(
- filepath.Join(gofolder, "go"), "tool", "asm", "-S", "-dynlink",
+ testenv.GoToolPath(t), "tool", "asm", "-S", "-dynlink",
"-o", filepath.Join(tmpdir, "output.6"), tmpfile.Name())
var env []string
defer os.RemoveAll(tmpDir)
testnmpath := filepath.Join(tmpDir, "testnm.exe")
- out, err := exec.Command("go", "build", "-o", testnmpath, "cmd/nm").CombinedOutput()
+ out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", testnmpath, "cmd/nm").CombinedOutput()
if err != nil {
t.Fatalf("go build -o %v cmd/nm: %v\n%s", testnmpath, err, string(out))
}
}
exe = filepath.Join(tmp, "testobjdump.exe")
- out, err := exec.Command("go", "build", "-o", exe, "cmd/objdump").CombinedOutput()
+ out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, "cmd/objdump").CombinedOutput()
if err != nil {
os.RemoveAll(tmp)
t.Fatalf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out))
args := []string{"build", "-o", hello}
args = append(args, flags...)
args = append(args, "testdata/fmthello.go")
- out, err := exec.Command("go", args...).CombinedOutput()
+ out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
if err != nil {
t.Fatalf("go build fmthello.go: %v\n%s", err, out)
}
return doRun(t, dir, args...)
}
- run("go", "build", "cmd/pack") // writes pack binary to dir
- run("go", "tool", "compile", "hello.go")
+ goBin := testenv.GoToolPath(t)
+ run(goBin, "build", "cmd/pack") // writes pack binary to dir
+ run(goBin, "tool", "compile", "hello.go")
run("./pack", "grc", "hello.a", "hello.o")
- run("go", "tool", "link", "-o", "a.out", "hello.a")
+ run(goBin, "tool", "link", "-o", "a.out", "hello.a")
out := run("./a.out")
if out != "hello world\n" {
t.Fatalf("incorrect output: %q, want %q", out, "hello world\n")
return doRun(t, dir, args...)
}
- run("go", "build", "cmd/pack") // writes pack binary to dir
- run("go", "tool", "compile", "large.go")
+ goBin := testenv.GoToolPath(t)
+ run(goBin, "build", "cmd/pack") // writes pack binary to dir
+ run(goBin, "tool", "compile", "large.go")
run("./pack", "grc", "large.a", "large.o")
- run("go", "tool", "compile", "-I", ".", "main.go")
- run("go", "tool", "link", "-L", ".", "-o", "a.out", "main.o")
+ run(goBin, "tool", "compile", "-I", ".", "main.go")
+ run(goBin, "tool", "link", "-L", ".", "-o", "a.out", "main.o")
out := run("./a.out")
if out != "ok\n" {
t.Fatalf("incorrect output: %q, want %q", out, "ok\n")
if failed {
t.Skip("cannot run on this environment")
}
- cmd := exec.Command("go", "build", "-o", binary)
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", binary)
output, err := cmd.CombinedOutput()
if err != nil {
failed = true
func TestImports(t *testing.T) {
testenv.MustHaveGoRun(t)
- if err := exec.Command("go", "run", "x509_test_import.go").Run(); err != nil {
+ if err := exec.Command(testenv.GoToolPath(t), "run", "x509_test_import.go").Run(); err != nil {
t.Errorf("failed to run x509_test_import.go: %s", err)
}
}
// the resulting binary looks like it was built from pclinetest.s,
// but we have renamed it to keep it away from the go tool.
pclinetestBinary = filepath.Join(pclineTempDir, "pclinetest")
- cmd := exec.Command("go", "tool", "asm", "-o", pclinetestBinary+".o", "pclinetest.asm")
+ cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", pclinetestBinary+".o", "pclinetest.asm")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
- cmd = exec.Command("go", "tool", "link", "-H", "linux",
+ cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-H", "linux",
"-o", pclinetestBinary, pclinetestBinary+".o")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
src := filepath.Join(tmpdir, "a.go")
exe := filepath.Join(tmpdir, "a.exe")
err = ioutil.WriteFile(src, []byte(prog), 0644)
- output, err := exec.Command("go", "build", "-o", exe, src).CombinedOutput()
+ output, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, src).CombinedOutput()
if err != nil {
t.Fatalf("building test executable failed: %s %s", err, output)
}
}
func compile(t *testing.T, dirname, filename string) string {
- testenv.MustHaveGoBuild(t)
- cmd := exec.Command("go", "tool", "compile", filename)
+ cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", filename)
cmd.Dir = dirname
out, err := cmd.CombinedOutput()
if err != nil {
// server code.
func TestCmdGoNoHTTPServer(t *testing.T) {
goBin := testenv.GoToolPath(t)
- out, err := exec.Command("go", "tool", "nm", goBin).CombinedOutput()
+ out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput()
if err != nil {
t.Fatalf("go tool nm: %v: %s", err, out)
}
import (
"bytes"
+ "internal/testenv"
"io/ioutil"
"os"
"os/exec"
// compile it
exe := name + ".exe"
defer os.Remove(exe)
- o, err := exec.Command("go", "build", "-o", exe, src).CombinedOutput()
+ o, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, src).CombinedOutput()
if err != nil {
t.Fatalf("Failed to compile: %v\n%v", err, string(o))
}
fn := strings.TrimSpace(string(got))
defer os.Remove(fn)
- cmd := testEnv(exec.Command("go", "tool", "pprof", "-top", "-nodecount=1", exe, fn))
+ cmd := testEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-top", "-nodecount=1", exe, fn))
found := false
for i, e := range cmd.Env {
}
exe := filepath.Join(testprog.dir, name+".exe")
- cmd := exec.Command("go", append([]string{"build", "-o", exe}, flags...)...)
+ cmd := exec.Command(testenv.GoToolPath(t), append([]string{"build", "-o", exe}, flags...)...)
cmd.Dir = "testdata/" + binary
out, err := testEnv(cmd).CombinedOutput()
if err != nil {
func checkStaleRuntime(t *testing.T) {
staleRuntimeOnce.Do(func() {
// 'go run' uses the installed copy of runtime.a, which may be out of date.
- out, err := testEnv(exec.Command("go", "list", "-f", "{{.Stale}}", "runtime")).CombinedOutput()
+ out, err := testEnv(exec.Command(testenv.GoToolPath(t), "list", "-f", "{{.Stale}}", "runtime")).CombinedOutput()
if err != nil {
staleRuntimeErr = fmt.Errorf("failed to execute 'go list': %v\n%v", err, string(out))
return
fn := strings.TrimSpace(string(got))
defer os.Remove(fn)
- cmd := testEnv(exec.Command("go", "tool", "pprof", "-alloc_space", "-top", exe, fn))
+ cmd := testEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-alloc_space", "-top", exe, fn))
found := false
for i, e := range cmd.Env {
t.Fatalf("failed to create Go file: %v", err)
}
- cmd := exec.Command("go", "build", "-o", "a.exe")
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe")
cmd.Dir = dir
out, err := testEnv(cmd).CombinedOutput()
if err != nil {
package race_test
import (
+ "internal/testenv"
"io/ioutil"
"os"
"os/exec"
t.Fatalf("failed to close file: %v", err)
}
// Pass -l to the compiler to test stack traces.
- cmd := exec.Command("go", test.run, "-race", "-gcflags=-l", src)
+ cmd := exec.Command(testenv.GoToolPath(t), test.run, "-race", "-gcflags=-l", src)
// GODEBUG spoils program output, GOMAXPROCS makes it flaky.
for _, env := range os.Environ() {
if strings.HasPrefix(env, "GODEBUG=") ||
"bufio"
"bytes"
"fmt"
+ "internal/testenv"
"io"
"log"
"math/rand"
)
func TestRace(t *testing.T) {
- testOutput, err := runTests()
+ testOutput, err := runTests(t)
if err != nil {
t.Fatalf("Failed to run tests: %v\n%v", err, string(testOutput))
}
// runTests assures that the package and its dependencies is
// built with instrumentation enabled and returns the output of 'go test'
// which includes possible data race reports from ThreadSanitizer.
-func runTests() ([]byte, error) {
+func runTests(t *testing.T) ([]byte, error) {
tests, err := filepath.Glob("./testdata/*_test.go")
if err != nil {
return nil, err
}
args := []string{"test", "-race", "-v"}
args = append(args, tests...)
- cmd := exec.Command("go", args...)
+ cmd := exec.Command(testenv.GoToolPath(t), args...)
// The following flags turn off heuristics that suppress seemingly identical reports.
// It is required because the tests contain a lot of data races on the same addresses
// (the tests are simple and the memory is constantly reused).
package race_test
import (
+ "internal/testenv"
"os"
"os/exec"
"testing"
)
func TestNoRaceCgoSync(t *testing.T) {
- cmd := exec.Command("go", "run", "-race", "cgo_test_main.go")
+ cmd := exec.Command(testenv.GoToolPath(t), "run", "-race", "cgo_test_main.go")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatalf("failed to create file: %v", err)
}
- cmd := exec.Command("go", "build", "-o", "a.exe")
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe")
cmd.Dir = dir
out, err := testEnv(cmd).CombinedOutput()
if err != nil {
// This can happen when using all.bash with
// GOROOT_FINAL set, because the tests are run before
// the final installation of the files.
- cmd := exec.Command("go", "env", "GOROOT")
+ cmd := exec.Command(testenv.GoToolPath(t), "env", "GOROOT")
cmd.Env = []string{}
out, err := cmd.CombinedOutput()
if err != nil && bytes.Contains(out, []byte("cannot find GOROOT")) {
if err != nil {
t.Fatalf("failed to create file: %v", err)
}
- cmd := exec.Command("go", "build", "-o", "a.exe")
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe")
cmd.Dir = dir
out, err := testEnv(cmd).CombinedOutput()
if err != nil {
t.Fatalf("failed to create file: %v", err)
}
- cmd := exec.Command("go", "build", "-gcflags", "-N -l", "-o", "a.exe")
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags", "-N -l", "-o", "a.exe")
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("failed to create file: %v", err)
}
- cmd := exec.Command("go", "build", "-o", "a.exe")
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "a.exe")
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {