github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/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 go() { 9 echo TEST ERROR: ran go, not testgo: go "$@" >&2 10 exit 2 11 } 12 13 started=false 14 TEST() { 15 if $started; then 16 stop 17 fi 18 echo TEST: "$@" 19 started=true 20 ok=true 21 } 22 stop() { 23 if ! $started; then 24 echo TEST ERROR: stop missing start >&2 25 exit 2 26 fi 27 started=false 28 if $ok; then 29 echo PASS 30 else 31 echo FAIL 32 allok=false 33 fi 34 } 35 36 ok=true 37 allok=true 38 39 unset GOBIN 40 unset GOPATH 41 unset GOROOT 42 43 TEST 'file:line in error messages' 44 # Test that error messages have file:line information at beginning of 45 # the line. Also test issue 4917: that the error is on stderr. 46 d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX) 47 fn=$d/err.go 48 echo "package main" > $fn 49 echo 'import "bar"' >> $fn 50 ./testgo run $fn 2>$d/err.out || true 51 if ! grep -q "^$fn:" $d/err.out; then 52 echo "missing file:line in error message" 53 cat $d/err.out 54 ok=false 55 fi 56 rm -r $d 57 58 # Test local (./) imports. 59 testlocal() { 60 local="$1" 61 TEST local imports $2 '(easy)' 62 ./testgo build -o hello "testdata/$local/easy.go" 63 ./hello >hello.out 64 if ! grep -q '^easysub\.Hello' hello.out; then 65 echo "testdata/$local/easy.go did not generate expected output" 66 cat hello.out 67 ok=false 68 fi 69 70 TEST local imports $2 '(easysub)' 71 ./testgo build -o hello "testdata/$local/easysub/main.go" 72 ./hello >hello.out 73 if ! grep -q '^easysub\.Hello' hello.out; then 74 echo "testdata/$local/easysub/main.go did not generate expected output" 75 cat hello.out 76 ok=false 77 fi 78 79 TEST local imports $2 '(hard)' 80 ./testgo build -o hello "testdata/$local/hard.go" 81 ./hello >hello.out 82 if ! grep -q '^sub\.Hello' hello.out || ! grep -q '^subsub\.Hello' hello.out ; then 83 echo "testdata/$local/hard.go did not generate expected output" 84 cat hello.out 85 ok=false 86 fi 87 88 rm -f hello.out hello 89 90 # Test that go install x.go fails. 91 TEST local imports $2 '(go install should fail)' 92 if ./testgo install "testdata/$local/easy.go" >/dev/null 2>&1; then 93 echo "go install testdata/$local/easy.go succeeded" 94 ok=false 95 fi 96 } 97 98 # Test local imports 99 testlocal local '' 100 101 # Test local imports again, with bad characters in the directory name. 102 bad='#$%:, &()*;<=>?\^{}' 103 rm -rf "testdata/$bad" 104 cp -R testdata/local "testdata/$bad" 105 testlocal "$bad" 'with bad characters in path' 106 rm -rf "testdata/$bad" 107 108 TEST error message for syntax error in test go file says FAIL 109 export GOPATH=$(pwd)/testdata 110 if ./testgo test syntaxerror 2>testdata/err; then 111 echo 'go test syntaxerror succeeded' 112 ok=false 113 elif ! grep FAIL testdata/err >/dev/null; then 114 echo 'go test did not say FAIL:' 115 cat testdata/err 116 ok=false 117 fi 118 rm -f ./testdata/err 119 unset GOPATH 120 121 TEST wildcards do not look in useless directories 122 export GOPATH=$(pwd)/testdata 123 if ./testgo list ... >testdata/err 2>&1; then 124 echo "go list ... succeeded" 125 ok=false 126 elif ! grep badpkg testdata/err >/dev/null; then 127 echo "go list ... failure does not mention badpkg" 128 cat testdata/err 129 ok=false 130 elif ! ./testgo list m... >testdata/err 2>&1; then 131 echo "go list m... failed" 132 ok=false 133 fi 134 rm -rf ./testdata/err 135 unset GOPATH 136 137 # Test tests with relative imports. 138 TEST relative imports '(go test)' 139 if ! ./testgo test ./testdata/testimport; then 140 echo "go test ./testdata/testimport failed" 141 ok=false 142 fi 143 144 # Test installation with relative imports. 145 TEST relative imports '(go test -i)' 146 if ! ./testgo test -i ./testdata/testimport; then 147 echo "go test -i ./testdata/testimport failed" 148 ok=false 149 fi 150 151 # Test tests with relative imports in packages synthesized 152 # from Go files named on the command line. 153 TEST relative imports in command-line package 154 if ! ./testgo test ./testdata/testimport/*.go; then 155 echo "go test ./testdata/testimport/*.go failed" 156 ok=false 157 fi 158 159 TEST version control error message includes correct directory 160 export GOPATH=$(pwd)/testdata/shadow/root1 161 if ./testgo get -u foo 2>testdata/err; then 162 echo "go get -u foo succeeded unexpectedly" 163 ok=false 164 elif ! grep testdata/shadow/root1/src/foo testdata/err >/dev/null; then 165 echo "go get -u error does not mention shadow/root1/src/foo:" 166 cat testdata/err 167 ok=false 168 fi 169 unset GOPATH 170 171 TEST go install fails with no buildable files 172 export GOPATH=$(pwd)/testdata 173 export CGO_ENABLED=0 174 if ./testgo install cgotest 2>testdata/err; then 175 echo "go install cgotest succeeded unexpectedly" 176 elif ! grep 'no buildable Go source files' testdata/err >/dev/null; then 177 echo "go install cgotest did not report 'no buildable Go source files'" 178 cat testdata/err 179 ok=false 180 fi 181 unset CGO_ENABLED 182 unset GOPATH 183 184 # Test that without $GOBIN set, binaries get installed 185 # into the GOPATH bin directory. 186 TEST install into GOPATH 187 rm -rf testdata/bin 188 if ! GOPATH=$(pwd)/testdata ./testgo install go-cmd-test; then 189 echo "go install go-cmd-test failed" 190 ok=false 191 elif ! test -x testdata/bin/go-cmd-test; then 192 echo "go install go-cmd-test did not write to testdata/bin/go-cmd-test" 193 ok=false 194 fi 195 196 TEST package main_test imports archive not binary 197 export GOBIN=$(pwd)/testdata/bin 198 mkdir -p $GOBIN 199 export GOPATH=$(pwd)/testdata 200 touch ./testdata/src/main_test/m.go 201 if ! ./testgo test main_test; then 202 echo "go test main_test failed without install" 203 ok=false 204 elif ! ./testgo install main_test; then 205 echo "go test main_test failed" 206 ok=false 207 elif [ "$(./testgo list -f '{{.Stale}}' main_test)" != false ]; then 208 echo "after go install, main listed as stale" 209 ok=false 210 elif ! ./testgo test main_test; then 211 echo "go test main_test failed after install" 212 ok=false 213 fi 214 rm -rf $GOBIN 215 unset GOBIN 216 217 # And with $GOBIN set, binaries get installed to $GOBIN. 218 TEST install into GOBIN 219 if ! GOBIN=$(pwd)/testdata/bin1 GOPATH=$(pwd)/testdata ./testgo install go-cmd-test; then 220 echo "go install go-cmd-test failed" 221 ok=false 222 elif ! test -x testdata/bin1/go-cmd-test; then 223 echo "go install go-cmd-test did not write to testdata/bin1/go-cmd-test" 224 ok=false 225 fi 226 227 # Without $GOBIN set, installing a program outside $GOPATH should fail 228 # (there is nowhere to install it). 229 TEST install without destination fails 230 if ./testgo install testdata/src/go-cmd-test/helloworld.go 2>testdata/err; then 231 echo "go install testdata/src/go-cmd-test/helloworld.go should have failed, did not" 232 ok=false 233 elif ! grep 'no install location for .go files listed on command line' testdata/err; then 234 echo "wrong error:" 235 cat testdata/err 236 ok=false 237 fi 238 rm -f testdata/err 239 240 # With $GOBIN set, should install there. 241 TEST install to GOBIN '(command-line package)' 242 if ! GOBIN=$(pwd)/testdata/bin1 ./testgo install testdata/src/go-cmd-test/helloworld.go; then 243 echo "go install testdata/src/go-cmd-test/helloworld.go failed" 244 ok=false 245 elif ! test -x testdata/bin1/helloworld; then 246 echo "go install testdata/src/go-cmd-test/helloworld.go did not write testdata/bin1/helloworld" 247 ok=false 248 fi 249 250 TEST godoc installs into GOBIN 251 d=$(mktemp -d -t testgoXXX) 252 export GOPATH=$d 253 mkdir $d/gobin 254 GOBIN=$d/gobin ./testgo get code.google.com/p/go.tools/cmd/godoc 255 if [ ! -x $d/gobin/godoc ]; then 256 echo did not install godoc to '$GOBIN' 257 GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' code.google.com/p/go.tools/cmd/godoc 258 ok=false 259 fi 260 261 TEST godoc installs into GOROOT 262 GOROOT=$(./testgo env GOROOT) 263 rm -f $GOROOT/bin/godoc 264 ./testgo install code.google.com/p/go.tools/cmd/godoc 265 if [ ! -x $GOROOT/bin/godoc ]; then 266 echo did not install godoc to '$GOROOT/bin' 267 ./testgo list -f 'Target: {{.Target}}' code.google.com/p/go.tools/cmd/godoc 268 ok=false 269 fi 270 271 TEST cmd/fix installs into tool 272 GOOS=$(./testgo env GOOS) 273 GOARCH=$(./testgo env GOARCH) 274 rm -f $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix 275 ./testgo install cmd/fix 276 if [ ! -x $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix ]; then 277 echo 'did not install cmd/fix to $GOROOT/pkg/tool' 278 GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' cmd/fix 279 ok=false 280 fi 281 rm -f $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix 282 GOBIN=$d/gobin ./testgo install cmd/fix 283 if [ ! -x $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix ]; then 284 echo 'did not install cmd/fix to $GOROOT/pkg/tool with $GOBIN set' 285 GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' cmd/fix 286 ok=false 287 fi 288 289 TEST gopath program installs into GOBIN 290 mkdir $d/src/progname 291 echo 'package main; func main() {}' >$d/src/progname/p.go 292 GOBIN=$d/gobin ./testgo install progname 293 if [ ! -x $d/gobin/progname ]; then 294 echo 'did not install progname to $GOBIN/progname' 295 ./testgo list -f 'Target: {{.Target}}' cmd/api 296 ok=false 297 fi 298 rm -f $d/gobin/progname $d/bin/progname 299 300 TEST gopath program installs into GOPATH/bin 301 ./testgo install progname 302 if [ ! -x $d/bin/progname ]; then 303 echo 'did not install progname to $GOPATH/bin/progname' 304 ./testgo list -f 'Target: {{.Target}}' progname 305 ok=false 306 fi 307 308 unset GOPATH 309 rm -rf $d 310 311 # Reject relative paths in GOPATH. 312 TEST reject relative paths in GOPATH '(command-line package)' 313 if GOPATH=. ./testgo build testdata/src/go-cmd-test/helloworld.go; then 314 echo 'GOPATH="." go build should have failed, did not' 315 ok=false 316 fi 317 318 TEST reject relative paths in GOPATH 319 if GOPATH=:$(pwd)/testdata:. ./testgo build go-cmd-test; then 320 echo 'GOPATH=":$(pwd)/testdata:." go build should have failed, did not' 321 ok=false 322 fi 323 324 # issue 4104 325 TEST go test with package listed multiple times 326 if [ $(./testgo test fmt fmt fmt fmt fmt | wc -l) -ne 1 ] ; then 327 echo 'go test fmt fmt fmt fmt fmt tested the same package multiple times' 328 ok=false 329 fi 330 331 # ensure that output of 'go list' is consistent between runs 332 TEST go list is consistent 333 ./testgo list std > test_std.list 334 if ! ./testgo list std | cmp -s test_std.list - ; then 335 echo "go list std ordering is inconsistent" 336 ok=false 337 fi 338 rm -f test_std.list 339 340 # issue 4096. Validate the output of unsuccessful go install foo/quxx 341 TEST unsuccessful go install should mention missing package 342 if [ $(./testgo install 'foo/quxx' 2>&1 | grep -c 'cannot find package "foo/quxx" in any of') -ne 1 ] ; then 343 echo 'go install foo/quxx expected error: .*cannot find package "foo/quxx" in any of' 344 ok=false 345 fi 346 # test GOROOT search failure is reported 347 TEST GOROOT search failure reporting 348 if [ $(./testgo install 'foo/quxx' 2>&1 | egrep -c 'foo/quxx \(from \$GOROOT\)$') -ne 1 ] ; then 349 echo 'go install foo/quxx expected error: .*foo/quxx (from $GOROOT)' 350 ok=false 351 fi 352 # test multiple GOPATH entries are reported separately 353 TEST multiple GOPATH entries reported separately 354 if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/./src/foo/quxx') -ne 2 ] ; then 355 echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)\n.*testdata/b/src/foo/quxx' 356 ok=false 357 fi 358 # test (from $GOPATH) annotation is reported for the first GOPATH entry 359 TEST mention GOPATH in first GOPATH entry 360 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 361 echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)' 362 ok=false 363 fi 364 # but not on the second 365 TEST but not the second entry 366 if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/b/src/foo/quxx$') -ne 1 ] ; then 367 echo 'go install foo/quxx expected error: .*testdata/b/src/foo/quxx' 368 ok=false 369 fi 370 # test missing GOPATH is reported 371 TEST missing GOPATH is reported 372 if [ $(GOPATH= ./testgo install 'foo/quxx' 2>&1 | egrep -c '\(\$GOPATH not set\)$') -ne 1 ] ; then 373 echo 'go install foo/quxx expected error: ($GOPATH not set)' 374 ok=false 375 fi 376 377 # issue 4186. go get cannot be used to download packages to $GOROOT 378 # Test that without GOPATH set, go get should fail 379 TEST without GOPATH, go get fails 380 d=$(mktemp -d -t testgoXXX) 381 mkdir -p $d/src/pkg 382 if GOPATH= GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch ; then 383 echo 'go get code.google.com/p/go.codereview/cmd/hgpatch should not succeed with $GOPATH unset' 384 ok=false 385 fi 386 rm -rf $d 387 388 # Test that with GOPATH=$GOROOT, go get should fail 389 TEST with GOPATH=GOROOT, go get fails 390 d=$(mktemp -d -t testgoXXX) 391 mkdir -p $d/src/pkg 392 if GOPATH=$d GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch ; then 393 echo 'go get code.google.com/p/go.codereview/cmd/hgpatch should not succeed with GOPATH=$GOROOT' 394 ok=false 395 fi 396 rm -rf $d 397 398 TEST ldflags arguments with spaces '(issue 3941)' 399 d=$(mktemp -d -t testgoXXX) 400 cat >$d/main.go<<EOF 401 package main 402 var extern string 403 func main() { 404 println(extern) 405 } 406 EOF 407 ./testgo run -ldflags '-X main.extern "hello world"' $d/main.go 2>hello.out 408 if ! grep -q '^hello world' hello.out; then 409 echo "ldflags -X main.extern 'hello world' failed. Output:" 410 cat hello.out 411 ok=false 412 fi 413 rm -rf $d hello.out 414 415 TEST go test -cpuprofile leaves binary behind 416 ./testgo test -cpuprofile strings.prof strings || ok=false 417 if [ ! -x strings.test ]; then 418 echo "go test -cpuprofile did not create strings.test" 419 ok=false 420 fi 421 rm -f strings.prof strings.test 422 423 TEST symlinks do not confuse go list '(issue 4568)' 424 old=$(pwd) 425 tmp=$(cd /tmp && pwd -P) 426 d=$(TMPDIR=$tmp mktemp -d -t testgoXXX) 427 mkdir -p $d/src 428 ( 429 ln -s $d $d/src/dir1 430 cd $d/src 431 echo package p >dir1/p.go 432 export GOPATH=$d 433 if [ "$($old/testgo list -f '{{.Root}}' dir1)" != "$d" ]; then 434 echo Confused by symlinks. 435 echo "Package in current directory $(pwd) should have Root $d" 436 env|grep WD 437 $old/testgo list -json . dir1 438 touch $d/failed 439 fi 440 ) 441 if [ -f $d/failed ]; then 442 ok=false 443 fi 444 rm -rf $d 445 446 TEST 'install with tags (issue 4515)' 447 d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX) 448 mkdir -p $d/src/example/a $d/src/example/b $d/bin 449 cat >$d/src/example/a/main.go <<EOF 450 package main 451 func main() {} 452 EOF 453 cat >$d/src/example/b/main.go <<EOF 454 // +build mytag 455 456 package main 457 func main() {} 458 EOF 459 GOPATH=$d ./testgo install -tags mytag example/a example/b || ok=false 460 if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then 461 echo go install example/a example/b did not install binaries 462 ok=false 463 fi 464 rm -f $d/bin/* 465 GOPATH=$d ./testgo install -tags mytag example/... || ok=false 466 if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then 467 echo go install example/... did not install binaries 468 ok=false 469 fi 470 rm -f $d/bin/*go 471 export GOPATH=$d 472 if [ "$(./testgo list -tags mytag example/b...)" != "example/b" ]; then 473 echo go list example/b did not find example/b 474 ok=false 475 fi 476 unset GOPATH 477 rm -rf $d 478 479 TEST case collisions '(issue 4773)' 480 d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX) 481 export GOPATH=$d 482 mkdir -p $d/src/example/{a/pkg,a/Pkg,b} 483 cat >$d/src/example/a/a.go <<EOF 484 package p 485 import ( 486 _ "example/a/pkg" 487 _ "example/a/Pkg" 488 ) 489 EOF 490 cat >$d/src/example/a/pkg/pkg.go <<EOF 491 package pkg 492 EOF 493 cat >$d/src/example/a/Pkg/pkg.go <<EOF 494 package pkg 495 EOF 496 if ./testgo list example/a 2>$d/out; then 497 echo go list example/a should have failed, did not. 498 ok=false 499 elif ! grep "case-insensitive import collision" $d/out >/dev/null; then 500 echo go list example/a did not report import collision. 501 ok=false 502 fi 503 cat >$d/src/example/b/file.go <<EOF 504 package b 505 EOF 506 cat >$d/src/example/b/FILE.go <<EOF 507 package b 508 EOF 509 if [ $(ls $d/src/example/b | wc -l) = 2 ]; then 510 # case-sensitive file system, let directory read find both files 511 args="example/b" 512 else 513 # case-insensitive file system, list files explicitly on command line. 514 args="$d/src/example/b/file.go $d/src/example/b/FILE.go" 515 fi 516 if ./testgo list $args 2>$d/out; then 517 echo go list example/b should have failed, did not. 518 ok=false 519 elif ! grep "case-insensitive file name collision" $d/out >/dev/null; then 520 echo go list example/b did not report file name collision. 521 ok=false 522 fi 523 524 TEST go get cover 525 ./testgo get code.google.com/p/go.tools/cmd/cover || ok=false 526 527 unset GOPATH 528 rm -rf $d 529 530 TEST shadowing logic 531 export GOPATH=$(pwd)/testdata/shadow/root1:$(pwd)/testdata/shadow/root2 532 533 # The math in root1 is not "math" because the standard math is. 534 cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root1/src/math) 535 if [ "$cdir" != "(_$(pwd)/testdata/shadow/root1/src/math) ($GOROOT/src/pkg/math)" ]; then 536 echo shadowed math is not shadowed: "$cdir" 537 ok=false 538 fi 539 540 # The foo in root1 is "foo". 541 cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root1/src/foo) 542 if [ "$cdir" != "(foo) ()" ]; then 543 echo unshadowed foo is shadowed: "$cdir" 544 ok=false 545 fi 546 547 # The foo in root2 is not "foo" because the foo in root1 got there first. 548 cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root2/src/foo) 549 if [ "$cdir" != "(_$(pwd)/testdata/shadow/root2/src/foo) ($(pwd)/testdata/shadow/root1/src/foo)" ]; then 550 echo shadowed foo is not shadowed: "$cdir" 551 ok=false 552 fi 553 554 # The error for go install should mention the conflicting directory. 555 err=$(! ./testgo install ./testdata/shadow/root2/src/foo 2>&1) 556 if [ "$err" != "go install: no install location for $(pwd)/testdata/shadow/root2/src/foo: hidden by $(pwd)/testdata/shadow/root1/src/foo" ]; then 557 echo wrong shadowed install error: "$err" 558 ok=false 559 fi 560 561 # Only succeeds if source order is preserved. 562 TEST source file name order preserved 563 ./testgo test testdata/example[12]_test.go || ok=false 564 565 # Check that coverage analysis works at all. 566 # Don't worry about the exact numbers but require not 0.0%. 567 checkcoverage() { 568 if grep '[^0-9]0\.0%' testdata/cover.txt >/dev/null; then 569 echo 'some coverage results are 0.0%' 570 ok=false 571 fi 572 cat testdata/cover.txt 573 rm -f testdata/cover.txt 574 } 575 576 TEST coverage runs 577 ./testgo test -short -coverpkg=strings strings regexp >testdata/cover.txt 2>&1 || ok=false 578 ./testgo test -short -cover strings math regexp >>testdata/cover.txt 2>&1 || ok=false 579 checkcoverage 580 581 # Check that coverage analysis uses set mode. 582 TEST coverage uses set mode 583 if ./testgo test -short -cover encoding/binary -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then 584 if ! grep -q 'mode: set' testdata/cover.out; then 585 ok=false 586 fi 587 checkcoverage 588 else 589 ok=false 590 fi 591 rm -f testdata/cover.out testdata/cover.txt 592 593 TEST coverage uses atomic mode for -race. 594 if ./testgo test -short -race -cover encoding/binary -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then 595 if ! grep -q 'mode: atomic' testdata/cover.out; then 596 ok=false 597 fi 598 checkcoverage 599 else 600 ok=false 601 fi 602 rm -f testdata/cover.out 603 604 TEST coverage uses actual setting to override even for -race. 605 if ./testgo test -short -race -cover encoding/binary -covermode=count -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then 606 if ! grep -q 'mode: count' testdata/cover.out; then 607 ok=false 608 fi 609 checkcoverage 610 else 611 ok=false 612 fi 613 rm -f testdata/cover.out 614 615 TEST coverage with cgo 616 d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX) 617 ./testgo test -short -cover ./testdata/cgocover >testdata/cover.txt 2>&1 || ok=false 618 checkcoverage 619 620 TEST cgo depends on syscall 621 rm -rf $GOROOT/pkg/*_race 622 d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX) 623 export GOPATH=$d 624 mkdir -p $d/src/foo 625 echo ' 626 package foo 627 //#include <stdio.h> 628 import "C" 629 ' >$d/src/foo/foo.go 630 ./testgo build -race foo || ok=false 631 rm -rf $d 632 unset GOPATH 633 634 TEST cgo shows full path names 635 d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX) 636 export GOPATH=$d 637 mkdir -p $d/src/x/y/dirname 638 echo ' 639 package foo 640 import "C" 641 func f() { 642 ' >$d/src/x/y/dirname/foo.go 643 if ./testgo build x/y/dirname >$d/err 2>&1; then 644 echo build succeeded unexpectedly. 645 ok=false 646 elif ! grep x/y/dirname $d/err >/dev/null; then 647 echo error did not use full path. 648 cat $d/err 649 ok=false 650 fi 651 rm -rf $d 652 unset GOPATH 653 654 TEST 'cgo handles -Wl,$ORIGIN' 655 d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX) 656 export GOPATH=$d 657 mkdir -p $d/src/origin 658 echo ' 659 package origin 660 // #cgo !darwin LDFLAGS: -Wl,-rpath -Wl,$ORIGIN 661 // void f(void) {} 662 import "C" 663 664 func f() { C.f() } 665 ' >$d/src/origin/origin.go 666 if ! ./testgo build origin; then 667 echo build failed 668 ok=false 669 fi 670 rm -rf $d 671 unset GOPATH 672 673 TEST 'Issue 6480: "go test -c -test.bench=XXX fmt" should not hang' 674 if ! ./testgo test -c -test.bench=XXX fmt; then 675 echo build test failed 676 ok=false 677 fi 678 rm -f fmt.test 679 680 TEST 'Issue 7573: cmd/cgo: undefined reference when linking a C-library using gccgo' 681 d=$(mktemp -d -t testgoXXX) 682 export GOPATH=$d 683 mkdir -p $d/src/cgoref 684 ldflags="-L alibpath -lalib" 685 echo " 686 package main 687 // #cgo LDFLAGS: $ldflags 688 // void f(void) {} 689 import \"C\" 690 691 func main() { C.f() } 692 " >$d/src/cgoref/cgoref.go 693 go_cmds="$(./testgo build -n -compiler gccgo cgoref 2>&1 1>/dev/null)" 694 ldflags_count="$(echo "$go_cmds" | egrep -c "^gccgo.*$(echo $ldflags | sed -e 's/-/\\-/g')" || true)" 695 if [ "$ldflags_count" -lt 1 ]; then 696 echo "No Go-inline "#cgo LDFLAGS:" (\"$ldflags\") passed to gccgo linking stage." 697 ok=false 698 fi 699 rm -rf $d 700 unset ldflags_count 701 unset go_cmds 702 unset ldflags 703 unset GOPATH 704 705 TEST list template can use context function 706 if ! ./testgo list -f "GOARCH: {{context.GOARCH}}"; then 707 echo unable to use context in list template 708 ok=false 709 fi 710 711 TEST 'Issue 7108: cmd/go: "go test" should fail if package does not build' 712 export GOPATH=$(pwd)/testdata 713 if ./testgo test notest >/dev/null 2>&1; then 714 echo 'go test notest succeeded, but should fail' 715 ok=false 716 fi 717 unset GOPATH 718 719 TEST 'Issue 6844: cmd/go: go test -a foo does not rebuild regexp' 720 if ! ./testgo test -x -a -c testdata/dep_test.go 2>deplist; then 721 echo "go test -x -a -c testdata/dep_test.go failed" 722 ok=false 723 elif ! grep -q regexp deplist; then 724 echo "go test -x -a -c testdata/dep_test.go did not rebuild regexp" 725 ok=false 726 fi 727 rm -f deplist 728 rm -f deps.test 729 730 TEST list template can use context function 731 if ! ./testgo list -f "GOARCH: {{context.GOARCH}}"; then 732 echo unable to use context in list template 733 ok=false 734 fi 735 736 TEST build -i installs dependencies 737 d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX) 738 export GOPATH=$d 739 mkdir -p $d/src/x/y/foo $d/src/x/y/bar 740 echo ' 741 package foo 742 func F() {} 743 ' >$d/src/x/y/foo/foo.go 744 echo ' 745 package bar 746 import "x/y/foo" 747 func F() { foo.F() } 748 ' >$d/src/x/y/bar/bar.go 749 if ! ./testgo build -v -i x/y/bar &> $d/err; then 750 echo build -i failed 751 cat $d/err 752 ok=false 753 elif ! grep x/y/foo $d/err >/dev/null; then 754 echo first build -i did not build x/y/foo 755 cat $d/err 756 ok=false 757 fi 758 if ! ./testgo build -v -i x/y/bar &> $d/err; then 759 echo second build -i failed 760 cat $d/err 761 ok=false 762 elif grep x/y/foo $d/err >/dev/null; then 763 echo second build -i built x/y/foo 764 cat $d/err 765 ok=false 766 fi 767 rm -rf $d 768 unset GOPATH 769 770 TEST 'go build in test-only directory fails with a good error' 771 if ./testgo build ./testdata/testonly 2>testdata/err.out; then 772 echo "go build ./testdata/testonly succeeded, should have failed" 773 ok=false 774 elif ! grep 'no buildable Go' testdata/err.out >/dev/null; then 775 echo "go build ./testdata/testonly produced unexpected error:" 776 cat testdata/err.out 777 ok=false 778 fi 779 rm -f testdata/err.out 780 781 TEST 'go test detects test-only import cycles' 782 export GOPATH=$(pwd)/testdata 783 if ./testgo test -c testcycle/p3 2>testdata/err.out; then 784 echo "go test testcycle/p3 succeeded, should have failed" 785 ok=false 786 elif ! grep 'import cycle not allowed in test' testdata/err.out >/dev/null; then 787 echo "go test testcycle/p3 produced unexpected error:" 788 cat testdata/err.out 789 ok=false 790 fi 791 rm -f testdata/err.out 792 unset GOPATH 793 794 TEST 'go test foo_test.go works' 795 if ! ./testgo test testdata/standalone_test.go; then 796 echo "go test testdata/standalone_test.go failed" 797 ok=false 798 fi 799 800 TEST 'go test xtestonly works' 801 export GOPATH=$(pwd)/testdata 802 ./testgo clean -i xtestonly 803 if ! ./testgo test xtestonly >/dev/null; then 804 echo "go test xtestonly failed" 805 ok=false 806 fi 807 unset GOPATH 808 809 810 # clean up 811 if $started; then stop; fi 812 rm -rf testdata/bin testdata/bin1 813 rm -f testgo 814 815 if $allok; then 816 echo PASS 817 else 818 echo FAIL 819 exit 1 820 fi