github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/go/build/build_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package build 6 7 import ( 8 "io" 9 "os" 10 "path/filepath" 11 "reflect" 12 "runtime" 13 "strings" 14 "testing" 15 ) 16 17 func TestMatch(t *testing.T) { 18 ctxt := Default 19 what := "default" 20 match := func(tag string, want map[string]bool) { 21 m := make(map[string]bool) 22 if !ctxt.match(tag, m) { 23 t.Errorf("%s context should match %s, does not", what, tag) 24 } 25 if !reflect.DeepEqual(m, want) { 26 t.Errorf("%s tags = %v, want %v", tag, m, want) 27 } 28 } 29 nomatch := func(tag string, want map[string]bool) { 30 m := make(map[string]bool) 31 if ctxt.match(tag, m) { 32 t.Errorf("%s context should NOT match %s, does", what, tag) 33 } 34 if !reflect.DeepEqual(m, want) { 35 t.Errorf("%s tags = %v, want %v", tag, m, want) 36 } 37 } 38 39 match(runtime.GOOS+","+runtime.GOARCH, map[string]bool{runtime.GOOS: true, runtime.GOARCH: true}) 40 match(runtime.GOOS+","+runtime.GOARCH+",!foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 41 nomatch(runtime.GOOS+","+runtime.GOARCH+",foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 42 43 what = "modified" 44 ctxt.BuildTags = []string{"foo"} 45 match(runtime.GOOS+","+runtime.GOARCH, map[string]bool{runtime.GOOS: true, runtime.GOARCH: true}) 46 match(runtime.GOOS+","+runtime.GOARCH+",foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 47 nomatch(runtime.GOOS+","+runtime.GOARCH+",!foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 48 match(runtime.GOOS+","+runtime.GOARCH+",!bar", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "bar": true}) 49 nomatch(runtime.GOOS+","+runtime.GOARCH+",bar", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "bar": true}) 50 nomatch("!", map[string]bool{}) 51 } 52 53 func TestDotSlashImport(t *testing.T) { 54 p, err := ImportDir("testdata/other", 0) 55 if err != nil { 56 t.Fatal(err) 57 } 58 if len(p.Imports) != 1 || p.Imports[0] != "./file" { 59 t.Fatalf("testdata/other: Imports=%v, want [./file]", p.Imports) 60 } 61 62 p1, err := Import("./file", "testdata/other", 0) 63 if err != nil { 64 t.Fatal(err) 65 } 66 if p1.Name != "file" { 67 t.Fatalf("./file: Name=%q, want %q", p1.Name, "file") 68 } 69 dir := filepath.Clean("testdata/other/file") // Clean to use \ on Windows 70 if p1.Dir != dir { 71 t.Fatalf("./file: Dir=%q, want %q", p1.Name, dir) 72 } 73 } 74 75 func TestEmptyImport(t *testing.T) { 76 p, err := Import("", Default.GOROOT, FindOnly) 77 if err == nil { 78 t.Fatal(`Import("") returned nil error.`) 79 } 80 if p == nil { 81 t.Fatal(`Import("") returned nil package.`) 82 } 83 if p.ImportPath != "" { 84 t.Fatalf("ImportPath=%q, want %q.", p.ImportPath, "") 85 } 86 } 87 88 func TestEmptyFolderImport(t *testing.T) { 89 _, err := Import(".", "testdata/empty", 0) 90 if _, ok := err.(*NoGoError); !ok { 91 t.Fatal(`Import("testdata/empty") did not return NoGoError.`) 92 } 93 } 94 95 func TestMultiplePackageImport(t *testing.T) { 96 _, err := Import(".", "testdata/multi", 0) 97 mpe, ok := err.(*MultiplePackageError) 98 if !ok { 99 t.Fatal(`Import("testdata/multi") did not return MultiplePackageError.`) 100 } 101 want := &MultiplePackageError{ 102 Dir: filepath.FromSlash("testdata/multi"), 103 Packages: []string{"main", "test_package"}, 104 Files: []string{"file.go", "file_appengine.go"}, 105 } 106 if !reflect.DeepEqual(mpe, want) { 107 t.Errorf("got %#v; want %#v", mpe, want) 108 } 109 } 110 111 func TestLocalDirectory(t *testing.T) { 112 cwd, err := os.Getwd() 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 p, err := ImportDir(cwd, 0) 118 if err != nil { 119 t.Fatal(err) 120 } 121 if p.ImportPath != "go/build" { 122 t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build") 123 } 124 } 125 126 func TestShouldBuild(t *testing.T) { 127 const file1 = "// +build tag1\n\n" + 128 "package main\n" 129 want1 := map[string]bool{"tag1": true} 130 131 const file2 = "// +build cgo\n\n" + 132 "// This package implements parsing of tags like\n" + 133 "// +build tag1\n" + 134 "package build" 135 want2 := map[string]bool{"cgo": true} 136 137 const file3 = "// Copyright The Go Authors.\n\n" + 138 "package build\n\n" + 139 "// shouldBuild checks tags given by lines of the form\n" + 140 "// +build tag\n" + 141 "func shouldBuild(content []byte)\n" 142 want3 := map[string]bool{} 143 144 ctx := &Context{BuildTags: []string{"tag1"}} 145 m := map[string]bool{} 146 if !ctx.shouldBuild([]byte(file1), m) { 147 t.Errorf("shouldBuild(file1) = false, want true") 148 } 149 if !reflect.DeepEqual(m, want1) { 150 t.Errorf("shoudBuild(file1) tags = %v, want %v", m, want1) 151 } 152 153 m = map[string]bool{} 154 if ctx.shouldBuild([]byte(file2), m) { 155 t.Errorf("shouldBuild(file2) = true, want fakse") 156 } 157 if !reflect.DeepEqual(m, want2) { 158 t.Errorf("shoudBuild(file2) tags = %v, want %v", m, want2) 159 } 160 161 m = map[string]bool{} 162 ctx = &Context{BuildTags: nil} 163 if !ctx.shouldBuild([]byte(file3), m) { 164 t.Errorf("shouldBuild(file3) = false, want true") 165 } 166 if !reflect.DeepEqual(m, want3) { 167 t.Errorf("shoudBuild(file3) tags = %v, want %v", m, want3) 168 } 169 } 170 171 type readNopCloser struct { 172 io.Reader 173 } 174 175 func (r readNopCloser) Close() error { 176 return nil 177 } 178 179 var ( 180 ctxtP9 = Context{GOARCH: "arm", GOOS: "plan9"} 181 ctxtAndroid = Context{GOARCH: "arm", GOOS: "android"} 182 ) 183 184 var matchFileTests = []struct { 185 ctxt Context 186 name string 187 data string 188 match bool 189 }{ 190 {ctxtP9, "foo_arm.go", "", true}, 191 {ctxtP9, "foo1_arm.go", "// +build linux\n\npackage main\n", false}, 192 {ctxtP9, "foo_darwin.go", "", false}, 193 {ctxtP9, "foo.go", "", true}, 194 {ctxtP9, "foo1.go", "// +build linux\n\npackage main\n", false}, 195 {ctxtP9, "foo.badsuffix", "", false}, 196 {ctxtAndroid, "foo_linux.go", "", true}, 197 {ctxtAndroid, "foo_android.go", "", true}, 198 {ctxtAndroid, "foo_plan9.go", "", false}, 199 {ctxtAndroid, "android.go", "", true}, 200 {ctxtAndroid, "plan9.go", "", true}, 201 {ctxtAndroid, "plan9_test.go", "", true}, 202 {ctxtAndroid, "arm.s", "", true}, 203 {ctxtAndroid, "amd64.s", "", true}, 204 } 205 206 func TestMatchFile(t *testing.T) { 207 for _, tt := range matchFileTests { 208 ctxt := tt.ctxt 209 ctxt.OpenFile = func(path string) (r io.ReadCloser, err error) { 210 if path != "x+"+tt.name { 211 t.Fatalf("OpenFile asked for %q, expected %q", path, "x+"+tt.name) 212 } 213 return &readNopCloser{strings.NewReader(tt.data)}, nil 214 } 215 ctxt.JoinPath = func(elem ...string) string { 216 return strings.Join(elem, "+") 217 } 218 match, err := ctxt.MatchFile("x", tt.name) 219 if match != tt.match || err != nil { 220 t.Fatalf("MatchFile(%q) = %v, %v, want %v, nil", tt.name, match, err, tt.match) 221 } 222 } 223 } 224 225 func TestImportCmd(t *testing.T) { 226 p, err := Import("cmd/internal/objfile", "", 0) 227 if err != nil { 228 t.Fatal(err) 229 } 230 if !strings.HasSuffix(filepath.ToSlash(p.Dir), "src/cmd/internal/objfile") { 231 t.Fatalf("Import cmd/internal/objfile returned Dir=%q, want %q", filepath.ToSlash(p.Dir), ".../src/cmd/internal/objfile") 232 } 233 } 234 235 var ( 236 expandSrcDirPath = filepath.Join(string(filepath.Separator)+"projects", "src", "add") 237 ) 238 239 var expandSrcDirTests = []struct { 240 input, expected string 241 }{ 242 {"-L ${SRCDIR}/libs -ladd", "-L /projects/src/add/libs -ladd"}, 243 {"${SRCDIR}/add_linux_386.a -pthread -lstdc++", "/projects/src/add/add_linux_386.a -pthread -lstdc++"}, 244 {"Nothing to expand here!", "Nothing to expand here!"}, 245 {"$", "$"}, 246 {"$$", "$$"}, 247 {"${", "${"}, 248 {"$}", "$}"}, 249 {"$FOO ${BAR}", "$FOO ${BAR}"}, 250 {"Find me the $SRCDIRECTORY.", "Find me the $SRCDIRECTORY."}, 251 {"$SRCDIR is missing braces", "$SRCDIR is missing braces"}, 252 } 253 254 func TestExpandSrcDir(t *testing.T) { 255 for _, test := range expandSrcDirTests { 256 output := expandSrcDir(test.input, expandSrcDirPath) 257 if output != test.expected { 258 t.Errorf("%q expands to %q with SRCDIR=%q when %q is expected", test.input, output, expandSrcDirPath, test.expected) 259 } else { 260 t.Logf("%q expands to %q with SRCDIR=%q", test.input, output, expandSrcDirPath) 261 } 262 } 263 }