github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/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 -tags testgo -o testgo
     8  go() {
     9  	echo TEST ERROR: ran go, not testgo: go "$@" >&2
    10  	exit 2
    11  }
    12  
    13  started=false
    14  testdesc=""
    15  nl="
    16  "
    17  TEST() {
    18  	if $started; then
    19  		stop
    20  	fi
    21  	echo TEST: "$@"
    22  	testdesc="$@"
    23  	started=true
    24  	ok=true
    25  }
    26  stop() {
    27  	if ! $started; then
    28  		echo TEST ERROR: stop missing start >&2
    29  		exit 2
    30  	fi
    31  	started=false
    32  	if $ok; then
    33  		echo PASS
    34  	else
    35  		echo FAIL
    36  		testfail="$testfail	$testdesc$nl"
    37  		allok=false
    38  	fi
    39  }
    40  
    41  ok=true
    42  allok=true
    43  
    44  unset GOBIN
    45  unset GOPATH
    46  unset GOROOT
    47  
    48  TEST 'file:line in error messages'
    49  # Test that error messages have file:line information at beginning of
    50  # the line. Also test issue 4917: that the error is on stderr.
    51  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
    52  fn=$d/err.go
    53  echo "package main" > $fn
    54  echo 'import "bar"' >> $fn
    55  ./testgo run $fn 2>$d/err.out || true
    56  if ! grep -q "^$fn:" $d/err.out; then
    57  	echo "missing file:line in error message"
    58  	cat $d/err.out
    59  	ok=false
    60  fi
    61  rm -r $d
    62  
    63  TEST 'program name in crash messages'
    64  linker=$(./testgo env GOCHAR)l
    65  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
    66  ./testgo build -ldflags -crash_for_testing $(./testgo env GOROOT)/test/helloworld.go 2>$d/err.out || true
    67  if ! grep -q "/tool/.*/$linker" $d/err.out; then
    68  	echo "missing linker name in error message"
    69  	cat $d/err.out
    70  	ok=false
    71  fi
    72  rm -r $d
    73  
    74  TEST broken tests without Test functions all fail
    75  d=$(mktemp -d -t testgoXXX)
    76  ./testgo test ./testdata/src/badtest/... >$d/err 2>&1 || true
    77  if grep -q '^ok' $d/err; then
    78  	echo test passed unexpectedly:
    79  	grep '^ok' $d/err
    80  	ok=false
    81  elif ! grep -q 'FAIL.*badtest/badexec' $d/err || ! grep -q 'FAIL.*badtest/badsyntax' $d/err || ! grep -q 'FAIL.*badtest/badvar' $d/err; then
    82  	echo test did not run everything
    83  	cat $d/err
    84  	ok=false
    85  fi
    86  rm -rf $d
    87  
    88  TEST 'go build -a in dev branch'
    89  ./testgo install math || ok=false # should be up to date already but just in case
    90  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
    91  if ! TESTGO_IS_GO_RELEASE=0 ./testgo build -v -a math 2>$d/err.out; then
    92  	cat $d/err.out
    93  	ok=false
    94  elif ! grep -q runtime $d/err.out; then
    95  	echo "testgo build -a math in dev branch DID NOT build runtime, but should have"
    96  	cat $d/err.out
    97  	ok=false
    98  fi
    99  rm -r $d
   100  
   101  TEST 'go build -a in release branch'
   102  ./testgo install math || ok=false # should be up to date already but just in case
   103  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   104  if ! TESTGO_IS_GO_RELEASE=1 ./testgo build -v -a math 2>$d/err.out; then
   105  	cat $d/err.out
   106  	ok=false
   107  elif grep -q runtime $d/err.out; then
   108  	echo "testgo build -a math in dev branch DID build runtime, but should NOT have"
   109  	cat $d/err.out
   110  	ok=false
   111  fi
   112  rm -r $d
   113  
   114  # Test local (./) imports.
   115  testlocal() {
   116  	local="$1"
   117  	TEST local imports $2 '(easy)'
   118  	./testgo build -o hello "testdata/$local/easy.go" || ok=false
   119  	./hello >hello.out || ok=false
   120  	if ! grep -q '^easysub\.Hello' hello.out; then
   121  		echo "testdata/$local/easy.go did not generate expected output"
   122  		cat hello.out
   123  		ok=false
   124  	fi
   125  	
   126  	TEST local imports $2 '(easysub)'
   127  	./testgo build -o hello "testdata/$local/easysub/main.go" || ok=false
   128  	./hello >hello.out || ok=false
   129  	if ! grep -q '^easysub\.Hello' hello.out; then
   130  		echo "testdata/$local/easysub/main.go did not generate expected output"
   131  		cat hello.out
   132  		ok=false
   133  	fi
   134  	
   135  	TEST local imports $2 '(hard)'
   136  	./testgo build -o hello "testdata/$local/hard.go" || ok=false
   137  	./hello >hello.out || ok=false
   138  	if ! grep -q '^sub\.Hello' hello.out || ! grep -q '^subsub\.Hello' hello.out ; then
   139  		echo "testdata/$local/hard.go did not generate expected output"
   140  		cat hello.out
   141  		ok=false
   142  	fi
   143  	
   144  	rm -f hello.out hello
   145  	
   146  	# Test that go install x.go fails.
   147  	TEST local imports $2 '(go install should fail)'
   148  	if ./testgo install "testdata/$local/easy.go" >/dev/null 2>&1; then
   149  		echo "go install testdata/$local/easy.go succeeded"
   150  		ok=false
   151  	fi
   152  }
   153  
   154  # Test local imports
   155  testlocal local ''
   156  
   157  # Test local imports again, with bad characters in the directory name.
   158  bad='#$%:, &()*;<=>?\^{}'
   159  rm -rf "testdata/$bad"
   160  cp -R testdata/local "testdata/$bad"
   161  testlocal "$bad" 'with bad characters in path'
   162  rm -rf "testdata/$bad"
   163  
   164  TEST 'internal packages in $GOROOT are respected'
   165  if ./testgo build -v ./testdata/testinternal >testdata/std.out 2>&1; then
   166  	echo "go build ./testdata/testinternal succeeded incorrectly"
   167  	ok=false
   168  elif ! grep 'use of internal package not allowed' testdata/std.out >/dev/null; then
   169  	echo "wrong error message for testdata/testinternal"
   170  	cat std.out
   171  	ok=false
   172  fi
   173  
   174  TEST 'internal packages outside $GOROOT are not respected'
   175  if ! ./testgo build -v ./testdata/testinternal2; then
   176  	echo "go build ./testdata/testinternal2 failed"
   177  	ok=false
   178  fi
   179  
   180  # Test that 'go get -u' reports moved packages.
   181  testmove() {
   182  	vcs=$1
   183  	url=$2
   184  	base=$3
   185  	config=$4
   186  
   187  	TEST go get -u notices $vcs package that moved
   188  	d=$(mktemp -d -t testgoXXX)
   189  	mkdir -p $d/src
   190  	if ! GOPATH=$d ./testgo get -d $url; then
   191  		echo 'go get -d $url failed'
   192  		ok=false
   193  	elif ! GOPATH=$d ./testgo get -d -u $url; then
   194  		echo 'go get -d -u $url failed'
   195  		ok=false
   196  	else
   197  		set +e
   198  		case "$vcs" in
   199  		svn)
   200  			# SVN doesn't believe in text files so we can't just edit the config.
   201  			# Check out a different repo into the wrong place.
   202  			rm -rf $d/src/code.google.com/p/rsc-svn
   203  			GOPATH=$d ./testgo get -d -u code.google.com/p/rsc-svn2/trunk
   204  			mv $d/src/code.google.com/p/rsc-svn2 $d/src/code.google.com/p/rsc-svn
   205  			;;
   206  		*)
   207  			echo '1,$s;'"$base"';'"$base"'XXX;
   208  w
   209  q' | ed $d/src/$config >/dev/null 2>&1
   210  		esac
   211  		set -e
   212  
   213  		if GOPATH=$d ./testgo get -d -u $url 2>$d/err; then
   214  			echo "go get -d -u $url succeeded with wrong remote repo"
   215  			cat $d/err
   216  			ok=false
   217  		elif ! grep 'should be from' $d/err >/dev/null; then
   218  			echo "go get -d -u $url failed for wrong reason"
   219  			cat $d/err
   220  			ok=false
   221  		fi
   222  		
   223  		if GOPATH=$d ./testgo get -d -f -u $url 2>$d/err; then
   224  			echo "go get -d -u $url succeeded with wrong remote repo"
   225  			cat $d/err
   226  			ok=false
   227  		elif ! egrep -i 'validating server certificate|not found' $d/err >/dev/null; then
   228  			echo "go get -d -f -u $url failed for wrong reason"
   229  			cat $d/err
   230  			ok=false
   231  		fi
   232  	fi
   233  	rm -rf $d
   234  }
   235  
   236  testmove hg rsc.io/x86/x86asm x86 rsc.io/x86/.hg/hgrc
   237  testmove git rsc.io/pdf pdf rsc.io/pdf/.git/config
   238  testmove svn code.google.com/p/rsc-svn/trunk - -
   239  
   240  export GOPATH=$(pwd)/testdata/importcom
   241  TEST 'import comment - match'
   242  if ! ./testgo build ./testdata/importcom/works.go; then
   243  	echo 'go build ./testdata/importcom/works.go failed'
   244  	ok=false
   245  fi
   246  TEST 'import comment - mismatch'
   247  if ./testgo build ./testdata/importcom/wrongplace.go 2>testdata/err; then
   248  	echo 'go build ./testdata/importcom/wrongplace.go suceeded'
   249  	ok=false
   250  elif ! grep 'wrongplace expects import "my/x"' testdata/err >/dev/null; then
   251  	echo 'go build did not mention incorrect import:'
   252  	cat testdata/err
   253  	ok=false
   254  fi
   255  TEST 'import comment - syntax error'
   256  if ./testgo build ./testdata/importcom/bad.go 2>testdata/err; then
   257  	echo 'go build ./testdata/importcom/bad.go suceeded'
   258  	ok=false
   259  elif ! grep 'cannot parse import comment' testdata/err >/dev/null; then
   260  	echo 'go build did not mention syntax error:'
   261  	cat testdata/err
   262  	ok=false
   263  fi
   264  TEST 'import comment - conflict'
   265  if ./testgo build ./testdata/importcom/conflict.go 2>testdata/err; then
   266  	echo 'go build ./testdata/importcom/conflict.go suceeded'
   267  	ok=false
   268  elif ! grep 'found import comments' testdata/err >/dev/null; then
   269  	echo 'go build did not mention comment conflict:'
   270  	cat testdata/err
   271  	ok=false
   272  fi
   273  rm -f ./testdata/err
   274  unset GOPATH
   275  
   276  export GOPATH=$(pwd)/testdata/src
   277  TEST disallowed C source files
   278  export GOPATH=$(pwd)/testdata
   279  if ./testgo build badc 2>testdata/err; then
   280  	echo 'go build badc succeeded'
   281  	ok=false
   282  elif ! grep 'C source files not allowed' testdata/err >/dev/null; then
   283  	echo 'go test did not say C source files not allowed:'
   284  	cat testdata/err
   285  	ok=false
   286  fi
   287  rm -f ./testdata/err
   288  unset GOPATH
   289  
   290  TEST error message for syntax error in test go file says FAIL
   291  export GOPATH=$(pwd)/testdata
   292  if ./testgo test syntaxerror 2>testdata/err; then
   293  	echo 'go test syntaxerror succeeded'
   294  	ok=false
   295  elif ! grep FAIL testdata/err >/dev/null; then
   296  	echo 'go test did not say FAIL:'
   297  	cat testdata/err
   298  	ok=false
   299  fi
   300  rm -f ./testdata/err
   301  unset GOPATH
   302  
   303  TEST wildcards do not look in useless directories
   304  export GOPATH=$(pwd)/testdata
   305  if ./testgo list ... >testdata/err 2>&1; then
   306  	echo "go list ... succeeded"
   307  	ok=false
   308  elif ! grep badpkg testdata/err >/dev/null; then
   309  	echo "go list ... failure does not mention badpkg"
   310  	cat testdata/err
   311  	ok=false
   312  elif ! ./testgo list m... >testdata/err 2>&1; then
   313  	echo "go list m... failed"
   314  	ok=false
   315  fi
   316  rm -rf ./testdata/err
   317  unset GOPATH
   318  
   319  # Test tests with relative imports.
   320  TEST relative imports '(go test)'
   321  if ! ./testgo test ./testdata/testimport; then
   322  	echo "go test ./testdata/testimport failed"
   323  	ok=false
   324  fi
   325  
   326  # Test installation with relative imports.
   327  TEST relative imports '(go test -i)'
   328  if ! ./testgo test -i ./testdata/testimport; then
   329      echo "go test -i ./testdata/testimport failed"
   330      ok=false
   331  fi
   332  
   333  # Test tests with relative imports in packages synthesized
   334  # from Go files named on the command line.
   335  TEST relative imports in command-line package
   336  if ! ./testgo test ./testdata/testimport/*.go; then
   337  	echo "go test ./testdata/testimport/*.go failed"
   338  	ok=false
   339  fi
   340  
   341  TEST version control error message includes correct directory
   342  export GOPATH=$(pwd)/testdata/shadow/root1
   343  if ./testgo get -u foo 2>testdata/err; then
   344  	echo "go get -u foo succeeded unexpectedly"
   345  	ok=false
   346  elif ! grep testdata/shadow/root1/src/foo testdata/err >/dev/null; then
   347  	echo "go get -u error does not mention shadow/root1/src/foo:"
   348  	cat testdata/err
   349  	ok=false
   350  fi
   351  unset GOPATH
   352  
   353  TEST go install fails with no buildable files
   354  export GOPATH=$(pwd)/testdata
   355  export CGO_ENABLED=0
   356  if ./testgo install cgotest 2>testdata/err; then
   357  	echo "go install cgotest succeeded unexpectedly"
   358  elif ! grep 'no buildable Go source files' testdata/err >/dev/null; then
   359  	echo "go install cgotest did not report 'no buildable Go source files'"
   360  	cat testdata/err
   361  	ok=false
   362  fi
   363  unset CGO_ENABLED
   364  unset GOPATH
   365  
   366  # Test that without $GOBIN set, binaries get installed
   367  # into the GOPATH bin directory.
   368  TEST install into GOPATH
   369  rm -rf testdata/bin
   370  if ! GOPATH=$(pwd)/testdata ./testgo install go-cmd-test; then
   371  	echo "go install go-cmd-test failed"
   372  	ok=false
   373  elif ! test -x testdata/bin/go-cmd-test; then
   374  	echo "go install go-cmd-test did not write to testdata/bin/go-cmd-test"
   375  	ok=false
   376  fi
   377  
   378  TEST package main_test imports archive not binary
   379  export GOBIN=$(pwd)/testdata/bin
   380  mkdir -p $GOBIN
   381  export GOPATH=$(pwd)/testdata
   382  touch ./testdata/src/main_test/m.go
   383  if ! ./testgo test main_test; then
   384  	echo "go test main_test failed without install"
   385  	ok=false
   386  elif ! ./testgo install main_test; then
   387  	echo "go test main_test failed"
   388  	ok=false
   389  elif [ "$(./testgo list -f '{{.Stale}}' main_test)" != false ]; then
   390  	echo "after go install, main listed as stale"
   391  	ok=false
   392  elif ! ./testgo test main_test; then
   393  	echo "go test main_test failed after install"
   394  	ok=false
   395  fi
   396  rm -rf $GOBIN
   397  unset GOBIN
   398  
   399  # And with $GOBIN set, binaries get installed to $GOBIN.
   400  TEST install into GOBIN
   401  if ! GOBIN=$(pwd)/testdata/bin1 GOPATH=$(pwd)/testdata ./testgo install go-cmd-test; then
   402  	echo "go install go-cmd-test failed"
   403  	ok=false
   404  elif ! test -x testdata/bin1/go-cmd-test; then
   405  	echo "go install go-cmd-test did not write to testdata/bin1/go-cmd-test"
   406  	ok=false
   407  fi
   408  
   409  # Without $GOBIN set, installing a program outside $GOPATH should fail
   410  # (there is nowhere to install it).
   411  TEST install without destination fails
   412  if ./testgo install testdata/src/go-cmd-test/helloworld.go 2>testdata/err; then
   413  	echo "go install testdata/src/go-cmd-test/helloworld.go should have failed, did not"
   414  	ok=false
   415  elif ! grep 'no install location for .go files listed on command line' testdata/err; then
   416  	echo "wrong error:"
   417  	cat testdata/err
   418  	ok=false
   419  fi
   420  rm -f testdata/err
   421  
   422  # With $GOBIN set, should install there.
   423  TEST install to GOBIN '(command-line package)'
   424  if ! GOBIN=$(pwd)/testdata/bin1 ./testgo install testdata/src/go-cmd-test/helloworld.go; then
   425  	echo "go install testdata/src/go-cmd-test/helloworld.go failed"
   426  	ok=false
   427  elif ! test -x testdata/bin1/helloworld; then
   428  	echo "go install testdata/src/go-cmd-test/helloworld.go did not write testdata/bin1/helloworld"
   429  	ok=false
   430  fi
   431  
   432  TEST godoc installs into GOBIN
   433  d=$(mktemp -d -t testgoXXX)
   434  export GOPATH=$d
   435  mkdir $d/gobin
   436  GOBIN=$d/gobin ./testgo get golang.org/x/tools/cmd/godoc || ok=false
   437  if [ ! -x $d/gobin/godoc ]; then
   438  	echo did not install godoc to '$GOBIN'
   439  	GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' golang.org/x/tools/cmd/godoc || true
   440  	ok=false
   441  fi
   442  
   443  TEST godoc installs into GOROOT
   444  GOROOT=$(./testgo env GOROOT)
   445  rm -f $GOROOT/bin/godoc
   446  ./testgo install golang.org/x/tools/cmd/godoc || ok=false
   447  if [ ! -x $GOROOT/bin/godoc ]; then
   448  	echo did not install godoc to '$GOROOT/bin'
   449  	./testgo list -f 'Target: {{.Target}}' golang.org/x/tools/cmd/godoc || true
   450  	ok=false
   451  fi
   452  
   453  TEST cmd/fix installs into tool
   454  GOOS=$(./testgo env GOOS)
   455  GOARCH=$(./testgo env GOARCH)
   456  rm -f $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix
   457  ./testgo install cmd/fix || ok=false
   458  if [ ! -x $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix ]; then
   459  	echo 'did not install cmd/fix to $GOROOT/pkg/tool'
   460  	GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' cmd/fix || true
   461  	ok=false
   462  fi
   463  rm -f $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix
   464  GOBIN=$d/gobin ./testgo install cmd/fix || ok=false
   465  if [ ! -x $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix ]; then
   466  	echo 'did not install cmd/fix to $GOROOT/pkg/tool with $GOBIN set'
   467  	GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' cmd/fix || true
   468  	ok=false
   469  fi
   470  
   471  TEST gopath program installs into GOBIN
   472  mkdir $d/src/progname
   473  echo 'package main; func main() {}' >$d/src/progname/p.go
   474  GOBIN=$d/gobin ./testgo install progname || ok=false
   475  if [ ! -x $d/gobin/progname ]; then
   476  	echo 'did not install progname to $GOBIN/progname'
   477  	./testgo list -f 'Target: {{.Target}}' cmd/api || true
   478  	ok=false
   479  fi
   480  rm -f $d/gobin/progname $d/bin/progname
   481  
   482  TEST gopath program installs into GOPATH/bin
   483  ./testgo install progname || ok=false
   484  if [ ! -x $d/bin/progname ]; then
   485  	echo 'did not install progname to $GOPATH/bin/progname'
   486  	./testgo list -f 'Target: {{.Target}}' progname || true
   487  	ok=false
   488  fi
   489  
   490  unset GOPATH
   491  rm -rf $d
   492  
   493  # Reject relative paths in GOPATH.
   494  TEST reject relative paths in GOPATH '(command-line package)'
   495  if GOPATH=. ./testgo build testdata/src/go-cmd-test/helloworld.go; then
   496      echo 'GOPATH="." go build should have failed, did not'
   497      ok=false
   498  fi
   499  
   500  TEST reject relative paths in GOPATH 
   501  if GOPATH=:$(pwd)/testdata:. ./testgo build go-cmd-test; then
   502      echo 'GOPATH=":$(pwd)/testdata:." go build should have failed, did not'
   503      ok=false
   504  fi
   505  
   506  # issue 4104
   507  TEST go test with package listed multiple times
   508  if [ $(./testgo test fmt fmt fmt fmt fmt | wc -l) -ne 1 ] ; then
   509      echo 'go test fmt fmt fmt fmt fmt tested the same package multiple times'
   510      ok=false
   511  fi
   512  
   513  TEST go list has a consistent order
   514  ./testgo list std > test_std.list || ok=false
   515  if ! ./testgo list std | cmp -s test_std.list - ; then
   516  	echo "go list std ordering is inconsistent"
   517  	ok=false
   518  fi
   519  rm -f test_std.list
   520  
   521  TEST go list std does not include commands
   522  if ./testgo list std | grep cmd/; then
   523  	echo "go list std shows commands"
   524  	ok=false
   525  fi
   526  
   527  TEST go list cmd only shows commands
   528  if ./testgo list cmd | grep -v 'cmd/'; then
   529  	echo "go list cmd shows non-commands"
   530  	ok=false
   531  fi
   532  
   533  # issue 4096. Validate the output of unsuccessful go install foo/quxx 
   534  TEST unsuccessful go install should mention missing package
   535  if [ $(./testgo install 'foo/quxx' 2>&1 | grep -c 'cannot find package "foo/quxx" in any of') -ne 1 ] ; then
   536  	echo 'go install foo/quxx expected error: .*cannot find package "foo/quxx" in any of'
   537  	ok=false
   538  fi 
   539  # test GOROOT search failure is reported
   540  TEST GOROOT search failure reporting
   541  if [ $(./testgo install 'foo/quxx' 2>&1 | egrep -c 'foo/quxx \(from \$GOROOT\)$') -ne 1 ] ; then
   542          echo 'go install foo/quxx expected error: .*foo/quxx (from $GOROOT)'
   543          ok=false
   544  fi
   545  # test multiple GOPATH entries are reported separately
   546  TEST multiple GOPATH entries reported separately
   547  if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/./src/foo/quxx') -ne 2 ] ; then
   548          echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)\n.*testdata/b/src/foo/quxx'
   549          ok=false
   550  fi
   551  # test (from $GOPATH) annotation is reported for the first GOPATH entry
   552  TEST mention GOPATH in first GOPATH entry
   553  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
   554          echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)'
   555          ok=false
   556  fi
   557  # but not on the second
   558  TEST but not the second entry
   559  if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/b/src/foo/quxx$') -ne 1 ] ; then
   560          echo 'go install foo/quxx expected error: .*testdata/b/src/foo/quxx'
   561          ok=false
   562  fi
   563  # test missing GOPATH is reported
   564  TEST missing GOPATH is reported
   565  if [ $(GOPATH= ./testgo install 'foo/quxx' 2>&1 | egrep -c '\(\$GOPATH not set\)$') -ne 1 ] ; then
   566          echo 'go install foo/quxx expected error: ($GOPATH not set)'
   567          ok=false
   568  fi
   569  
   570  # issue 4186. go get cannot be used to download packages to $GOROOT
   571  # Test that without GOPATH set, go get should fail
   572  TEST without GOPATH, go get fails
   573  d=$(mktemp -d -t testgoXXX)
   574  mkdir -p $d/src
   575  if GOPATH= GOROOT=$d ./testgo get -d golang.org/x/codereview/cmd/hgpatch ; then 
   576  	echo 'go get golang.org/x/codereview/cmd/hgpatch should not succeed with $GOPATH unset'
   577  	ok=false
   578  fi	
   579  rm -rf $d
   580  
   581  # Test that with GOPATH=$GOROOT, go get should fail
   582  TEST with GOPATH=GOROOT, go get fails
   583  d=$(mktemp -d -t testgoXXX)
   584  mkdir -p $d/src
   585  if GOPATH=$d GOROOT=$d ./testgo get -d golang.org/x/codereview/cmd/hgpatch ; then
   586          echo 'go get golang.org/x/codereview/cmd/hgpatch should not succeed with GOPATH=$GOROOT'
   587          ok=false
   588  fi
   589  rm -rf $d
   590  
   591  TEST ldflags arguments with spaces '(issue 3941)'
   592  d=$(mktemp -d -t testgoXXX)
   593  cat >$d/main.go<<EOF
   594  package main
   595  var extern string
   596  func main() {
   597  	println(extern)
   598  }
   599  EOF
   600  ./testgo run -ldflags '-X main.extern "hello world"' $d/main.go 2>hello.out || ok=false
   601  if ! grep -q '^hello world' hello.out; then
   602  	echo "ldflags -X main.extern 'hello world' failed. Output:"
   603  	cat hello.out
   604  	ok=false
   605  fi
   606  rm -rf $d hello.out
   607  
   608  TEST go test -cpuprofile leaves binary behind
   609  ./testgo test -cpuprofile strings.prof strings || ok=false
   610  if [ ! -x strings.test ]; then
   611  	echo "go test -cpuprofile did not create strings.test"
   612  	ok=false
   613  fi
   614  rm -f strings.prof strings.test
   615  
   616  TEST go test -cpuprofile -o controls binary location
   617  ./testgo test -cpuprofile strings.prof -o mystrings.test strings || ok=false
   618  if [ ! -x mystrings.test ]; then
   619  	echo "go test -cpuprofile -o mystrings.test did not create mystrings.test"
   620  	ok=false
   621  fi
   622  rm -f strings.prof mystrings.test
   623  
   624  TEST go test -c -o controls binary location
   625  ./testgo test -c -o mystrings.test strings || ok=false
   626  if [ ! -x mystrings.test ]; then
   627  	echo "go test -c -o mystrings.test did not create mystrings.test"
   628  	ok=false
   629  fi
   630  rm -f mystrings.test
   631  
   632  TEST go test -o writes binary
   633  ./testgo test -o mystrings.test strings || ok=false
   634  if [ ! -x mystrings.test ]; then
   635  	echo "go test -o mystrings.test did not create mystrings.test"
   636  	ok=false
   637  fi
   638  rm -f mystrings.test
   639  
   640  TEST symlinks do not confuse go list '(issue 4568)'
   641  old=$(pwd)
   642  tmp=$(cd /tmp && pwd -P)
   643  d=$(TMPDIR=$tmp mktemp -d -t testgoXXX)
   644  mkdir -p $d/src
   645  (
   646  	ln -s $d $d/src/dir1
   647  	cd $d/src
   648  	echo package p >dir1/p.go
   649  	export GOPATH=$d
   650  	if [ "$($old/testgo list -f '{{.Root}}' dir1)" != "$d" ]; then
   651  		echo Confused by symlinks.
   652  		echo "Package in current directory $(pwd) should have Root $d"
   653  		env|grep WD
   654  		$old/testgo list -json . dir1
   655  		touch $d/failed
   656  	fi		
   657  )
   658  if [ -f $d/failed ]; then
   659  	ok=false
   660  fi
   661  rm -rf $d
   662  
   663  TEST 'install with tags (issue 4515)'
   664  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   665  mkdir -p $d/src/example/a $d/src/example/b $d/bin
   666  cat >$d/src/example/a/main.go <<EOF
   667  package main
   668  func main() {}
   669  EOF
   670  cat >$d/src/example/b/main.go <<EOF
   671  // +build mytag
   672  
   673  package main
   674  func main() {}
   675  EOF
   676  GOPATH=$d ./testgo install -tags mytag example/a example/b || ok=false
   677  if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then
   678  	echo go install example/a example/b did not install binaries
   679  	ok=false
   680  fi
   681  rm -f $d/bin/*
   682  GOPATH=$d ./testgo install -tags mytag example/... || ok=false
   683  if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then
   684  	echo go install example/... did not install binaries
   685  	ok=false
   686  fi
   687  rm -f $d/bin/*go
   688  export GOPATH=$d
   689  if [ "$(./testgo list -tags mytag example/b...)" != "example/b" ]; then
   690  	echo go list example/b did not find example/b
   691  	ok=false
   692  fi
   693  unset GOPATH
   694  rm -rf $d
   695  
   696  TEST case collisions '(issue 4773)'
   697  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   698  export GOPATH=$d
   699  mkdir -p $d/src/example/{a/pkg,a/Pkg,b}
   700  cat >$d/src/example/a/a.go <<EOF
   701  package p
   702  import (
   703  	_ "example/a/pkg"
   704  	_ "example/a/Pkg"
   705  )
   706  EOF
   707  cat >$d/src/example/a/pkg/pkg.go <<EOF
   708  package pkg
   709  EOF
   710  cat >$d/src/example/a/Pkg/pkg.go <<EOF
   711  package pkg
   712  EOF
   713  if ./testgo list example/a 2>$d/out; then
   714  	echo go list example/a should have failed, did not.
   715  	ok=false
   716  elif ! grep "case-insensitive import collision" $d/out >/dev/null; then
   717  	echo go list example/a did not report import collision.
   718  	ok=false
   719  fi
   720  cat >$d/src/example/b/file.go <<EOF
   721  package b
   722  EOF
   723  cat >$d/src/example/b/FILE.go <<EOF
   724  package b
   725  EOF
   726  if [ $(ls $d/src/example/b | wc -l) = 2 ]; then
   727  	# case-sensitive file system, let directory read find both files
   728  	args="example/b"
   729  else
   730  	# case-insensitive file system, list files explicitly on command line.
   731  	args="$d/src/example/b/file.go $d/src/example/b/FILE.go"
   732  fi
   733  if ./testgo list $args 2>$d/out; then
   734  	echo go list example/b should have failed, did not.
   735  	ok=false
   736  elif ! grep "case-insensitive file name collision" $d/out >/dev/null; then
   737  	echo go list example/b did not report file name collision.
   738  	ok=false
   739  fi
   740  
   741  TEST go get cover
   742  ./testgo get golang.org/x/tools/cmd/cover || ok=false
   743  
   744  unset GOPATH
   745  rm -rf $d
   746  
   747  TEST go get -t "code.google.com/p/go-get-issue-8181/{a,b}"
   748  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   749  export GOPATH=$d
   750  if ./testgo get -t code.google.com/p/go-get-issue-8181/{a,b}; then
   751  	./testgo list ... | grep go.tools/godoc > /dev/null || ok=false
   752  else
   753  	ok=false
   754  fi
   755  unset GOPATH
   756  rm -rf $d
   757  
   758  TEST shadowing logic
   759  export GOPATH=$(pwd)/testdata/shadow/root1:$(pwd)/testdata/shadow/root2
   760  
   761  # The math in root1 is not "math" because the standard math is.
   762  set +e
   763  cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root1/src/math)
   764  set -e
   765  if [ "$cdir" != "(_$(pwd)/testdata/shadow/root1/src/math) ($GOROOT/src/math)" ]; then
   766  	echo shadowed math is not shadowed: "$cdir"
   767  	ok=false
   768  fi
   769  
   770  # The foo in root1 is "foo".
   771  set +e
   772  cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root1/src/foo)
   773  set -e
   774  if [ "$cdir" != "(foo) ()" ]; then
   775  	echo unshadowed foo is shadowed: "$cdir"
   776  	ok=false
   777  fi
   778  
   779  # The foo in root2 is not "foo" because the foo in root1 got there first.
   780  set +e
   781  cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root2/src/foo)
   782  set -e
   783  if [ "$cdir" != "(_$(pwd)/testdata/shadow/root2/src/foo) ($(pwd)/testdata/shadow/root1/src/foo)" ]; then
   784  	echo shadowed foo is not shadowed: "$cdir"
   785  	ok=false
   786  fi
   787  
   788  # The error for go install should mention the conflicting directory.
   789  set +e
   790  err=$(./testgo install ./testdata/shadow/root2/src/foo 2>&1)
   791  set -e
   792  if [ "$err" != "go install: no install location for $(pwd)/testdata/shadow/root2/src/foo: hidden by $(pwd)/testdata/shadow/root1/src/foo" ]; then
   793  	echo wrong shadowed install error: "$err"
   794  	ok=false
   795  fi
   796  
   797  # Only succeeds if source order is preserved.
   798  TEST source file name order preserved
   799  ./testgo test testdata/example[12]_test.go || ok=false
   800  
   801  # Check that coverage analysis works at all.
   802  # Don't worry about the exact numbers but require not 0.0%.
   803  checkcoverage() {
   804  	if grep '[^0-9]0\.0%' testdata/cover.txt >/dev/null; then
   805  		echo 'some coverage results are 0.0%'
   806  		ok=false
   807  	fi
   808  	cat testdata/cover.txt
   809  	rm -f testdata/cover.txt
   810  }
   811  	
   812  TEST coverage runs
   813  ./testgo test -short -coverpkg=strings strings regexp >testdata/cover.txt 2>&1 || ok=false
   814  ./testgo test -short -cover strings math regexp >>testdata/cover.txt 2>&1 || ok=false
   815  checkcoverage
   816  
   817  # Check that coverage analysis uses set mode.
   818  TEST coverage uses set mode
   819  if ./testgo test -short -cover encoding/binary -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then
   820  	if ! grep -q 'mode: set' testdata/cover.out; then
   821  		ok=false
   822  	fi
   823  	checkcoverage
   824  else
   825  	ok=false
   826  fi
   827  rm -f testdata/cover.out testdata/cover.txt
   828  
   829  TEST coverage uses atomic mode for -race.
   830  if ./testgo test -short -race -cover encoding/binary -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then
   831  	if ! grep -q 'mode: atomic' testdata/cover.out; then
   832  		ok=false
   833  	fi
   834  	checkcoverage
   835  else
   836  	ok=false
   837  fi
   838  rm -f testdata/cover.out
   839  
   840  TEST coverage uses actual setting to override even for -race.
   841  if ./testgo test -short -race -cover encoding/binary -covermode=count -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then
   842  	if ! grep -q 'mode: count' testdata/cover.out; then
   843  		ok=false
   844  	fi
   845  	checkcoverage
   846  else
   847  	ok=false
   848  fi
   849  rm -f testdata/cover.out
   850  
   851  TEST coverage with cgo
   852  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   853  ./testgo test -short -cover ./testdata/cgocover >testdata/cover.txt 2>&1 || ok=false
   854  checkcoverage
   855  
   856  TEST cgo depends on syscall
   857  rm -rf $GOROOT/pkg/*_race
   858  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   859  export GOPATH=$d
   860  mkdir -p $d/src/foo
   861  echo '
   862  package foo
   863  //#include <stdio.h>
   864  import "C"
   865  ' >$d/src/foo/foo.go
   866  ./testgo build -race foo || ok=false
   867  rm -rf $d
   868  unset GOPATH
   869  
   870  TEST cgo shows full path names
   871  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   872  export GOPATH=$d
   873  mkdir -p $d/src/x/y/dirname
   874  echo '
   875  package foo
   876  import "C"
   877  func f() {
   878  ' >$d/src/x/y/dirname/foo.go
   879  if ./testgo build x/y/dirname >$d/err 2>&1; then
   880  	echo build succeeded unexpectedly.
   881  	ok=false
   882  elif ! grep x/y/dirname $d/err >/dev/null; then
   883  	echo error did not use full path.
   884  	cat $d/err
   885  	ok=false
   886  fi
   887  rm -rf $d
   888  unset GOPATH
   889  
   890  TEST 'cgo handles -Wl,$ORIGIN'
   891  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   892  export GOPATH=$d
   893  mkdir -p $d/src/origin
   894  echo '
   895  package origin
   896  // #cgo !darwin LDFLAGS: -Wl,-rpath -Wl,$ORIGIN
   897  // void f(void) {}
   898  import "C"
   899  
   900  func f() { C.f() }
   901  ' >$d/src/origin/origin.go
   902  if ! ./testgo build origin; then
   903  	echo build failed
   904  	ok=false
   905  fi
   906  rm -rf $d
   907  unset GOPATH
   908  
   909  TEST 'Issue 6480: "go test -c -test.bench=XXX fmt" should not hang'
   910  if ! ./testgo test -c -test.bench=XXX fmt; then
   911  	echo build test failed
   912  	ok=false
   913  fi
   914  rm -f fmt.test
   915  
   916  TEST 'Issue 7573: cmd/cgo: undefined reference when linking a C-library using gccgo'
   917  d=$(mktemp -d -t testgoXXX)
   918  export GOPATH=$d
   919  mkdir -p $d/src/cgoref
   920  ldflags="-L alibpath -lalib"
   921  echo "
   922  package main
   923  // #cgo LDFLAGS: $ldflags
   924  // void f(void) {}
   925  import \"C\"
   926  
   927  func main() { C.f() }
   928  " >$d/src/cgoref/cgoref.go
   929  go_cmds="$(./testgo build -n -compiler gccgo cgoref 2>&1 1>/dev/null)"
   930  ldflags_count="$(echo "$go_cmds" | egrep -c "^gccgo.*$(echo $ldflags | sed -e 's/-/\\-/g')" || true)"
   931  if [ "$ldflags_count" -lt 1 ]; then
   932  	echo "No Go-inline "#cgo LDFLAGS:" (\"$ldflags\") passed to gccgo linking stage."
   933  	ok=false
   934  fi
   935  rm -rf $d
   936  unset ldflags_count
   937  unset go_cmds
   938  unset ldflags
   939  unset GOPATH
   940  
   941  TEST list template can use context function
   942  if ! ./testgo list -f "GOARCH: {{context.GOARCH}}"; then 
   943  	echo unable to use context in list template
   944  	ok=false
   945  fi
   946  
   947  TEST 'Issue 7108: cmd/go: "go test" should fail if package does not build'
   948  export GOPATH=$(pwd)/testdata
   949  if ./testgo test notest >/dev/null 2>&1; then
   950  	echo 'go test notest succeeded, but should fail'
   951  	ok=false
   952  fi
   953  unset GOPATH
   954  
   955  TEST 'Issue 6844: cmd/go: go test -a foo does not rebuild regexp'
   956  if ! ./testgo test -x -a -c testdata/dep_test.go 2>deplist; then
   957  	echo "go test -x -a -c testdata/dep_test.go failed"
   958  	ok=false
   959  elif ! grep -q regexp deplist; then
   960  	echo "go test -x -a -c testdata/dep_test.go did not rebuild regexp"
   961  	ok=false
   962  fi
   963  rm -f deplist
   964  rm -f deps.test
   965  
   966  TEST list template can use context function
   967  if ! ./testgo list -f "GOARCH: {{context.GOARCH}}"; then 
   968  	echo unable to use context in list template
   969  	ok=false
   970  fi
   971  
   972  TEST build -i installs dependencies
   973  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   974  export GOPATH=$d
   975  mkdir -p $d/src/x/y/foo $d/src/x/y/bar
   976  echo '
   977  package foo
   978  func F() {}
   979  ' >$d/src/x/y/foo/foo.go
   980  checkbar() {
   981  	desc="$1"
   982  	sleep 1
   983  	touch $d/src/x/y/foo/foo.go
   984  	if ! ./testgo build -v -i x/y/bar &> $d/err; then
   985  		echo build -i "$1" failed
   986  		cat $d/err
   987  		ok=false
   988  	elif ! grep x/y/foo $d/err >/dev/null; then
   989  		echo first build -i "$1" did not build x/y/foo
   990  		cat $d/err
   991  		ok=false
   992  	fi
   993  	if ! ./testgo build -v -i x/y/bar &> $d/err; then
   994  		echo second build -i "$1" failed
   995  		cat $d/err
   996  		ok=false
   997  	elif grep x/y/foo $d/err >/dev/null; then
   998  		echo second build -i "$1" built x/y/foo
   999  		cat $d/err
  1000  		ok=false
  1001  	fi
  1002  }
  1003  
  1004  echo '
  1005  package bar
  1006  import "x/y/foo"
  1007  func F() { foo.F() }
  1008  ' >$d/src/x/y/bar/bar.go
  1009  checkbar pkg
  1010  
  1011  TEST build -i installs dependencies for command
  1012  echo '
  1013  package main
  1014  import "x/y/foo"
  1015  func main() { foo.F() }
  1016  ' >$d/src/x/y/bar/bar.go
  1017  checkbar cmd
  1018  
  1019  rm -rf $d bar
  1020  unset GOPATH
  1021  
  1022  TEST 'go build in test-only directory fails with a good error'
  1023  if ./testgo build ./testdata/testonly 2>testdata/err.out; then
  1024  	echo "go build ./testdata/testonly succeeded, should have failed"
  1025  	ok=false
  1026  elif ! grep 'no buildable Go' testdata/err.out >/dev/null; then
  1027  	echo "go build ./testdata/testonly produced unexpected error:"
  1028  	cat testdata/err.out
  1029  	ok=false
  1030  fi
  1031  rm -f testdata/err.out
  1032  
  1033  TEST 'go test detects test-only import cycles'
  1034  export GOPATH=$(pwd)/testdata
  1035  if ./testgo test -c testcycle/p3 2>testdata/err.out; then
  1036  	echo "go test testcycle/p3 succeeded, should have failed"
  1037  	ok=false
  1038  elif ! grep 'import cycle not allowed in test' testdata/err.out >/dev/null; then
  1039  	echo "go test testcycle/p3 produced unexpected error:"
  1040  	cat testdata/err.out
  1041  	ok=false
  1042  fi
  1043  rm -f testdata/err.out
  1044  unset GOPATH
  1045  
  1046  TEST 'go test foo_test.go works'
  1047  if ! ./testgo test testdata/standalone_test.go; then
  1048  	echo "go test testdata/standalone_test.go failed"
  1049  	ok=false
  1050  fi
  1051  
  1052  TEST 'go test xtestonly works'
  1053  export GOPATH=$(pwd)/testdata
  1054  ./testgo clean -i xtestonly || ok=false
  1055  if ! ./testgo test xtestonly >/dev/null; then
  1056  	echo "go test xtestonly failed"
  1057  	ok=false
  1058  fi
  1059  unset GOPATH
  1060  
  1061  TEST 'go test builds an xtest containing only non-runnable examples'
  1062  if ! ./testgo test -v ./testdata/norunexample > testdata/std.out; then
  1063  	echo "go test ./testdata/norunexample failed"
  1064  	ok=false
  1065  elif ! grep 'File with non-runnable example was built.' testdata/std.out > /dev/null; then
  1066  	echo "file with non-runnable example was not built"
  1067  	ok=false
  1068  fi
  1069  
  1070  TEST 'go generate handles simple command'
  1071  if ! ./testgo generate ./testdata/generate/test1.go > testdata/std.out; then
  1072  	echo "go test ./testdata/generate/test1.go failed to run"
  1073  	ok=false
  1074  elif ! grep 'Success' testdata/std.out > /dev/null; then
  1075  	echo "go test ./testdata/generate/test1.go generated wrong output"
  1076  	ok=false
  1077  fi
  1078  
  1079  TEST 'go generate handles command alias'
  1080  if ! ./testgo generate ./testdata/generate/test2.go > testdata/std.out; then
  1081  	echo "go test ./testdata/generate/test2.go failed to run"
  1082  	ok=false
  1083  elif ! grep 'Now is the time for all good men' testdata/std.out > /dev/null; then
  1084  	echo "go test ./testdata/generate/test2.go generated wrong output"
  1085  	ok=false
  1086  fi
  1087  
  1088  TEST 'go generate variable substitution'
  1089  if ! ./testgo generate ./testdata/generate/test3.go > testdata/std.out; then
  1090  	echo "go test ./testdata/generate/test3.go failed to run"
  1091  	ok=false
  1092  elif ! grep "$GOARCH test3.go p xyzp/test3.go/123" testdata/std.out > /dev/null; then
  1093  	echo "go test ./testdata/generate/test3.go generated wrong output"
  1094  	ok=false
  1095  fi
  1096  
  1097  TEST go get works with vanity wildcards
  1098  d=$(mktemp -d -t testgoXXX)
  1099  export GOPATH=$d
  1100  if ! ./testgo get -u rsc.io/pdf/...; then
  1101  	ok=false
  1102  elif [ ! -x $d/bin/pdfpasswd ]; then
  1103  	echo did not build rsc.io/pdf/pdfpasswd
  1104  	ok=false
  1105  fi
  1106  unset GOPATH
  1107  rm -rf $d
  1108  
  1109  TEST go vet with external tests
  1110  d=$(mktemp -d -t testgoXXX)
  1111  export GOPATH=$(pwd)/testdata
  1112  if ./testgo vet vetpkg >$d/err 2>&1; then
  1113  	echo "go vet vetpkg passes incorrectly"
  1114  	ok=false
  1115  elif ! grep -q 'missing argument for Printf' $d/err; then
  1116  	echo "go vet vetpkg did not find missing argument for Printf"
  1117  	cat $d/err
  1118  	ok=false
  1119  fi
  1120  unset GOPATH
  1121  rm -rf $d
  1122  
  1123  TEST go get ./rsc.io/toolstash '(golang.org/issue/9767)'
  1124  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
  1125  export GOPATH=$d
  1126  export testgo=$(pwd)/testgo
  1127  mkdir -p $GOPATH/src/rsc.io
  1128  (cd $GOPATH/src/rsc.io && $testgo get ./toolstash) || ok=false
  1129  unset GOPATH
  1130  unset testgo
  1131  rm -rf $d
  1132  
  1133  # clean up
  1134  if $started; then stop; fi
  1135  rm -rf testdata/bin testdata/bin1
  1136  rm -f testgo
  1137  
  1138  if $allok; then
  1139  	echo PASS
  1140  else
  1141  	echo FAIL:
  1142  	echo "$testfail"
  1143  	exit 1
  1144  fi