github.com/0xKiwi/rules_go@v0.24.3/tests/core/stdlib/buildid_test.go (about)

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