1/* Copyright 2016 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 main 17 18import ( 19 "go/build" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "reflect" 24 "sort" 25 "testing" 26) 27 28var testfiles = map[string]string{ 29 "cgo.go": ` 30//+build cgo 31 32package tags 33 34/* 35#include <stdio.h> 36#include <stdlib.h> 37 38void myprint(char* s) { 39 printf("%s", s); 40} 41*/ 42 43import "C" 44 45func main() { 46 C.myprint("hello") 47} 48`, 49 "extra.go": ` 50//+build a,b b,c 51 52package tags 53`, 54 "ignore.go": ` 55//+build ignore 56 57package tags 58`, 59 "normal.go": ` 60package tags 61`, 62 "on_darwin.go": ` 63package tags 64`, 65 "system.go": ` 66//+build arm,darwin linux,amd64 67 68package tags 69`, 70} 71 72func TestTags(t *testing.T) { 73 tempdir, err := ioutil.TempDir("", "goruletest") 74 if err != nil { 75 t.Fatalf("Error creating temporary directory: %v", err) 76 } 77 defer os.RemoveAll(tempdir) 78 79 input := []string{} 80 for k, v := range testfiles { 81 p := filepath.Join(tempdir, k) 82 if err := ioutil.WriteFile(p, []byte(v), 0644); err != nil { 83 t.Fatalf("WriteFile(%s): %v", p, err) 84 } 85 input = append(input, k) 86 } 87 sort.Strings(input) 88 89 wd, err := os.Getwd() 90 if err != nil { 91 t.Fatalf("Getwd: %v", err) 92 } 93 94 err = os.Chdir(tempdir) 95 if err != nil { 96 t.Fatalf("Chdir(%s): %v", tempdir, err) 97 } 98 defer os.Chdir(wd) 99 100 bctx := build.Default 101 // Always fake the os and arch 102 bctx.GOOS = "darwin" 103 bctx.GOARCH = "amd64" 104 bctx.CgoEnabled = false 105 runTest(t, bctx, input, []string{"normal.go", "on_darwin.go"}) 106 bctx.GOOS = "linux" 107 runTest(t, bctx, input, []string{"normal.go", "system.go"}) 108 bctx.GOARCH = "arm" 109 runTest(t, bctx, input, []string{"normal.go"}) 110 bctx.BuildTags = []string{"a", "b"} 111 runTest(t, bctx, input, []string{"extra.go", "normal.go"}) 112 bctx.BuildTags = []string{"a", "c"} 113 runTest(t, bctx, input, []string{"normal.go"}) 114 bctx.CgoEnabled = true 115 runTest(t, bctx, input, []string{"cgo.go", "normal.go"}) 116} 117 118func runTest(t *testing.T, bctx build.Context, inputs []string, expect []string) { 119 build.Default = bctx 120 got, err := filterAndSplitFiles(inputs) 121 if err != nil { 122 t.Errorf("filter %v,%v,%v,%v failed: %v", bctx.GOOS, bctx.GOARCH, bctx.CgoEnabled, bctx.BuildTags, err) 123 } 124 gotGoFilenames := make([]string, len(got.goSrcs)) 125 for i, src := range got.goSrcs { 126 gotGoFilenames[i] = src.filename 127 } 128 if !reflect.DeepEqual(expect, gotGoFilenames) { 129 t.Errorf("filter %v,%v,%v,%v: expect %v got %v", bctx.GOOS, bctx.GOARCH, bctx.CgoEnabled, bctx.BuildTags, expect, got) 130 } 131} 132 133// abs is a dummy env.go abs to avoid depending on env.go and flags.go. 134func abs(p string) string { 135 return p 136} 137