aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-11-15 10:37:22 -0500
committerGopher Robot <gobot@golang.org>2022-11-15 20:23:17 +0000
commita3d545933fcc8bdd2edad56cc3f8c2aa6140814d (patch)
treeb5fcb233c958a210a9941342ed0af13302d48db0 /src/cmd
parent5e73f6a02928518bd8ea0a7b1112ce5e536d5dd6 (diff)
downloadgo-a3d545933fcc8bdd2edad56cc3f8c2aa6140814d.tar.xz
cmd/objdump: use the test binary as 'objdump' instead of rebuilding it
This not only reduces the latency of the test, but also respects build flags like '-race' and '-cover' passed to the 'go test' command. Change-Id: Icd22062ab75964a74d011c81ea6c99be80bece18 Reviewed-on: https://go-review.googlesource.com/c/go/+/450704 Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/objdump/objdump_test.go74
1 files changed, 34 insertions, 40 deletions
diff --git a/src/cmd/objdump/objdump_test.go b/src/cmd/objdump/objdump_test.go
index b747d0d542..23b299a42b 100644
--- a/src/cmd/objdump/objdump_test.go
+++ b/src/cmd/objdump/objdump_test.go
@@ -16,48 +16,42 @@ import (
"path/filepath"
"runtime"
"strings"
+ "sync"
"testing"
)
-var tmp, exe string // populated by buildObjdump
-
+// TestMain executes the test binary as the objdump command if
+// GO_OBJDUMPTEST_IS_OBJDUMP is set, and runs the test otherwise.
func TestMain(m *testing.M) {
- if !testenv.HasGoBuild() {
- return
+ if os.Getenv("GO_OBJDUMPTEST_IS_OBJDUMP") != "" {
+ main()
+ os.Exit(0)
}
- var exitcode int
- if err := buildObjdump(); err == nil {
- exitcode = m.Run()
- } else {
- fmt.Println(err)
- exitcode = 1
- }
- os.RemoveAll(tmp)
- os.Exit(exitcode)
+ os.Setenv("GO_OBJDUMPTEST_IS_OBJDUMP", "1")
+ os.Exit(m.Run())
}
-func buildObjdump() error {
- var err error
- tmp, err = os.MkdirTemp("", "TestObjDump")
- if err != nil {
- return fmt.Errorf("TempDir failed: %v", err)
- }
+// objdumpPath returns the path to the "objdump" binary to run.
+func objdumpPath(t testing.TB) string {
+ t.Helper()
+ testenv.MustHaveExec(t)
- exe = filepath.Join(tmp, "testobjdump.exe")
- gotool, err := testenv.GoTool()
- if err != nil {
- return err
- }
- out, err := exec.Command(gotool, "build", "-o", exe, "cmd/objdump").CombinedOutput()
- if err != nil {
- os.RemoveAll(tmp)
- return fmt.Errorf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out))
+ objdumpPathOnce.Do(func() {
+ objdumpExePath, objdumpPathErr = os.Executable()
+ })
+ if objdumpPathErr != nil {
+ t.Fatal(objdumpPathErr)
}
-
- return nil
+ return objdumpExePath
}
+var (
+ objdumpPathOnce sync.Once
+ objdumpExePath string
+ objdumpPathErr error
+)
+
var x86Need = []string{ // for both 386 and AMD64
"JMP main.main(SB)",
"CALL main.Println(SB)",
@@ -152,6 +146,7 @@ func testDisasm(t *testing.T, srcfname string, printCode bool, printGnuAsm bool,
}
hash := notsha256.Sum256([]byte(fmt.Sprintf("%v-%v-%v-%v", srcfname, flags, printCode, printGnuAsm)))
+ tmp := t.TempDir()
hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash))
args := []string{"build", "-o", hello}
args = append(args, flags...)
@@ -226,7 +221,7 @@ func testDisasm(t *testing.T, srcfname string, printCode bool, printGnuAsm bool,
if printGnuAsm {
args = append([]string{"-gnu"}, args...)
}
- cmd = exec.Command(exe, args...)
+ cmd = exec.Command(objdumpPath(t), args...)
cmd.Dir = "testdata" // "Bad line" bug #36683 is sensitive to being run in the source directory
out, err = cmd.CombinedOutput()
t.Logf("Running %v", cmd.Args)
@@ -300,6 +295,9 @@ func TestDisasmPIE(t *testing.T) {
func TestDisasmGoobj(t *testing.T) {
mustHaveDisasm(t)
+ testenv.MustHaveGoBuild(t)
+
+ tmp := t.TempDir()
importcfgfile := filepath.Join(tmp, "hello.importcfg")
testenv.WriteImportcfg(t, importcfgfile, nil)
@@ -321,7 +319,7 @@ func TestDisasmGoobj(t *testing.T) {
hello,
}
- out, err = exec.Command(exe, args...).CombinedOutput()
+ out, err = exec.Command(objdumpPath(t), args...).CombinedOutput()
if err != nil {
t.Fatalf("objdump fmthello.o: %v\n%s", err, out)
}
@@ -352,13 +350,9 @@ func TestGoobjFileNumber(t *testing.T) {
t.Parallel()
- tmpdir, err := os.MkdirTemp("", "TestGoobjFileNumber")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpdir)
+ tmp := t.TempDir()
- obj := filepath.Join(tmpdir, "p.a")
+ obj := filepath.Join(tmp, "p.a")
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", obj)
cmd.Dir = filepath.Join("testdata/testfilenum")
out, err := cmd.CombinedOutput()
@@ -366,7 +360,7 @@ func TestGoobjFileNumber(t *testing.T) {
t.Fatalf("build failed: %v\n%s", err, out)
}
- cmd = exec.Command(exe, obj)
+ cmd = exec.Command(objdumpPath(t), obj)
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("objdump failed: %v\n%s", err, out)
@@ -389,7 +383,7 @@ func TestGoObjOtherVersion(t *testing.T) {
t.Parallel()
obj := filepath.Join("testdata", "go116.o")
- cmd := exec.Command(exe, obj)
+ cmd := exec.Command(objdumpPath(t), obj)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("objdump go116.o succeeded unexpectedly")