1/* Copyright 2017 The Bazel Authors. All rights reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14*/ 15 16package cross_test 17 18import ( 19 "flag" 20 "os" 21 "os/exec" 22 "path/filepath" 23 "strings" 24 "testing" 25 26 "github.com/bazelbuild/rules_go/go/tools/bazel" 27) 28 29type check struct { 30 file *string 31 info []string 32} 33 34var darwin = flag.String("darwin", "", "The darwin binary") 35var linux = flag.String("linux", "", "The linux binary") 36var windows = flag.String("windows", "", "The windows binary") 37 38var checks = []check{ 39 {darwin, []string{ 40 "Mach-O", 41 "64-bit", 42 "executable", 43 "x86_64", 44 }}, 45 {linux, []string{ 46 "ELF", 47 "64-bit", 48 "executable", 49 "x86-64", 50 }}, 51 {windows, []string{ 52 "PE32+", 53 "Windows", 54 "executable", 55 "console", 56 "x86-64", 57 }}, 58} 59 60func TestCross(t *testing.T) { 61 for _, c := range checks { 62 path, err := bazel.Runfile(*c.file) 63 if err != nil { 64 t.Fatalf("Could not find runfile %s: %q", *c.file, err) 65 } 66 67 if _, err := os.Stat(path); os.IsNotExist(err) { 68 t.Fatalf("Missing binary %v", path) 69 } 70 file, err := filepath.EvalSymlinks(path) 71 if err != nil { 72 t.Fatalf("Invalid filename %v", path) 73 } 74 cmd := exec.Command("file", file) 75 cmd.Stderr = os.Stderr 76 res, err := cmd.Output() 77 if err != nil { 78 t.Fatalf("failed running 'file': %v", err) 79 } 80 output := string(res) 81 if index := strings.Index(output, ":"); index >= 0 { 82 output = output[index+1:] 83 } 84 output = strings.TrimSpace(output) 85 for _, info := range c.info { 86 if !strings.Contains(output, info) { 87 t.Errorf("incorrect type for %v\nExpected %v\nGot %v", file, info, output) 88 } 89 } 90 } 91} 92