• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//go:build go1.10
2// +build go1.10
3
4/* Copyright 2018 The Bazel Authors. All rights reserved.
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10   http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17*/
18
19package buildid_test
20
21import (
22	"bytes"
23	"errors"
24	"os"
25	"os/exec"
26	"path/filepath"
27	"testing"
28
29	"github.com/bazelbuild/rules_go/go/runfiles"
30)
31
32func TestEmptyBuildID(t *testing.T) {
33	// Locate the buildid tool and several archive files to check.
34	//   fmt.a - pure go
35	//   crypto/aes.a - contains assembly
36	//   runtime/cgo.a - contains cgo
37	// The path may vary depending on platform and architecture, so just
38	// do a search.
39	var buildidPath string
40	pkgPaths := map[string]string{
41		"fmt.a": "",
42		"aes.a": "",
43		"cgo.a": "",
44	}
45	stdlibPkgDir, err := runfiles.Rlocation("io_bazel_rules_go/stdlib_/pkg")
46	if err != nil {
47		t.Fatal(err)
48	}
49	n := len(pkgPaths)
50	done := errors.New("done")
51	var visit filepath.WalkFunc
52	visit = func(path string, info os.FileInfo, err error) error {
53		if err != nil {
54			return err
55		}
56		if filepath.Base(path) == "buildid" && (info.Mode()&0111) != 0 {
57			buildidPath = path
58		}
59		for pkg := range pkgPaths {
60			if filepath.Base(path) == pkg {
61				pkgPaths[pkg] = path
62				n--
63			}
64		}
65		if buildidPath != "" && n == 0 {
66			return done
67		}
68		return nil
69	}
70	if err = filepath.Walk(stdlibPkgDir, visit); err != nil && err != done {
71		t.Fatal(err)
72	}
73	if buildidPath == "" {
74		t.Fatal("buildid not found")
75	}
76
77	for pkg, path := range pkgPaths {
78		if path == "" {
79			t.Errorf("could not locate %s", pkg)
80			continue
81		}
82		// Equivalent to: go tool buildid pkg.a
83		// It's an error if this produces any output.
84		cmd := exec.Command(buildidPath, path)
85		out, err := cmd.Output()
86		if err != nil {
87			t.Error(err)
88		}
89		if len(bytes.TrimSpace(out)) > 0 {
90			t.Errorf("%s: unexpected buildid: %s", path, out)
91		}
92	}
93}
94