github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/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 if runtime.GOOS == "darwin" { 113 switch runtime.GOARCH { 114 case "arm", "arm64": 115 t.Skipf("skipping on %s/%s, no valid GOROOT", runtime.GOOS, runtime.GOARCH) 116 } 117 } 118 119 cwd, err := os.Getwd() 120 if err != nil { 121 t.Fatal(err) 122 } 123 124 p, err := ImportDir(cwd, 0) 125 if err != nil { 126 t.Fatal(err) 127 } 128 if p.ImportPath != "go/build" { 129 t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build") 130 } 131 } 132 133 func TestShouldBuild(t *testing.T) { 134 const file1 = "// +build tag1\n\n" + 135 "package main\n" 136 want1 := map[string]bool{"tag1": true} 137 138 const file2 = "// +build cgo\n\n" + 139 "// This package implements parsing of tags like\n" + 140 "// +build tag1\n" + 141 "package build" 142 want2 := map[string]bool{"cgo": true} 143 144 const file3 = "// Copyright The Go Authors.\n\n" + 145 "package build\n\n" + 146 "// shouldBuild checks tags given by lines of the form\n" + 147 "// +build tag\n" + 148 "func shouldBuild(content []byte)\n" 149 want3 := map[string]bool{} 150 151 ctx := &Context{BuildTags: []string{"tag1"}} 152 m := map[string]bool{} 153 if !ctx.shouldBuild([]byte(file1), m) { 154 t.Errorf("shouldBuild(file1) = false, want true") 155 } 156 if !reflect.DeepEqual(m, want1) { 157 t.Errorf("shoudBuild(file1) tags = %v, want %v", m, want1) 158 } 159 160 m = map[string]bool{} 161 if ctx.shouldBuild([]byte(file2), m) { 162 t.Errorf("shouldBuild(file2) = true, want fakse") 163 } 164 if !reflect.DeepEqual(m, want2) { 165 t.Errorf("shoudBuild(file2) tags = %v, want %v", m, want2) 166 } 167 168 m = map[string]bool{} 169 ctx = &Context{BuildTags: nil} 170 if !ctx.shouldBuild([]byte(file3), m) { 171 t.Errorf("shouldBuild(file3) = false, want true") 172 } 173 if !reflect.DeepEqual(m, want3) { 174 t.Errorf("shoudBuild(file3) tags = %v, want %v", m, want3) 175 } 176 } 177 178 type readNopCloser struct { 179 io.Reader 180 } 181 182 func (r readNopCloser) Close() error { 183 return nil 184 } 185 186 var ( 187 ctxtP9 = Context{GOARCH: "arm", GOOS: "plan9"} 188 ctxtAndroid = Context{GOARCH: "arm", GOOS: "android"} 189 ) 190 191 var matchFileTests = []struct { 192 ctxt Context 193 name string 194 data string 195 match bool 196 }{ 197 {ctxtP9, "foo_arm.go", "", true}, 198 {ctxtP9, "foo1_arm.go", "// +build linux\n\npackage main\n", false}, 199 {ctxtP9, "foo_darwin.go", "", false}, 200 {ctxtP9, "foo.go", "", true}, 201 {ctxtP9, "foo1.go", "// +build linux\n\npackage main\n", false}, 202 {ctxtP9, "foo.badsuffix", "", false}, 203 {ctxtAndroid, "foo_linux.go", "", true}, 204 {ctxtAndroid, "foo_android.go", "", true}, 205 {ctxtAndroid, "foo_plan9.go", "", false}, 206 {ctxtAndroid, "android.go", "", true}, 207 {ctxtAndroid, "plan9.go", "", true}, 208 {ctxtAndroid, "plan9_test.go", "", true}, 209 {ctxtAndroid, "arm.s", "", true}, 210 {ctxtAndroid, "amd64.s", "", true}, 211 } 212 213 func TestMatchFile(t *testing.T) { 214 for _, tt := range matchFileTests { 215 ctxt := tt.ctxt 216 ctxt.OpenFile = func(path string) (r io.ReadCloser, err error) { 217 if path != "x+"+tt.name { 218 t.Fatalf("OpenFile asked for %q, expected %q", path, "x+"+tt.name) 219 } 220 return &readNopCloser{strings.NewReader(tt.data)}, nil 221 } 222 ctxt.JoinPath = func(elem ...string) string { 223 return strings.Join(elem, "+") 224 } 225 match, err := ctxt.MatchFile("x", tt.name) 226 if match != tt.match || err != nil { 227 t.Fatalf("MatchFile(%q) = %v, %v, want %v, nil", tt.name, match, err, tt.match) 228 } 229 } 230 } 231 232 func TestImportCmd(t *testing.T) { 233 if runtime.GOOS == "darwin" { 234 switch runtime.GOARCH { 235 case "arm", "arm64": 236 t.Skipf("skipping on %s/%s, no valid GOROOT", runtime.GOOS, runtime.GOARCH) 237 } 238 } 239 240 p, err := Import("cmd/internal/objfile", "", 0) 241 if err != nil { 242 t.Fatal(err) 243 } 244 if !strings.HasSuffix(filepath.ToSlash(p.Dir), "src/cmd/internal/objfile") { 245 t.Fatalf("Import cmd/internal/objfile returned Dir=%q, want %q", filepath.ToSlash(p.Dir), ".../src/cmd/internal/objfile") 246 } 247 } 248 249 var ( 250 expandSrcDirPath = filepath.Join(string(filepath.Separator)+"projects", "src", "add") 251 ) 252 253 var expandSrcDirTests = []struct { 254 input, expected string 255 }{ 256 {"-L ${SRCDIR}/libs -ladd", "-L /projects/src/add/libs -ladd"}, 257 {"${SRCDIR}/add_linux_386.a -pthread -lstdc++", "/projects/src/add/add_linux_386.a -pthread -lstdc++"}, 258 {"Nothing to expand here!", "Nothing to expand here!"}, 259 {"$", "$"}, 260 {"$$", "$$"}, 261 {"${", "${"}, 262 {"$}", "$}"}, 263 {"$FOO ${BAR}", "$FOO ${BAR}"}, 264 {"Find me the $SRCDIRECTORY.", "Find me the $SRCDIRECTORY."}, 265 {"$SRCDIR is missing braces", "$SRCDIR is missing braces"}, 266 } 267 268 func TestExpandSrcDir(t *testing.T) { 269 for _, test := range expandSrcDirTests { 270 output := expandSrcDir(test.input, expandSrcDirPath) 271 if output != test.expected { 272 t.Errorf("%q expands to %q with SRCDIR=%q when %q is expected", test.input, output, expandSrcDirPath, test.expected) 273 } else { 274 t.Logf("%q expands to %q with SRCDIR=%q", test.input, output, expandSrcDirPath) 275 } 276 } 277 }