github.com/aca02djr/gb@v0.4.1/importer/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 importer 6 7 import ( 8 "io" 9 "path/filepath" 10 "reflect" 11 "runtime" 12 "testing" 13 ) 14 15 func TestMatch(t *testing.T) { 16 ctxt := &Importer{ 17 Context: &Context{ 18 GOOS: runtime.GOOS, 19 GOARCH: runtime.GOARCH, 20 }, 21 Root: filepath.Join(runtime.GOROOT()), 22 } 23 what := "default" 24 match := func(tag string, want map[string]bool) { 25 m := make(map[string]bool) 26 if !ctxt.match(tag, m) { 27 t.Errorf("%s context should match %s, does not", what, tag) 28 } 29 if !reflect.DeepEqual(m, want) { 30 t.Errorf("%s tags = %v, want %v", tag, m, want) 31 } 32 } 33 nomatch := func(tag string, want map[string]bool) { 34 m := make(map[string]bool) 35 if ctxt.match(tag, m) { 36 t.Errorf("%s context should NOT match %s, does", what, tag) 37 } 38 if !reflect.DeepEqual(m, want) { 39 t.Errorf("%s tags = %v, want %v", tag, m, want) 40 } 41 } 42 43 match(runtime.GOOS+","+runtime.GOARCH, map[string]bool{runtime.GOOS: true, runtime.GOARCH: true}) 44 match(runtime.GOOS+","+runtime.GOARCH+",!foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 45 nomatch(runtime.GOOS+","+runtime.GOARCH+",foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 46 47 what = "modified" 48 ctxt.BuildTags = []string{"foo"} 49 match(runtime.GOOS+","+runtime.GOARCH, map[string]bool{runtime.GOOS: true, runtime.GOARCH: true}) 50 match(runtime.GOOS+","+runtime.GOARCH+",foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 51 nomatch(runtime.GOOS+","+runtime.GOARCH+",!foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 52 match(runtime.GOOS+","+runtime.GOARCH+",!bar", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "bar": true}) 53 nomatch(runtime.GOOS+","+runtime.GOARCH+",bar", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "bar": true}) 54 nomatch("!", map[string]bool{}) 55 } 56 57 func TestShouldBuild(t *testing.T) { 58 const file1 = "// +build tag1\n\n" + 59 "package main\n" 60 want1 := map[string]bool{"tag1": true} 61 62 const file2 = "// +build cgo\n\n" + 63 "// This package implements parsing of tags like\n" + 64 "// +build tag1\n" + 65 "package build" 66 want2 := map[string]bool{"cgo": true} 67 68 const file3 = "// Copyright The Go Authors.\n\n" + 69 "package build\n\n" + 70 "// shouldBuild checks tags given by lines of the form\n" + 71 "// +build tag\n" + 72 "func shouldBuild(content []byte)\n" 73 want3 := map[string]bool{} 74 75 ctx := &Importer{Context: &Context{BuildTags: []string{"tag1"}}} 76 m := map[string]bool{} 77 if !ctx.shouldBuild([]byte(file1), m) { 78 t.Errorf("shouldBuild(file1) = false, want true") 79 } 80 if !reflect.DeepEqual(m, want1) { 81 t.Errorf("shoudBuild(file1) tags = %v, want %v", m, want1) 82 } 83 84 m = map[string]bool{} 85 if ctx.shouldBuild([]byte(file2), m) { 86 t.Errorf("shouldBuild(file2) = true, want fakse") 87 } 88 if !reflect.DeepEqual(m, want2) { 89 t.Errorf("shoudBuild(file2) tags = %v, want %v", m, want2) 90 } 91 92 m = map[string]bool{} 93 ctx = &Importer{Context: &Context{BuildTags: nil}} 94 if !ctx.shouldBuild([]byte(file3), m) { 95 t.Errorf("shouldBuild(file3) = false, want true") 96 } 97 if !reflect.DeepEqual(m, want3) { 98 t.Errorf("shoudBuild(file3) tags = %v, want %v", m, want3) 99 } 100 } 101 102 type readNopCloser struct { 103 io.Reader 104 } 105 106 func (r readNopCloser) Close() error { 107 return nil 108 } 109 110 var ( 111 ctxtP9 = Context{GOARCH: "arm", GOOS: "plan9"} 112 ctxtAndroid = Context{GOARCH: "arm", GOOS: "android"} 113 ) 114 115 var matchFileTests = []struct { 116 ctxt Context 117 name string 118 data string 119 match bool 120 }{ 121 {ctxtP9, "foo_arm.go", "", true}, 122 {ctxtP9, "foo1_arm.go", "// +build linux\n\npackage main\n", false}, 123 {ctxtP9, "foo_darwin.go", "", false}, 124 {ctxtP9, "foo.go", "", true}, 125 {ctxtP9, "foo1.go", "// +build linux\n\npackage main\n", false}, 126 {ctxtP9, "foo.badsuffix", "", false}, 127 {ctxtAndroid, "foo_linux.go", "", true}, 128 {ctxtAndroid, "foo_android.go", "", true}, 129 {ctxtAndroid, "foo_plan9.go", "", false}, 130 {ctxtAndroid, "android.go", "", true}, 131 {ctxtAndroid, "plan9.go", "", true}, 132 {ctxtAndroid, "plan9_test.go", "", true}, 133 {ctxtAndroid, "arm.s", "", true}, 134 {ctxtAndroid, "amd64.s", "", true}, 135 } 136 137 var ( 138 expandSrcDirPath = filepath.Join(string(filepath.Separator)+"projects", "src", "add") 139 ) 140 141 var expandSrcDirTests = []struct { 142 input, expected string 143 }{ 144 {"-L ${SRCDIR}/libs -ladd", "-L /projects/src/add/libs -ladd"}, 145 {"${SRCDIR}/add_linux_386.a -pthread -lstdc++", "/projects/src/add/add_linux_386.a -pthread -lstdc++"}, 146 {"Nothing to expand here!", "Nothing to expand here!"}, 147 {"$", "$"}, 148 {"$$", "$$"}, 149 {"${", "${"}, 150 {"$}", "$}"}, 151 {"$FOO ${BAR}", "$FOO ${BAR}"}, 152 {"Find me the $SRCDIRECTORY.", "Find me the $SRCDIRECTORY."}, 153 {"$SRCDIR is missing braces", "$SRCDIR is missing braces"}, 154 } 155 156 func TestExpandSrcDir(t *testing.T) { 157 for _, test := range expandSrcDirTests { 158 output, _ := expandSrcDir(test.input, expandSrcDirPath) 159 if output != test.expected { 160 t.Errorf("%q expands to %q with SRCDIR=%q when %q is expected", test.input, output, expandSrcDirPath, test.expected) 161 } else { 162 t.Logf("%q expands to %q with SRCDIR=%q", test.input, output, expandSrcDirPath) 163 } 164 } 165 } 166 167 func TestShellSafety(t *testing.T) { 168 tests := []struct { 169 input, srcdir, expected string 170 result bool 171 }{ 172 {"-I${SRCDIR}/../include", "/projects/src/issue 11868", "-I/projects/src/issue 11868/../include", true}, 173 {"-X${SRCDIR}/1,${SRCDIR}/2", "/projects/src/issue 11868", "-X/projects/src/issue 11868/1,/projects/src/issue 11868/2", true}, 174 {"-I/tmp -I/tmp", "/tmp2", "-I/tmp -I/tmp", false}, 175 {"-I/tmp", "/tmp/[0]", "-I/tmp", true}, 176 {"-I${SRCDIR}/dir", "/tmp/[0]", "-I/tmp/[0]/dir", false}, 177 } 178 for _, test := range tests { 179 output, ok := expandSrcDir(test.input, test.srcdir) 180 if ok != test.result { 181 t.Errorf("Expected %t while %q expands to %q with SRCDIR=%q; got %t", test.result, test.input, output, test.srcdir, ok) 182 } 183 if output != test.expected { 184 t.Errorf("Expected %q while %q expands with SRCDIR=%q; got %q", test.expected, test.input, test.srcdir, output) 185 } 186 } 187 }