From 0b82b670ed0ba6826ce8215e816df9c8016df323 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 15 Nov 2022 10:21:21 -0500 Subject: [PATCH] cmd/link: 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: I25a67aa5793c6fa977ff6d4dc59ca1533bf58d41 Reviewed-on: https://go-review.googlesource.com/c/go/+/450702 Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Than McIntosh Auto-Submit: Bryan Mills --- src/cmd/link/cgo_test.go | 2 +- src/cmd/link/dwarf_test.go | 10 +-- src/cmd/link/elf_test.go | 22 ++--- src/cmd/link/internal/ld/deadcode_test.go | 3 +- src/cmd/link/internal/ld/dwarf_test.go | 7 +- src/cmd/link/internal/ld/elf_test.go | 5 +- src/cmd/link/internal/ld/go_test.go | 5 +- src/cmd/link/internal/ld/ld_test.go | 15 ++-- src/cmd/link/internal/ld/nooptcgolink_test.go | 3 +- src/cmd/link/internal/ld/stackcheck_test.go | 3 +- src/cmd/link/link_test.go | 87 +++++++++---------- src/cmd/link/linkbig_test.go | 9 +- 12 files changed, 81 insertions(+), 90 deletions(-) diff --git a/src/cmd/link/cgo_test.go b/src/cmd/link/cgo_test.go index 26ab8024541..4393c3fa801 100644 --- a/src/cmd/link/cgo_test.go +++ b/src/cmd/link/cgo_test.go @@ -106,7 +106,7 @@ func testCGOLTO(t *testing.T, cc string, test int) { t.Fatalf("bad case %d", test) } - cmd := exec.Command(testenv.GoToolPath(t), "build") + cmd := testenv.Command(t, testenv.GoToolPath(t), "build") cmd.Dir = dir cmd.Env = append(os.Environ(), "CC="+cc, diff --git a/src/cmd/link/dwarf_test.go b/src/cmd/link/dwarf_test.go index 78ef3cfe971..53dc686a4b5 100644 --- a/src/cmd/link/dwarf_test.go +++ b/src/cmd/link/dwarf_test.go @@ -85,7 +85,7 @@ func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string) exe := filepath.Join(tmpDir, prog+".exe") dir := "../../runtime/testdata/" + prog - cmd := exec.Command(testenv.GoToolPath(t), "build", "-toolexec", os.Args[0], "-o", exe) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-toolexec", os.Args[0], "-o", exe) if buildmode != "" { cmd.Args = append(cmd.Args, "-buildmode", buildmode) } @@ -100,7 +100,7 @@ func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string) if buildmode == "c-archive" { // Extract the archive and use the go.o object within. - cmd := exec.Command("ar", "-x", exe) + cmd := testenv.Command(t, "ar", "-x", exe) cmd.Dir = tmpDir if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("ar -x %s: %v\n%s", exe, err, out) @@ -112,7 +112,7 @@ func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string) if runtime.GOOS == "darwin" && !darwinSymbolTestIsTooFlaky { if _, err = exec.LookPath("symbols"); err == nil { // Ensure Apple's tooling can parse our object for symbols. - out, err = exec.Command("symbols", exe).CombinedOutput() + out, err = testenv.Command(t, "symbols", exe).CombinedOutput() if err != nil { t.Fatalf("symbols %v: %v: %s", filepath.Base(exe), err, out) } else { @@ -208,12 +208,12 @@ func TestDWARFiOS(t *testing.T) { if runtime.GOARCH != "amd64" || runtime.GOOS != "darwin" { t.Skip("skipping on non-darwin/amd64 platform") } - if err := exec.Command("xcrun", "--help").Run(); err != nil { + if err := testenv.Command(t, "xcrun", "--help").Run(); err != nil { t.Skipf("error running xcrun, required for iOS cross build: %v", err) } // Check to see if the ios tools are installed. It's possible to have the command line tools // installed without the iOS sdk. - if output, err := exec.Command("xcodebuild", "-showsdks").CombinedOutput(); err != nil { + if output, err := testenv.Command(t, "xcodebuild", "-showsdks").CombinedOutput(); err != nil { t.Skipf("error running xcodebuild, required for iOS cross build: %v", err) } else if !strings.Contains(string(output), "iOS SDK") { t.Skipf("iOS SDK not detected.") diff --git a/src/cmd/link/elf_test.go b/src/cmd/link/elf_test.go index 8f7af2598c9..27285ff566e 100644 --- a/src/cmd/link/elf_test.go +++ b/src/cmd/link/elf_test.go @@ -23,7 +23,7 @@ import ( func getCCAndCCFLAGS(t *testing.T, env []string) (string, []string) { goTool := testenv.GoToolPath(t) - cmd := exec.Command(goTool, "env", "CC") + cmd := testenv.Command(t, goTool, "env", "CC") cmd.Env = env ccb, err := cmd.Output() if err != nil { @@ -31,7 +31,7 @@ func getCCAndCCFLAGS(t *testing.T, env []string) (string, []string) { } cc := strings.TrimSpace(string(ccb)) - cmd = exec.Command(goTool, "env", "GOGCCFLAGS") + cmd = testenv.Command(t, goTool, "env", "GOGCCFLAGS") cmd.Env = env cflagsb, err := cmd.Output() if err != nil { @@ -87,14 +87,14 @@ func TestSectionsWithSameName(t *testing.T) { asmObj := filepath.Join(dir, "x.o") t.Logf("%s %v -c -o %s %s", cc, cflags, asmObj, asmFile) - if out, err := exec.Command(cc, append(cflags, "-c", "-o", asmObj, asmFile)...).CombinedOutput(); err != nil { + if out, err := testenv.Command(t, cc, append(cflags, "-c", "-o", asmObj, asmFile)...).CombinedOutput(); err != nil { t.Logf("%s", out) t.Fatal(err) } asm2Obj := filepath.Join(dir, "x2.syso") t.Logf("%s --rename-section .text2=.text1 %s %s", objcopy, asmObj, asm2Obj) - if out, err := exec.Command(objcopy, "--rename-section", ".text2=.text1", asmObj, asm2Obj).CombinedOutput(); err != nil { + if out, err := testenv.Command(t, objcopy, "--rename-section", ".text2=.text1", asmObj, asm2Obj).CombinedOutput(); err != nil { t.Logf("%s", out) t.Fatal(err) } @@ -110,7 +110,7 @@ func TestSectionsWithSameName(t *testing.T) { t.Fatal(err) } - cmd := exec.Command(goTool, "build") + cmd := testenv.Command(t, goTool, "build") cmd.Dir = dir cmd.Env = env t.Logf("%s build", goTool) @@ -162,7 +162,7 @@ func TestMinusRSymsWithSameName(t *testing.T) { obj := filepath.Join(dir, fmt.Sprintf("x%d.o", i)) objs = append(objs, obj) t.Logf("%s %v -c -o %s %s", cc, cflags, obj, csrcFile) - if out, err := exec.Command(cc, append(cflags, "-c", "-o", obj, csrcFile)...).CombinedOutput(); err != nil { + if out, err := testenv.Command(t, cc, append(cflags, "-c", "-o", obj, csrcFile)...).CombinedOutput(); err != nil { t.Logf("%s", out) t.Fatal(err) } @@ -170,7 +170,7 @@ func TestMinusRSymsWithSameName(t *testing.T) { sysoObj := filepath.Join(dir, "ldr.syso") t.Logf("%s %v -nostdlib -r -o %s %v", cc, cflags, sysoObj, objs) - if out, err := exec.Command(cc, append(cflags, "-nostdlib", "-r", "-o", sysoObj, objs[0], objs[1])...).CombinedOutput(); err != nil { + if out, err := testenv.Command(t, cc, append(cflags, "-nostdlib", "-r", "-o", sysoObj, objs[0], objs[1])...).CombinedOutput(); err != nil { t.Logf("%s", out) t.Fatal(err) } @@ -190,7 +190,7 @@ func TestMinusRSymsWithSameName(t *testing.T) { } t.Logf("%s build", goTool) - cmd := exec.Command(goTool, "build") + cmd := testenv.Command(t, goTool, "build") cmd.Dir = dir cmd.Env = env if out, err := cmd.CombinedOutput(); err != nil { @@ -221,7 +221,7 @@ func TestMergeNoteSections(t *testing.T) { goTool := testenv.GoToolPath(t) // sha1sum of "gopher" id := "0xf4e8cd51ce8bae2996dc3b74639cdeaa1f7fee5f" - cmd := exec.Command(goTool, "build", "-o", outFile, "-ldflags", + cmd := testenv.Command(t, goTool, "build", "-o", outFile, "-ldflags", "-B "+id, goFile) cmd.Dir = t.TempDir() if out, err := cmd.CombinedOutput(); err != nil { @@ -328,7 +328,7 @@ func TestPIESize(t *testing.T) { } build := func(bin, mode string) error { - cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", bin, "-buildmode="+mode) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", bin, "-buildmode="+mode) if external { cmd.Args = append(cmd.Args, "-ldflags=-linkmode=external") } @@ -478,7 +478,7 @@ func TestIssue51939(t *testing.T) { } outFile := filepath.Join(td, "issue51939.exe") goTool := testenv.GoToolPath(t) - cmd := exec.Command(goTool, "build", "-o", outFile, goFile) + cmd := testenv.Command(t, goTool, "build", "-o", outFile, goFile) if out, err := cmd.CombinedOutput(); err != nil { t.Logf("%s", out) t.Fatal(err) diff --git a/src/cmd/link/internal/ld/deadcode_test.go b/src/cmd/link/internal/ld/deadcode_test.go index ed37a088128..573bff3c850 100644 --- a/src/cmd/link/internal/ld/deadcode_test.go +++ b/src/cmd/link/internal/ld/deadcode_test.go @@ -7,7 +7,6 @@ package ld import ( "bytes" "internal/testenv" - "os/exec" "path/filepath" "testing" ) @@ -35,7 +34,7 @@ func TestDeadcode(t *testing.T) { t.Parallel() src := filepath.Join("testdata", "deadcode", test.src+".go") exe := filepath.Join(tmpdir, test.src+".exe") - cmd := exec.Command(testenv.GoToolPath(t), "build", "-ldflags=-dumpdep", "-o", exe, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-dumpdep", "-o", exe, src) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) diff --git a/src/cmd/link/internal/ld/dwarf_test.go b/src/cmd/link/internal/ld/dwarf_test.go index 3132e1233cd..a3db4a99ffb 100644 --- a/src/cmd/link/internal/ld/dwarf_test.go +++ b/src/cmd/link/internal/ld/dwarf_test.go @@ -11,7 +11,6 @@ import ( "internal/testenv" "io" "os" - "os/exec" "path/filepath" "reflect" "runtime" @@ -100,7 +99,7 @@ func gobuild(t *testing.T, dir string, testfile string, gcflags string) *builtFi t.Fatal(err) } - cmd := exec.Command(testenv.GoToolPath(t), "build", gcflags, "-o", dst, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", gcflags, "-o", dst, src) b, err := cmd.CombinedOutput() if len(b) != 0 { t.Logf("## build output:\n%s", b) @@ -122,7 +121,7 @@ func gobuildTestdata(t *testing.T, tdir string, pkgDir string, gcflags string) * dst := filepath.Join(tdir, "out.exe") // Run a build with an updated GOPATH - cmd := exec.Command(testenv.GoToolPath(t), "build", gcflags, "-o", dst) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", gcflags, "-o", dst) cmd.Dir = pkgDir if b, err := cmd.CombinedOutput(); err != nil { t.Logf("build: %s\n", b) @@ -768,7 +767,7 @@ func main() { f := gobuild(t, dir, prog, flags) defer f.Close() - out, err := exec.Command(f.path).CombinedOutput() + out, err := testenv.Command(t, f.path).CombinedOutput() if err != nil { t.Fatalf("could not run test program: %v", err) } diff --git a/src/cmd/link/internal/ld/elf_test.go b/src/cmd/link/internal/ld/elf_test.go index 9f5a0ed9e2e..8af0ca144eb 100644 --- a/src/cmd/link/internal/ld/elf_test.go +++ b/src/cmd/link/internal/ld/elf_test.go @@ -11,7 +11,6 @@ import ( "debug/elf" "internal/testenv" "os" - "os/exec" "path/filepath" "runtime" "strings" @@ -38,7 +37,7 @@ func main() { } binFile := filepath.Join(dir, "issue33358") - cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", binFile, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", binFile, src) if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) } @@ -101,7 +100,7 @@ func TestNoDuplicateNeededEntries(t *testing.T) { path := filepath.Join(dir, "x") argv := []string{"build", "-o", path, filepath.Join(wd, "testdata", "issue39256")} - out, err := exec.Command(testenv.GoToolPath(t), argv...).CombinedOutput() + out, err := testenv.Command(t, testenv.GoToolPath(t), argv...).CombinedOutput() if err != nil { t.Fatalf("Build failure: %s\n%s\n", err, string(out)) } diff --git a/src/cmd/link/internal/ld/go_test.go b/src/cmd/link/internal/ld/go_test.go index 7dc9b571128..836731a891e 100644 --- a/src/cmd/link/internal/ld/go_test.go +++ b/src/cmd/link/internal/ld/go_test.go @@ -7,7 +7,6 @@ package ld import ( "internal/testenv" "os" - "os/exec" "path/filepath" "reflect" "runtime" @@ -105,13 +104,13 @@ func main() {}` } exe := filepath.Join(dir, "deduped.exe") - out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, srcFile).CombinedOutput() + out, err := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe, srcFile).CombinedOutput() if err != nil { t.Fatalf("build failure: %s\n%s\n", err, string(out)) } // Result should be runnable. - if _, err = exec.Command(exe).CombinedOutput(); err != nil { + if _, err = testenv.Command(t, exe).CombinedOutput(); err != nil { t.Fatal(err) } } diff --git a/src/cmd/link/internal/ld/ld_test.go b/src/cmd/link/internal/ld/ld_test.go index e1149d3e563..314dab7d7da 100644 --- a/src/cmd/link/internal/ld/ld_test.go +++ b/src/cmd/link/internal/ld/ld_test.go @@ -10,7 +10,6 @@ import ( "fmt" "internal/testenv" "os" - "os/exec" "path/filepath" "runtime" "strings" @@ -26,7 +25,7 @@ func TestUndefinedRelocErrors(t *testing.T) { t.Parallel() - out, err := exec.Command(testenv.GoToolPath(t), "build", "./testdata/issue10978").CombinedOutput() + out, err := testenv.Command(t, testenv.GoToolPath(t), "build", "./testdata/issue10978").CombinedOutput() if err == nil { t.Fatal("expected build to fail") } @@ -113,7 +112,7 @@ func TestArchiveBuildInvokeWithExec(t *testing.T) { ldf := fmt.Sprintf("-ldflags=-v -tmpdir=%s", dir) argv := []string{"build", "-buildmode=c-archive", "-o", arfile, ldf, srcfile} - out, err := exec.Command(testenv.GoToolPath(t), argv...).CombinedOutput() + out, err := testenv.Command(t, testenv.GoToolPath(t), argv...).CombinedOutput() if err != nil { t.Fatalf("build failure: %s\n%s\n", err, string(out)) } @@ -155,13 +154,13 @@ func TestLargeTextSectionSplitting(t *testing.T) { // is arbitrary; we just need something sufficiently large that uses // external linking. exe := filepath.Join(dir, "go.exe") - out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, "-ldflags=-linkmode=external -debugtextsize=1048576", "cmd/go").CombinedOutput() + out, err := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe, "-ldflags=-linkmode=external -debugtextsize=1048576", "cmd/go").CombinedOutput() if err != nil { t.Fatalf("build failure: %s\n%s\n", err, string(out)) } // Check that we did split text sections. - out, err = exec.Command(testenv.GoToolPath(t), "tool", "nm", exe).CombinedOutput() + out, err = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", exe).CombinedOutput() if err != nil { t.Fatalf("nm failure: %s\n%s\n", err, string(out)) } @@ -170,7 +169,7 @@ func TestLargeTextSectionSplitting(t *testing.T) { } // Result should be runnable. - _, err = exec.Command(exe, "version").CombinedOutput() + _, err = testenv.Command(t, exe, "version").CombinedOutput() if err != nil { t.Fatal(err) } @@ -210,7 +209,7 @@ func testWindowsBuildmodeCSharedASLR(t *testing.T, useASLR bool) { argv = append(argv, "-ldflags", "-aslr=false") } argv = append(argv, "-o", objfile, srcfile) - out, err := exec.Command(testenv.GoToolPath(t), argv...).CombinedOutput() + out, err := testenv.Command(t, testenv.GoToolPath(t), argv...).CombinedOutput() if err != nil { t.Fatalf("build failure: %s\n%s\n", err, string(out)) } @@ -330,7 +329,7 @@ func main() { if err := os.WriteFile(src, []byte(tt.prog), 0644); err != nil { t.Fatal(err) } - cmd := exec.Command(testenv.GoToolPath(t), "run", src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "run", src) out, err := cmd.CombinedOutput() if err != nil { t.Fatal(err) diff --git a/src/cmd/link/internal/ld/nooptcgolink_test.go b/src/cmd/link/internal/ld/nooptcgolink_test.go index 0b76ecaecb8..646583f13ac 100644 --- a/src/cmd/link/internal/ld/nooptcgolink_test.go +++ b/src/cmd/link/internal/ld/nooptcgolink_test.go @@ -6,7 +6,6 @@ package ld import ( "internal/testenv" - "os/exec" "path/filepath" "testing" ) @@ -20,7 +19,7 @@ func TestNooptCgoBuild(t *testing.T) { testenv.MustHaveGoBuild(t) testenv.MustHaveCGO(t) dir := t.TempDir() - cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags=-N -l", "-o", filepath.Join(dir, "a.out")) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-gcflags=-N -l", "-o", filepath.Join(dir, "a.out")) cmd.Dir = filepath.Join(testenv.GOROOT(t), "src", "runtime", "testdata", "testprogcgo") out, err := cmd.CombinedOutput() if err != nil { diff --git a/src/cmd/link/internal/ld/stackcheck_test.go b/src/cmd/link/internal/ld/stackcheck_test.go index d9354017401..dd7e20528f0 100644 --- a/src/cmd/link/internal/ld/stackcheck_test.go +++ b/src/cmd/link/internal/ld/stackcheck_test.go @@ -8,7 +8,6 @@ import ( "fmt" "internal/testenv" "os" - "os/exec" "regexp" "strconv" "testing" @@ -20,7 +19,7 @@ func TestStackCheckOutput(t *testing.T) { testenv.MustHaveGoBuild(t) t.Parallel() - cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", os.DevNull, "./testdata/stackcheck") + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", os.DevNull, "./testdata/stackcheck") // The rules for computing frame sizes on all of the // architectures are complicated, so just do this on amd64. cmd.Env = append(os.Environ(), "GOARCH=amd64", "GOOS=linux") diff --git a/src/cmd/link/link_test.go b/src/cmd/link/link_test.go index 406487c7eee..a770c919365 100644 --- a/src/cmd/link/link_test.go +++ b/src/cmd/link/link_test.go @@ -12,7 +12,6 @@ import ( "internal/platform" "internal/testenv" "os" - "os/exec" "path/filepath" "regexp" "runtime" @@ -58,14 +57,14 @@ func main() {} t.Fatalf("failed to write main.go: %v\n", err) } - cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=main", "main.go") + cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=main", "main.go") cmd.Dir = tmpdir out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("failed to compile main.go: %v, output: %s\n", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgfile, "main.o") + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgfile, "main.o") cmd.Dir = tmpdir out, err = cmd.CombinedOutput() if err != nil { @@ -92,7 +91,7 @@ func TestIssue28429(t *testing.T) { } runGo := func(args ...string) { - cmd := exec.Command(testenv.GoToolPath(t), args...) + cmd := testenv.Command(t, testenv.GoToolPath(t), args...) cmd.Dir = tmpdir out, err := cmd.CombinedOutput() if err != nil { @@ -154,7 +153,7 @@ TEXT ·x(SB),0,$0 MOVD ·zero(SB), AX RET `) - cmd := exec.Command(testenv.GoToolPath(t), "build") + cmd := testenv.Command(t, testenv.GoToolPath(t), "build") cmd.Dir = tmpdir cmd.Env = append(os.Environ(), "GOARCH=amd64", "GOOS=linux", "GOPATH="+filepath.Join(tmpdir, "_gopath")) @@ -202,7 +201,7 @@ func TestIssue33979(t *testing.T) { } run := func(name string, args ...string) string { - cmd := exec.Command(name, args...) + cmd := testenv.Command(t, name, args...) cmd.Dir = tmpdir out, err := cmd.CombinedOutput() if err != nil { @@ -253,7 +252,7 @@ void foo() { runGo("tool", "pack", "c", "x.a", "x1.o", "x2.o", "x3.o") // Now attempt to link using the internal linker. - cmd := exec.Command(testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgfile, "-linkmode=internal", "x.a") + cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgfile, "-linkmode=internal", "x.a") cmd.Dir = tmpdir out, err := cmd.CombinedOutput() if err == nil { @@ -276,13 +275,13 @@ func TestBuildForTvOS(t *testing.T) { if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" { t.Skip("skipping in -short mode with $GO_BUILDER_NAME empty") } - if err := exec.Command("xcrun", "--help").Run(); err != nil { + if err := testenv.Command(t, "xcrun", "--help").Run(); err != nil { t.Skipf("error running xcrun, required for iOS cross build: %v", err) } t.Parallel() - sdkPath, err := exec.Command("xcrun", "--sdk", "appletvos", "--show-sdk-path").Output() + sdkPath, err := testenv.Command(t, "xcrun", "--sdk", "appletvos", "--show-sdk-path").Output() if err != nil { t.Skip("failed to locate appletvos SDK, skipping") } @@ -299,7 +298,7 @@ func TestBuildForTvOS(t *testing.T) { tmpDir := t.TempDir() ar := filepath.Join(tmpDir, "lib.a") - cmd := exec.Command(testenv.GoToolPath(t), "build", "-buildmode=c-archive", "-o", ar, lib) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-buildmode=c-archive", "-o", ar, lib) cmd.Env = append(os.Environ(), "CGO_ENABLED=1", "GOOS=ios", @@ -312,7 +311,7 @@ func TestBuildForTvOS(t *testing.T) { t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) } - link := exec.Command(CC[0], CC[1:]...) + link := testenv.Command(t, CC[0], CC[1:]...) link.Args = append(link.Args, CGO_LDFLAGS...) link.Args = append(link.Args, "-o", filepath.Join(tmpDir, "a.out")) // Avoid writing to package directory. link.Args = append(link.Args, ar, filepath.Join("testdata", "testBuildFortvOS", "main.m")) @@ -341,7 +340,7 @@ func TestXFlag(t *testing.T) { t.Fatal(err) } - cmd := exec.Command(testenv.GoToolPath(t), "build", "-ldflags=-X=main.X=meow", "-o", filepath.Join(tmpdir, "main"), src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-X=main.X=meow", "-o", filepath.Join(tmpdir, "main"), src) if out, err := cmd.CombinedOutput(); err != nil { t.Errorf("%v: %v:\n%s", cmd.Args, err, out) } @@ -366,7 +365,7 @@ func TestMachOBuildVersion(t *testing.T) { } exe := filepath.Join(tmpdir, "main") - cmd := exec.Command(testenv.GoToolPath(t), "build", "-ldflags=-linkmode=internal", "-o", exe, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-linkmode=internal", "-o", exe, src) cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=darwin", @@ -442,14 +441,14 @@ func TestIssue34788Android386TLSSequence(t *testing.T) { } obj := filepath.Join(tmpdir, "blah.o") - cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=blah", "-o", obj, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=blah", "-o", obj, src) cmd.Env = append(os.Environ(), "GOARCH=386", "GOOS=android") if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("failed to compile blah.go: %v, output: %s\n", err, out) } // Run objdump on the resulting object. - cmd = exec.Command(testenv.GoToolPath(t), "tool", "objdump", obj) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "objdump", obj) out, oerr := cmd.CombinedOutput() if oerr != nil { t.Fatalf("failed to objdump blah.o: %v, output: %s\n", oerr, out) @@ -529,7 +528,7 @@ func TestStrictDup(t *testing.T) { t.Fatal(err) } - cmd := exec.Command(testenv.GoToolPath(t), "build", "-ldflags=-strictdups=1") + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-strictdups=1") cmd.Dir = tmpdir out, err := cmd.CombinedOutput() if err != nil { @@ -539,7 +538,7 @@ func TestStrictDup(t *testing.T) { t.Errorf("unexpected output:\n%s", out) } - cmd = exec.Command(testenv.GoToolPath(t), "build", "-ldflags=-strictdups=2") + cmd = testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-strictdups=2") cmd.Dir = tmpdir out, err = cmd.CombinedOutput() if err == nil { @@ -613,13 +612,13 @@ func TestFuncAlign(t *testing.T) { } // Build and run with old object file format. - cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "falign") + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "falign") cmd.Dir = tmpdir out, err := cmd.CombinedOutput() if err != nil { t.Errorf("build failed: %v", err) } - cmd = exec.Command(tmpdir + "/falign") + cmd = testenv.Command(t, tmpdir+"/falign") out, err = cmd.CombinedOutput() if err != nil { t.Errorf("failed to run with err %v, output: %s", err, out) @@ -677,12 +676,12 @@ func TestTrampoline(t *testing.T) { exe := filepath.Join(tmpdir, "hello.exe") for _, mode := range buildmodes { - cmd := exec.Command(testenv.GoToolPath(t), "build", "-buildmode="+mode, "-ldflags=-debugtramp=2", "-o", exe, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-buildmode="+mode, "-ldflags=-debugtramp=2", "-o", exe, src) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("build (%s) failed: %v\n%s", mode, err, out) } - cmd = exec.Command(exe) + cmd = testenv.Command(t, exe) out, err = cmd.CombinedOutput() if err != nil { t.Errorf("executable failed to run (%s): %v\n%s", mode, err, out) @@ -735,12 +734,12 @@ func TestTrampolineCgo(t *testing.T) { exe := filepath.Join(tmpdir, "hello.exe") for _, mode := range buildmodes { - cmd := exec.Command(testenv.GoToolPath(t), "build", "-buildmode="+mode, "-ldflags=-debugtramp=2", "-o", exe, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-buildmode="+mode, "-ldflags=-debugtramp=2", "-o", exe, src) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("build (%s) failed: %v\n%s", mode, err, out) } - cmd = exec.Command(exe) + cmd = testenv.Command(t, exe) out, err = cmd.CombinedOutput() if err != nil { t.Errorf("executable failed to run (%s): %v\n%s", mode, err, out) @@ -754,12 +753,12 @@ func TestTrampolineCgo(t *testing.T) { if runtime.GOARCH == "ppc64" || (runtime.GOARCH == "arm64" && runtime.GOOS == "windows") || !testenv.CanInternalLink() { return // internal linking cgo is not supported } - cmd = exec.Command(testenv.GoToolPath(t), "build", "-buildmode="+mode, "-ldflags=-debugtramp=2 -linkmode=internal", "-o", exe, src) + cmd = testenv.Command(t, testenv.GoToolPath(t), "build", "-buildmode="+mode, "-ldflags=-debugtramp=2 -linkmode=internal", "-o", exe, src) out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("build (%s) failed: %v\n%s", mode, err, out) } - cmd = exec.Command(exe) + cmd = testenv.Command(t, exe) out, err = cmd.CombinedOutput() if err != nil { t.Errorf("executable failed to run (%s): %v\n%s", mode, err, out) @@ -793,19 +792,19 @@ func TestIndexMismatch(t *testing.T) { testenv.WriteImportcfg(t, importcfgWithAFile, map[string]string{"a": aObj}) // Build a program with main package importing package a. - cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgFile, "-p=a", "-o", aObj, aSrc) + cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgFile, "-p=a", "-o", aObj, aSrc) t.Log(cmd) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("compiling a.go failed: %v\n%s", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgWithAFile, "-p=main", "-I", tmpdir, "-o", mObj, mSrc) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgWithAFile, "-p=main", "-I", tmpdir, "-o", mObj, mSrc) t.Log(cmd) out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("compiling main.go failed: %v\n%s", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgWithAFile, "-L", tmpdir, "-o", exe, mObj) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgWithAFile, "-L", tmpdir, "-o", exe, mObj) t.Log(cmd) out, err = cmd.CombinedOutput() if err != nil { @@ -814,13 +813,13 @@ func TestIndexMismatch(t *testing.T) { // Now, overwrite a.o with the object of b.go. This should // result in an index mismatch. - cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgFile, "-p=a", "-o", aObj, bSrc) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgFile, "-p=a", "-o", aObj, bSrc) t.Log(cmd) out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("compiling a.go failed: %v\n%s", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgWithAFile, "-L", tmpdir, "-o", exe, mObj) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgWithAFile, "-L", tmpdir, "-o", exe, mObj) t.Log(cmd) out, err = cmd.CombinedOutput() if err == nil { @@ -846,7 +845,7 @@ func TestPErsrcBinutils(t *testing.T) { pkgdir := filepath.Join("testdata", "pe-binutils") exe := filepath.Join(tmpdir, "a.exe") - cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", exe) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe) cmd.Dir = pkgdir // cmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64") // uncomment if debugging in a cross-compiling environment out, err := cmd.CombinedOutput() @@ -878,7 +877,7 @@ func TestPErsrcLLVM(t *testing.T) { pkgdir := filepath.Join("testdata", "pe-llvm") exe := filepath.Join(tmpdir, "a.exe") - cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", exe) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe) cmd.Dir = pkgdir // cmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64") // uncomment if debugging in a cross-compiling environment out, err := cmd.CombinedOutput() @@ -903,7 +902,7 @@ func TestContentAddressableSymbols(t *testing.T) { t.Parallel() src := filepath.Join("testdata", "testHashedSyms", "p.go") - cmd := exec.Command(testenv.GoToolPath(t), "run", src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "run", src) out, err := cmd.CombinedOutput() if err != nil { t.Errorf("command %s failed: %v\n%s", cmd, err, out) @@ -917,7 +916,7 @@ func TestReadOnly(t *testing.T) { t.Parallel() src := filepath.Join("testdata", "testRO", "x.go") - cmd := exec.Command(testenv.GoToolPath(t), "run", src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "run", src) out, err := cmd.CombinedOutput() if err == nil { t.Errorf("running test program did not fail. output:\n%s", out) @@ -953,7 +952,7 @@ func TestIssue38554(t *testing.T) { t.Fatalf("failed to write source file: %v", err) } exe := filepath.Join(tmpdir, "x.exe") - cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe, src) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("build failed: %v\n%s", err, out) @@ -1003,7 +1002,7 @@ func TestIssue42396(t *testing.T) { t.Fatalf("failed to write source file: %v", err) } exe := filepath.Join(tmpdir, "main.exe") - cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags=-race", "-o", exe, src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-gcflags=-race", "-o", exe, src) out, err := cmd.CombinedOutput() if err == nil { t.Fatalf("build unexpectedly succeeded") @@ -1073,14 +1072,14 @@ func TestLargeReloc(t *testing.T) { if err != nil { t.Fatalf("failed to write source file: %v", err) } - cmd := exec.Command(testenv.GoToolPath(t), "run", src) + cmd := testenv.Command(t, testenv.GoToolPath(t), "run", src) out, err := cmd.CombinedOutput() if err != nil { t.Errorf("build failed: %v. output:\n%s", err, out) } if testenv.HasCGO() { // currently all targets that support cgo can external link - cmd = exec.Command(testenv.GoToolPath(t), "run", "-ldflags=-linkmode=external", src) + cmd = testenv.Command(t, testenv.GoToolPath(t), "run", "-ldflags=-linkmode=external", src) out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("build failed: %v. output:\n%s", err, out) @@ -1114,17 +1113,17 @@ func TestUnlinkableObj(t *testing.T) { if err != nil { t.Fatalf("failed to write source file: %v", err) } - cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-o", pObj, pSrc) // without -p + cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-o", pObj, pSrc) // without -p out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("compile p.go failed: %v. output:\n%s", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=main", "-o", xObj, xSrc) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=main", "-o", xObj, xSrc) out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("compile x.go failed: %v. output:\n%s", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgfile, "-o", exe, xObj) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgfile, "-o", exe, xObj) out, err = cmd.CombinedOutput() if err == nil { t.Fatalf("link did not fail") @@ -1134,18 +1133,18 @@ func TestUnlinkableObj(t *testing.T) { } // It is okay to omit -p for (only) main package. - cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", pObj, pSrc) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", pObj, pSrc) out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("compile p.go failed: %v. output:\n%s", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-o", xObj, xSrc) // without -p + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-o", xObj, xSrc) // without -p out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("compile failed: %v. output:\n%s", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgfile, "-o", exe, xObj) + cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "link", "-importcfg="+importcfgfile, "-o", exe, xObj) out, err = cmd.CombinedOutput() if err != nil { t.Errorf("link failed: %v. output:\n%s", err, out) diff --git a/src/cmd/link/linkbig_test.go b/src/cmd/link/linkbig_test.go index 796adfd422d..45cb1b3ab67 100644 --- a/src/cmd/link/linkbig_test.go +++ b/src/cmd/link/linkbig_test.go @@ -14,7 +14,6 @@ import ( "internal/buildcfg" "internal/testenv" "os" - "os/exec" "testing" ) @@ -83,13 +82,13 @@ func TestLargeText(t *testing.T) { } // Build and run with internal linking. - cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext") + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "bigtext") cmd.Dir = tmpdir out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("Build failed for big text program with internal linking: %v, output: %s", err, out) } - cmd = exec.Command("./bigtext") + cmd = testenv.Command(t, "./bigtext") cmd.Dir = tmpdir out, err = cmd.CombinedOutput() if err != nil { @@ -97,13 +96,13 @@ func TestLargeText(t *testing.T) { } // Build and run with external linking - cmd = exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext", "-ldflags", "-linkmode=external") + cmd = testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "bigtext", "-ldflags", "-linkmode=external") cmd.Dir = tmpdir out, err = cmd.CombinedOutput() if err != nil { t.Fatalf("Build failed for big text program with external linking: %v, output: %s", err, out) } - cmd = exec.Command("./bigtext") + cmd = testenv.Command(t, "./bigtext") cmd.Dir = tmpdir out, err = cmd.CombinedOutput() if err != nil {