From 6e269256264599a49739ad1145bac3845e94ead4 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 2 Nov 2016 22:56:10 -0700 Subject: [PATCH] cmd/objdump: speed up tests Rebuild cmd/objdump once instead of twice. Speeds up standalone 'go test cmd/objdump' on my machine from ~1.4s to ~1s. Updates #17751 Change-Id: I15fd79cf18c310f892bc28a9e9ca47ee010c989a Reviewed-on: https://go-review.googlesource.com/32673 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/cmd/objdump/objdump_test.go | 40 ++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/cmd/objdump/objdump_test.go b/src/cmd/objdump/objdump_test.go index a41af9a5c6..4b70862d09 100644 --- a/src/cmd/objdump/objdump_test.go +++ b/src/cmd/objdump/objdump_test.go @@ -6,6 +6,7 @@ package main import ( "flag" + "fmt" "go/build" "internal/testenv" "io/ioutil" @@ -17,21 +18,43 @@ import ( "testing" ) -func buildObjdump(t *testing.T) (tmp, exe string) { - testenv.MustHaveGoBuild(t) +var tmp, exe string // populated by buildObjdump - tmp, err := ioutil.TempDir("", "TestObjDump") +func TestMain(m *testing.M) { + flag.Parse() + if !testenv.HasGoBuild() { + return + } + var exitcode int + if err := buildObjdump(); err == nil { + exitcode = m.Run() + } else { + fmt.Println(err) + exitcode = 1 + } + os.RemoveAll(tmp) + os.Exit(exitcode) +} + +func buildObjdump() error { + var err error + tmp, err = ioutil.TempDir("", "TestObjDump") if err != nil { - t.Fatal("TempDir failed: ", err) + return fmt.Errorf("TempDir failed: %v", err) } exe = filepath.Join(tmp, "testobjdump.exe") - out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, "cmd/objdump").CombinedOutput() + 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) - t.Fatalf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out)) + return fmt.Errorf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out)) } - return + + return nil } var x86Need = []string{ @@ -70,9 +93,6 @@ var target = flag.String("target", "", "test disassembly of `goos/goarch` binary // can handle that one. func testDisasm(t *testing.T, flags ...string) { - tmp, exe := buildObjdump(t) - defer os.RemoveAll(tmp) - goarch := runtime.GOARCH if *target != "" { f := strings.Split(*target, "/")