From: Ian Lance Taylor Date: Mon, 20 Jul 2015 01:14:53 +0000 (-0700) Subject: cmd/go: rewrite TestNoteReading to use test harness X-Git-Tag: go1.5beta3~149 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f7e7719f653b91cf84ed80c03dd4a17e0558b5eb;p=gostls13.git cmd/go: rewrite TestNoteReading to use test harness On my laptop reduces time required for test from 22 seconds to 0.14 seconds. Update #11779. Change-Id: I715d85bd9c6f7683c6915eedd2539813aa5efc58 Reviewed-on: https://go-review.googlesource.com/12363 Reviewed-by: David Crawshaw Reviewed-by: Andrew Gerrand --- diff --git a/src/cmd/go/note_test.go b/src/cmd/go/note_test.go index 0170108672..fb25f94ec3 100644 --- a/src/cmd/go/note_test.go +++ b/src/cmd/go/note_test.go @@ -2,42 +2,23 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package main +package main_test import ( - "internal/testenv" - "io/ioutil" - "os" - "os/exec" + "cmd/go" "testing" ) func TestNoteReading(t *testing.T) { - testenv.MustHaveGoBuild(t) - - // TODO: Replace with new test scaffolding by iant. - d, err := ioutil.TempDir("", "go-test-") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(d) - - out, err := exec.Command("go", "build", "-o", d+"/go.exe", "cmd/go").CombinedOutput() - if err != nil { - t.Fatalf("go build cmd/go: %v\n%s", err, out) - } - + tg := testgo(t) + defer tg.cleanup() + tg.tempFile("hello.go", `package main; func main() { print("hello, world\n") }`) const buildID = "TestNoteReading-Build-ID" - out, err = exec.Command(d+"/go.exe", "build", "-ldflags", "-buildid="+buildID, "-o", d+"/hello.exe", "../../../test/helloworld.go").CombinedOutput() - if err != nil { - t.Fatalf("go build hello: %v\n%s", err, out) - } - - id, err := readBuildIDFromBinary(d + "/hello.exe") + tg.run("build", "-ldflags", "-buildid="+buildID, "-o", tg.path("hello.exe"), tg.path("hello.go")) + id, err := main.ReadBuildIDFromBinary(tg.path("hello.exe")) if err != nil { t.Fatalf("reading build ID from hello binary: %v", err) } - if id != buildID { t.Fatalf("buildID in hello binary = %q, want %q", id, buildID) } diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go index 03858d9b4c..ae9744218d 100644 --- a/src/cmd/go/pkg.go +++ b/src/cmd/go/pkg.go @@ -1637,7 +1637,7 @@ func readBuildID(p *Package) (id string, err error) { // For commands, read build ID directly from binary. if p.Name == "main" { - return readBuildIDFromBinary(p.Target) + return ReadBuildIDFromBinary(p.Target) } // Otherwise, we expect to have an archive (.a) file, @@ -1715,7 +1715,7 @@ var ( elfPrefix = []byte("\x7fELF") ) -// readBuildIDFromBinary reads the build ID from a binary. +// ReadBuildIDFromBinary reads the build ID from a binary. // // ELF binaries store the build ID in a proper PT_NOTE section. // @@ -1724,7 +1724,7 @@ var ( // of the text segment, which should appear near the beginning // of the file. This is clumsy but fairly portable. Custom locations // can be added for other binary types as needed, like we did for ELF. -func readBuildIDFromBinary(filename string) (id string, err error) { +func ReadBuildIDFromBinary(filename string) (id string, err error) { if filename == "" { return "", &os.PathError{Op: "parse", Path: filename, Err: errBuildIDUnknown} }