github.com/fjballest/golang@v0.0.0-20151209143359-e4c5fe594ca8/src/cmd/go/note_test.go (about)

     1  // Copyright 2015 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main_test
     6  
     7  import (
     8  	main "cmd/go"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  func TestNoteReading(t *testing.T) {
    14  	testNoteReading(t)
    15  }
    16  
    17  func TestNoteReading2K(t *testing.T) {
    18  	if runtime.GOOS == "windows" {
    19  		t.Skipf("2kB is not enough on %s", runtime.GOOS)
    20  	}
    21  	// Set BuildIDReadSize to 2kB to exercise Mach-O parsing more strictly.
    22  	defer func(old int) {
    23  		main.BuildIDReadSize = old
    24  	}(main.BuildIDReadSize)
    25  	main.BuildIDReadSize = 2 * 1024
    26  
    27  	testNoteReading(t)
    28  }
    29  
    30  func testNoteReading(t *testing.T) {
    31  	tg := testgo(t)
    32  	defer tg.cleanup()
    33  	tg.tempFile("hello.go", `package main; func main() { print("hello, world\n") }`)
    34  	const buildID = "TestNoteReading-Build-ID"
    35  	tg.run("build", "-ldflags", "-buildid="+buildID, "-o", tg.path("hello.exe"), tg.path("hello.go"))
    36  	id, err := main.ReadBuildIDFromBinary(tg.path("hello.exe"))
    37  	if err != nil {
    38  		t.Fatalf("reading build ID from hello binary: %v", err)
    39  	}
    40  	if id != buildID {
    41  		t.Fatalf("buildID in hello binary = %q, want %q", id, buildID)
    42  	}
    43  
    44  	switch {
    45  	case runtime.GOOS == "linux" && (runtime.GOARCH == "ppc64le" || runtime.GOARCH == "ppc64"):
    46  		t.Skipf("skipping - external linking not supported, golang.org/issue/11184")
    47  	case runtime.GOOS == "linux" && (runtime.GOARCH == "mips64le" || runtime.GOARCH == "mips64"):
    48  		t.Skipf("skipping - external linking not supported, golang.org/issue/12560")
    49  	case runtime.GOOS == "openbsd" && runtime.GOARCH == "arm":
    50  		t.Skipf("skipping - external linking not supported, golang.org/issue/10619")
    51  	case runtime.GOOS == "plan9":
    52  		t.Skipf("skipping - external linking not supported")
    53  	}
    54  
    55  	tg.run("build", "-ldflags", "-buildid="+buildID+" -linkmode=external", "-o", tg.path("hello.exe"), tg.path("hello.go"))
    56  	id, err = main.ReadBuildIDFromBinary(tg.path("hello.exe"))
    57  	if err != nil {
    58  		t.Fatalf("reading build ID from hello binary (linkmode=external): %v", err)
    59  	}
    60  	if id != buildID {
    61  		t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external)", id, buildID)
    62  	}
    63  }