github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/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 TestLocalDirectory(t *testing.T) { 89 cwd, err := os.Getwd() 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 p, err := ImportDir(cwd, 0) 95 if err != nil { 96 t.Fatal(err) 97 } 98 if p.ImportPath != "go/build" { 99 t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build") 100 } 101 } 102 103 func TestShouldBuild(t *testing.T) { 104 const file1 = "// +build tag1\n\n" + 105 "package main\n" 106 want1 := map[string]bool{"tag1": true} 107 108 const file2 = "// +build cgo\n\n" + 109 "// This package implements parsing of tags like\n" + 110 "// +build tag1\n" + 111 "package build" 112 want2 := map[string]bool{"cgo": true} 113 114 const file3 = "// Copyright The Go Authors.\n\n" + 115 "package build\n\n" + 116 "// shouldBuild checks tags given by lines of the form\n" + 117 "// +build tag\n" + 118 "func shouldBuild(content []byte)\n" 119 want3 := map[string]bool{} 120 121 ctx := &Context{BuildTags: []string{"tag1"}} 122 m := map[string]bool{} 123 if !ctx.shouldBuild([]byte(file1), m) { 124 t.Errorf("shouldBuild(file1) = false, want true") 125 } 126 if !reflect.DeepEqual(m, want1) { 127 t.Errorf("shoudBuild(file1) tags = %v, want %v", m, want1) 128 } 129 130 m = map[string]bool{} 131 if ctx.shouldBuild([]byte(file2), m) { 132 t.Errorf("shouldBuild(file2) = true, want fakse") 133 } 134 if !reflect.DeepEqual(m, want2) { 135 t.Errorf("shoudBuild(file2) tags = %v, want %v", m, want2) 136 } 137 138 m = map[string]bool{} 139 ctx = &Context{BuildTags: nil} 140 if !ctx.shouldBuild([]byte(file3), m) { 141 t.Errorf("shouldBuild(file3) = false, want true") 142 } 143 if !reflect.DeepEqual(m, want3) { 144 t.Errorf("shoudBuild(file3) tags = %v, want %v", m, want3) 145 } 146 } 147 148 type readNopCloser struct { 149 io.Reader 150 } 151 152 func (r readNopCloser) Close() error { 153 return nil 154 } 155 156 var matchFileTests = []struct { 157 name string 158 data string 159 match bool 160 }{ 161 {"foo_arm.go", "", true}, 162 {"foo1_arm.go", "// +build linux\n\npackage main\n", false}, 163 {"foo_darwin.go", "", false}, 164 {"foo.go", "", true}, 165 {"foo1.go", "// +build linux\n\npackage main\n", false}, 166 {"foo.badsuffix", "", false}, 167 } 168 169 func TestMatchFile(t *testing.T) { 170 for _, tt := range matchFileTests { 171 ctxt := Context{GOARCH: "arm", GOOS: "plan9"} 172 ctxt.OpenFile = func(path string) (r io.ReadCloser, err error) { 173 if path != "x+"+tt.name { 174 t.Fatalf("OpenFile asked for %q, expected %q", path, "x+"+tt.name) 175 } 176 return &readNopCloser{strings.NewReader(tt.data)}, nil 177 } 178 ctxt.JoinPath = func(elem ...string) string { 179 return strings.Join(elem, "+") 180 } 181 match, err := ctxt.MatchFile("x", tt.name) 182 if match != tt.match || err != nil { 183 t.Fatalf("MatchFile(%q) = %v, %v, want %v, nil", tt.name, match, err, tt.match) 184 } 185 } 186 }