aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/note_test.go
blob: 39b79c5cfd8630e107f6daf7dd5174f8292fd623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2015 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main_test

import (
	main "cmd/go"
	"runtime"
	"testing"
)

func TestNoteReading(t *testing.T) {
	testNoteReading(t)
}

func TestNoteReading2K(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skipf("2kB is not enough on %s", runtime.GOOS)
	}
	// Set BuildIDReadSize to 2kB to exercise Mach-O parsing more strictly.
	defer func(old int) {
		main.BuildIDReadSize = old
	}(main.BuildIDReadSize)
	main.BuildIDReadSize = 2 * 1024

	testNoteReading(t)
}

func testNoteReading(t *testing.T) {
	tg := testgo(t)
	defer tg.cleanup()
	tg.tempFile("hello.go", `package main; func main() { print("hello, world\n") }`)
	const buildID = "TestNoteReading-Build-ID"
	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)
	}

	if runtime.GOOS == "linux" && runtime.GOARCH == "ppc64le" {
		t.Skipf("skipping - golang.org/issue/11184")
	}

	switch runtime.GOOS {
	case "plan9":
		// no external linking
		t.Logf("no external linking - skipping linkmode=external test")

	default:
		tg.run("build", "-ldflags", "-buildid="+buildID+" -linkmode=external", "-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 (linkmode=external): %v", err)
		}
		if id != buildID {
			t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external)", id, buildID)
		}
	}
}