// 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)
}
// 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,
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.
//
// 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}
}