github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/go/test.bash (about) 1 #!/bin/bash 2 # Copyright 2012 The Go Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style 4 # license that can be found in the LICENSE file. 5 6 set -e 7 go build -o testgo 8 9 ok=true 10 11 unset GOPATH 12 unset GOBIN 13 14 # Test that error messages have file:line information at beginning of 15 # the line. Also test issue 4917: that the error is on stderr. 16 d=$(mktemp -d -t testgoXXX) 17 fn=$d/err.go 18 echo "package main" > $fn 19 echo 'import "bar"' >> $fn 20 ./testgo run $fn 2>$d/err.out || true 21 if ! grep -q "^$fn:" $d/err.out; then 22 echo "missing file:line in error message" 23 cat $d/err.out 24 ok=false 25 fi 26 rm -r $d 27 28 # Test local (./) imports. 29 testlocal() { 30 local="$1" 31 ./testgo build -o hello "testdata/$local/easy.go" 32 ./hello >hello.out 33 if ! grep -q '^easysub\.Hello' hello.out; then 34 echo "testdata/$local/easy.go did not generate expected output" 35 cat hello.out 36 ok=false 37 fi 38 39 ./testgo build -o hello "testdata/$local/easysub/main.go" 40 ./hello >hello.out 41 if ! grep -q '^easysub\.Hello' hello.out; then 42 echo "testdata/$local/easysub/main.go did not generate expected output" 43 cat hello.out 44 ok=false 45 fi 46 47 ./testgo build -o hello "testdata/$local/hard.go" 48 ./hello >hello.out 49 if ! grep -q '^sub\.Hello' hello.out || ! grep -q '^subsub\.Hello' hello.out ; then 50 echo "testdata/$local/hard.go did not generate expected output" 51 cat hello.out 52 ok=false 53 fi 54 55 rm -f hello.out hello 56 57 # Test that go install x.go fails. 58 if ./testgo install "testdata/$local/easy.go" >/dev/null 2>&1; then 59 echo "go install testdata/$local/easy.go succeeded" 60 ok=false 61 fi 62 } 63 64 # Test local imports 65 testlocal local 66 67 # Test local imports again, with bad characters in the directory name. 68 bad='#$%:, &()*;<=>?\^{}' 69 rm -rf "testdata/$bad" 70 cp -R testdata/local "testdata/$bad" 71 testlocal "$bad" 72 rm -rf "testdata/$bad" 73 74 # Test tests with relative imports. 75 if ! ./testgo test ./testdata/testimport; then 76 echo "go test ./testdata/testimport failed" 77 ok=false 78 fi 79 80 # Test installation with relative imports. 81 if ! ./testgo test -i ./testdata/testimport; then 82 echo "go test -i ./testdata/testimport failed" 83 ok=false 84 fi 85 86 # Test tests with relative imports in packages synthesized 87 # from Go files named on the command line. 88 if ! ./testgo test ./testdata/testimport/*.go; then 89 echo "go test ./testdata/testimport/*.go failed" 90 ok=false 91 fi 92 93 # Test that without $GOBIN set, binaries get installed 94 # into the GOPATH bin directory. 95 rm -rf testdata/bin 96 if ! GOPATH=$(pwd)/testdata ./testgo install go-cmd-test; then 97 echo "go install go-cmd-test failed" 98 ok=false 99 elif ! test -x testdata/bin/go-cmd-test; then 100 echo "go install go-cmd-test did not write to testdata/bin/go-cmd-test" 101 ok=false 102 fi 103 104 # And with $GOBIN set, binaries get installed to $GOBIN. 105 if ! GOBIN=$(pwd)/testdata/bin1 GOPATH=$(pwd)/testdata ./testgo install go-cmd-test; then 106 echo "go install go-cmd-test failed" 107 ok=false 108 elif ! test -x testdata/bin1/go-cmd-test; then 109 echo "go install go-cmd-test did not write to testdata/bin1/go-cmd-test" 110 ok=false 111 fi 112 113 # Without $GOBIN set, installing a program outside $GOPATH should fail 114 # (there is nowhere to install it). 115 if ./testgo install testdata/src/go-cmd-test/helloworld.go; then 116 echo "go install testdata/src/go-cmd-test/helloworld.go should have failed, did not" 117 ok=false 118 fi 119 120 # With $GOBIN set, should install there. 121 if ! GOBIN=$(pwd)/testdata/bin1 ./testgo install testdata/src/go-cmd-test/helloworld.go; then 122 echo "go install testdata/src/go-cmd-test/helloworld.go failed" 123 ok=false 124 elif ! test -x testdata/bin1/helloworld; then 125 echo "go install testdata/src/go-cmd-test/helloworld.go did not write testdata/bin1/helloworld" 126 ok=false 127 fi 128 129 # Reject relative paths in GOPATH. 130 if GOPATH=. ./testgo build testdata/src/go-cmd-test/helloworld.go; then 131 echo 'GOPATH="." go build should have failed, did not' 132 ok=false 133 fi 134 135 if GOPATH=:$(pwd)/testdata:. ./testgo build go-cmd-test; then 136 echo 'GOPATH=":$(pwd)/testdata:." go build should have failed, did not' 137 ok=false 138 fi 139 140 # issue 4104 141 if [ $(./testgo test fmt fmt fmt fmt fmt | wc -l) -ne 1 ] ; then 142 echo 'go test fmt fmt fmt fmt fmt tested the same package multiple times' 143 ok=false 144 fi 145 146 # ensure that output of 'go list' is consistent between runs 147 ./testgo list std > test_std.list 148 if ! ./testgo list std | cmp -s test_std.list - ; then 149 echo "go list std ordering is inconsistent" 150 ok=false 151 fi 152 rm -f test_std.list 153 154 # issue 4096. Validate the output of unsucessful go install foo/quxx 155 if [ $(./testgo install 'foo/quxx' 2>&1 | grep -c 'cannot find package "foo/quxx" in any of') -ne 1 ] ; then 156 echo 'go install foo/quxx expected error: .*cannot find package "foo/quxx" in any of' 157 ok=false 158 fi 159 # test GOROOT search failure is reported 160 if [ $(./testgo install 'foo/quxx' 2>&1 | egrep -c 'foo/quxx \(from \$GOROOT\)$') -ne 1 ] ; then 161 echo 'go install foo/quxx expected error: .*foo/quxx (from $GOROOT)' 162 ok=false 163 fi 164 # test multiple GOPATH entries are reported separately 165 if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/./src/foo/quxx') -ne 2 ] ; then 166 echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)\n.*testdata/b/src/foo/quxx' 167 ok=false 168 fi 169 # test (from $GOPATH) annotation is reported for the first GOPATH entry 170 if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/a/src/foo/quxx \(from \$GOPATH\)$') -ne 1 ] ; then 171 echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)' 172 ok=false 173 fi 174 # but not on the second 175 if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/b/src/foo/quxx$') -ne 1 ] ; then 176 echo 'go install foo/quxx expected error: .*testdata/b/src/foo/quxx' 177 ok=false 178 fi 179 # test missing GOPATH is reported 180 if [ $(GOPATH= ./testgo install 'foo/quxx' 2>&1 | egrep -c '\(\$GOPATH not set\)$') -ne 1 ] ; then 181 echo 'go install foo/quxx expected error: ($GOPATH not set)' 182 ok=false 183 fi 184 185 # issue 4186. go get cannot be used to download packages to $GOROOT 186 # Test that without GOPATH set, go get should fail 187 d=$(mktemp -d -t testgoXXX) 188 mkdir -p $d/src/pkg 189 if GOPATH= GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch ; then 190 echo 'go get code.google.com/p/go.codereview/cmd/hgpatch should not succeed with $GOPATH unset' 191 ok=false 192 fi 193 rm -rf $d 194 # Test that with GOPATH=$GOROOT, go get should fail 195 d=$(mktemp -d -t testgoXXX) 196 mkdir -p $d/src/pkg 197 if GOPATH=$d GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch ; then 198 echo 'go get code.google.com/p/go.codereview/cmd/hgpatch should not succeed with GOPATH=$GOROOT' 199 ok=false 200 fi 201 rm -rf $d 202 203 # issue 3941: args with spaces 204 d=$(mktemp -d -t testgoXXX) 205 cat >$d/main.go<<EOF 206 package main 207 var extern string 208 func main() { 209 println(extern) 210 } 211 EOF 212 ./testgo run -ldflags '-X main.extern "hello world"' $d/main.go 2>hello.out 213 if ! grep -q '^hello world' hello.out; then 214 echo "ldflags -X main.extern 'hello world' failed. Output:" 215 cat hello.out 216 ok=false 217 fi 218 rm -rf $d 219 220 # test that go test -cpuprofile leaves binary behind 221 ./testgo test -cpuprofile strings.prof strings || ok=false 222 if [ ! -x strings.test ]; then 223 echo "go test -cpuprofile did not create strings.test" 224 ok=false 225 fi 226 rm -f strings.prof strings.test 227 228 # issue 4568. test that symlinks don't screw things up too badly. 229 old=$(pwd) 230 d=$(mktemp -d -t testgoXXX) 231 mkdir -p $d/src 232 ( 233 ln -s $d $d/src/dir1 234 cd $d/src/dir1 235 echo package p >p.go 236 export GOPATH=$d 237 if [ "$($old/testgo list -f '{{.Root}}' .)" != "$d" ]; then 238 echo got lost in symlink tree: 239 pwd 240 env|grep WD 241 $old/testgo list -json . dir1 242 touch $d/failed 243 fi 244 ) 245 if [ -f $d/failed ]; then 246 ok=false 247 fi 248 rm -rf $d 249 250 # issue 4515. 251 d=$(mktemp -d -t testgoXXX) 252 mkdir -p $d/src/example/a $d/src/example/b $d/bin 253 cat >$d/src/example/a/main.go <<EOF 254 package main 255 func main() {} 256 EOF 257 cat >$d/src/example/b/main.go <<EOF 258 // +build mytag 259 260 package main 261 func main() {} 262 EOF 263 GOPATH=$d ./testgo install -tags mytag example/a example/b || ok=false 264 if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then 265 echo go install example/a example/b did not install binaries 266 ok=false 267 fi 268 rm -f $d/bin/* 269 GOPATH=$d ./testgo install -tags mytag example/... || ok=false 270 if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then 271 echo go install example/... did not install binaries 272 ok=false 273 fi 274 rm -f $d/bin/*go 275 export GOPATH=$d 276 if [ "$(./testgo list -tags mytag example/b...)" != "example/b" ]; then 277 echo go list example/b did not find example/b 278 ok=false 279 fi 280 unset GOPATH 281 rm -rf $d 282 283 # issue 4773. case-insensitive collisions 284 d=$(mktemp -d -t testgoXXX) 285 export GOPATH=$d 286 mkdir -p $d/src/example/a $d/src/example/b 287 cat >$d/src/example/a/a.go <<EOF 288 package p 289 import ( 290 _ "math/rand" 291 _ "math/Rand" 292 ) 293 EOF 294 if ./testgo list example/a 2>$d/out; then 295 echo go list example/a should have failed, did not. 296 ok=false 297 elif ! grep "case-insensitive import collision" $d/out >/dev/null; then 298 echo go list example/a did not report import collision. 299 ok=false 300 fi 301 cat >$d/src/example/b/file.go <<EOF 302 package b 303 EOF 304 cat >$d/src/example/b/FILE.go <<EOF 305 package b 306 EOF 307 if [ $(ls $d/src/example/b | wc -l) = 2 ]; then 308 # case-sensitive file system, let directory read find both files 309 args="example/b" 310 else 311 # case-insensitive file system, list files explicitly on command line. 312 args="$d/src/example/b/file.go $d/src/example/b/FILE.go" 313 fi 314 if ./testgo list $args 2>$d/out; then 315 echo go list example/b should have failed, did not. 316 ok=false 317 elif ! grep "case-insensitive file name collision" $d/out >/dev/null; then 318 echo go list example/b did not report file name collision. 319 ok=false 320 fi 321 unset GOPATH 322 rm -rf $d 323 324 # Only succeeds if source order is preserved. 325 ./testgo test testdata/example[12]_test.go 326 327 # clean up 328 rm -rf testdata/bin testdata/bin1 329 rm -f testgo 330 331 if $ok; then 332 echo PASS 333 else 334 echo FAIL 335 exit 1 336 fi