aboutsummaryrefslogtreecommitdiff
path: root/tests/core/stdlib/buildid_test.go
blob: d95ba90310ef1a597c0a1c0aa66e37d8d9636bc4 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//go:build go1.10
// +build go1.10

/* Copyright 2018 The Bazel Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package buildid_test

import (
	"bytes"
	"errors"
	"os"
	"os/exec"
	"path/filepath"
	"testing"

	"github.com/bazelbuild/rules_go/go/runfiles"
)

func TestEmptyBuildID(t *testing.T) {
	// Locate the buildid tool and several archive files to check.
	//   fmt.a - pure go
	//   crypto/aes.a - contains assembly
	//   runtime/cgo.a - contains cgo
	// The path may vary depending on platform and architecture, so just
	// do a search.
	var buildidPath string
	pkgPaths := map[string]string{
		"fmt.a": "",
		"aes.a": "",
		"cgo.a": "",
	}
	stdlibPkgDir, err := runfiles.Rlocation("io_bazel_rules_go/stdlib_/pkg")
	if err != nil {
		t.Fatal(err)
	}
	n := len(pkgPaths)
	done := errors.New("done")
	var visit filepath.WalkFunc
	visit = func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if filepath.Base(path) == "buildid" && (info.Mode()&0111) != 0 {
			buildidPath = path
		}
		for pkg := range pkgPaths {
			if filepath.Base(path) == pkg {
				pkgPaths[pkg] = path
				n--
			}
		}
		if buildidPath != "" && n == 0 {
			return done
		}
		return nil
	}
	if err = filepath.Walk(stdlibPkgDir, visit); err != nil && err != done {
		t.Fatal(err)
	}
	if buildidPath == "" {
		t.Fatal("buildid not found")
	}

	for pkg, path := range pkgPaths {
		if path == "" {
			t.Errorf("could not locate %s", pkg)
			continue
		}
		// Equivalent to: go tool buildid pkg.a
		// It's an error if this produces any output.
		cmd := exec.Command(buildidPath, path)
		out, err := cmd.Output()
		if err != nil {
			t.Error(err)
		}
		if len(bytes.TrimSpace(out)) > 0 {
			t.Errorf("%s: unexpected buildid: %s", path, out)
		}
	}
}