github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/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  # ensure that output of 'go list' is consistent between runs
   514  TEST go list is consistent
   515  ./testgo list std > test_std.list || ok=false
   516  if ! ./testgo list std | cmp -s test_std.list - ; then
   517  	echo "go list std ordering is inconsistent"
   518  	ok=false
   519  fi
   520  rm -f test_std.list
   521  
   522  # issue 4096. Validate the output of unsuccessful go install foo/quxx 
   523  TEST unsuccessful go install should mention missing package
   524  if [ $(./testgo install 'foo/quxx' 2>&1 | grep -c 'cannot find package "foo/quxx" in any of') -ne 1 ] ; then
   525  	echo 'go install foo/quxx expected error: .*cannot find package "foo/quxx" in any of'
   526  	ok=false
   527  fi 
   528  # test GOROOT search failure is reported
   529  TEST GOROOT search failure reporting
   530  if [ $(./testgo install 'foo/quxx' 2>&1 | egrep -c 'foo/quxx \(from \$GOROOT\)$') -ne 1 ] ; then
   531          echo 'go install foo/quxx expected error: .*foo/quxx (from $GOROOT)'
   532          ok=false
   533  fi
   534  # test multiple GOPATH entries are reported separately
   535  TEST multiple GOPATH entries reported separately
   536  if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/./src/foo/quxx') -ne 2 ] ; then
   537          echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)\n.*testdata/b/src/foo/quxx'
   538          ok=false
   539  fi
   540  # test (from $GOPATH) annotation is reported for the first GOPATH entry
   541  TEST mention GOPATH in first GOPATH entry
   542  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
   543          echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)'
   544          ok=false
   545  fi
   546  # but not on the second
   547  TEST but not the second entry
   548  if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/b/src/foo/quxx$') -ne 1 ] ; then
   549          echo 'go install foo/quxx expected error: .*testdata/b/src/foo/quxx'
   550          ok=false
   551  fi
   552  # test missing GOPATH is reported
   553  TEST missing GOPATH is reported
   554  if [ $(GOPATH= ./testgo install 'foo/quxx' 2>&1 | egrep -c '\(\$GOPATH not set\)$') -ne 1 ] ; then
   555          echo 'go install foo/quxx expected error: ($GOPATH not set)'
   556          ok=false
   557  fi
   558  
   559  # issue 4186. go get cannot be used to download packages to $GOROOT
   560  # Test that without GOPATH set, go get should fail
   561  TEST without GOPATH, go get fails
   562  d=$(mktemp -d -t testgoXXX)
   563  mkdir -p $d/src
   564  if GOPATH= GOROOT=$d ./testgo get -d golang.org/x/codereview/cmd/hgpatch ; then 
   565  	echo 'go get golang.org/x/codereview/cmd/hgpatch should not succeed with $GOPATH unset'
   566  	ok=false
   567  fi	
   568  rm -rf $d
   569  
   570  # Test that with GOPATH=$GOROOT, go get should fail
   571  TEST with GOPATH=GOROOT, go get fails
   572  d=$(mktemp -d -t testgoXXX)
   573  mkdir -p $d/src
   574  if GOPATH=$d GOROOT=$d ./testgo get -d golang.org/x/codereview/cmd/hgpatch ; then
   575          echo 'go get golang.org/x/codereview/cmd/hgpatch should not succeed with GOPATH=$GOROOT'
   576          ok=false
   577  fi
   578  rm -rf $d
   579  
   580  TEST ldflags arguments with spaces '(issue 3941)'
   581  d=$(mktemp -d -t testgoXXX)
   582  cat >$d/main.go<<EOF
   583  package main
   584  var extern string
   585  func main() {
   586  	println(extern)
   587  }
   588  EOF
   589  ./testgo run -ldflags '-X main.extern "hello world"' $d/main.go 2>hello.out || ok=false
   590  if ! grep -q '^hello world' hello.out; then
   591  	echo "ldflags -X main.extern 'hello world' failed. Output:"
   592  	cat hello.out
   593  	ok=false
   594  fi
   595  rm -rf $d hello.out
   596  
   597  TEST go test -cpuprofile leaves binary behind
   598  ./testgo test -cpuprofile strings.prof strings || ok=false
   599  if [ ! -x strings.test ]; then
   600  	echo "go test -cpuprofile did not create strings.test"
   601  	ok=false
   602  fi
   603  rm -f strings.prof strings.test
   604  
   605  TEST go test -cpuprofile -o controls binary location
   606  ./testgo test -cpuprofile strings.prof -o mystrings.test strings || ok=false
   607  if [ ! -x mystrings.test ]; then
   608  	echo "go test -cpuprofile -o mystrings.test did not create mystrings.test"
   609  	ok=false
   610  fi
   611  rm -f strings.prof mystrings.test
   612  
   613  TEST go test -c -o controls binary location
   614  ./testgo test -c -o mystrings.test strings || ok=false
   615  if [ ! -x mystrings.test ]; then
   616  	echo "go test -c -o mystrings.test did not create mystrings.test"
   617  	ok=false
   618  fi
   619  rm -f mystrings.test
   620  
   621  TEST go test -o writes binary
   622  ./testgo test -o mystrings.test strings || ok=false
   623  if [ ! -x mystrings.test ]; then
   624  	echo "go test -o mystrings.test did not create mystrings.test"
   625  	ok=false
   626  fi
   627  rm -f mystrings.test
   628  
   629  TEST symlinks do not confuse go list '(issue 4568)'
   630  old=$(pwd)
   631  tmp=$(cd /tmp && pwd -P)
   632  d=$(TMPDIR=$tmp mktemp -d -t testgoXXX)
   633  mkdir -p $d/src
   634  (
   635  	ln -s $d $d/src/dir1
   636  	cd $d/src
   637  	echo package p >dir1/p.go
   638  	export GOPATH=$d
   639  	if [ "$($old/testgo list -f '{{.Root}}' dir1)" != "$d" ]; then
   640  		echo Confused by symlinks.
   641  		echo "Package in current directory $(pwd) should have Root $d"
   642  		env|grep WD
   643  		$old/testgo list -json . dir1
   644  		touch $d/failed
   645  	fi		
   646  )
   647  if [ -f $d/failed ]; then
   648  	ok=false
   649  fi
   650  rm -rf $d
   651  
   652  TEST 'install with tags (issue 4515)'
   653  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   654  mkdir -p $d/src/example/a $d/src/example/b $d/bin
   655  cat >$d/src/example/a/main.go <<EOF
   656  package main
   657  func main() {}
   658  EOF
   659  cat >$d/src/example/b/main.go <<EOF
   660  // +build mytag
   661  
   662  package main
   663  func main() {}
   664  EOF
   665  GOPATH=$d ./testgo install -tags mytag example/a example/b || ok=false
   666  if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then
   667  	echo go install example/a example/b did not install binaries
   668  	ok=false
   669  fi
   670  rm -f $d/bin/*
   671  GOPATH=$d ./testgo install -tags mytag example/... || ok=false
   672  if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then
   673  	echo go install example/... did not install binaries
   674  	ok=false
   675  fi
   676  rm -f $d/bin/*go
   677  export GOPATH=$d
   678  if [ "$(./testgo list -tags mytag example/b...)" != "example/b" ]; then
   679  	echo go list example/b did not find example/b
   680  	ok=false
   681  fi
   682  unset GOPATH
   683  rm -rf $d
   684  
   685  TEST case collisions '(issue 4773)'
   686  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   687  export GOPATH=$d
   688  mkdir -p $d/src/example/{a/pkg,a/Pkg,b}
   689  cat >$d/src/example/a/a.go <<EOF
   690  package p
   691  import (
   692  	_ "example/a/pkg"
   693  	_ "example/a/Pkg"
   694  )
   695  EOF
   696  cat >$d/src/example/a/pkg/pkg.go <<EOF
   697  package pkg
   698  EOF
   699  cat >$d/src/example/a/Pkg/pkg.go <<EOF
   700  package pkg
   701  EOF
   702  if ./testgo list example/a 2>$d/out; then
   703  	echo go list example/a should have failed, did not.
   704  	ok=false
   705  elif ! grep "case-insensitive import collision" $d/out >/dev/null; then
   706  	echo go list example/a did not report import collision.
   707  	ok=false
   708  fi
   709  cat >$d/src/example/b/file.go <<EOF
   710  package b
   711  EOF
   712  cat >$d/src/example/b/FILE.go <<EOF
   713  package b
   714  EOF
   715  if [ $(ls $d/src/example/b | wc -l) = 2 ]; then
   716  	# case-sensitive file system, let directory read find both files
   717  	args="example/b"
   718  else
   719  	# case-insensitive file system, list files explicitly on command line.
   720  	args="$d/src/example/b/file.go $d/src/example/b/FILE.go"
   721  fi
   722  if ./testgo list $args 2>$d/out; then
   723  	echo go list example/b should have failed, did not.
   724  	ok=false
   725  elif ! grep "case-insensitive file name collision" $d/out >/dev/null; then
   726  	echo go list example/b did not report file name collision.
   727  	ok=false
   728  fi
   729  
   730  TEST go get cover
   731  ./testgo get golang.org/x/tools/cmd/cover || ok=false
   732  
   733  unset GOPATH
   734  rm -rf $d
   735  
   736  TEST go get -t "code.google.com/p/go-get-issue-8181/{a,b}"
   737  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   738  export GOPATH=$d
   739  if ./testgo get -t code.google.com/p/go-get-issue-8181/{a,b}; then
   740  	./testgo list ... | grep go.tools/godoc > /dev/null || ok=false
   741  else
   742  	ok=false
   743  fi
   744  unset GOPATH
   745  rm -rf $d
   746  
   747  TEST shadowing logic
   748  export GOPATH=$(pwd)/testdata/shadow/root1:$(pwd)/testdata/shadow/root2
   749  
   750  # The math in root1 is not "math" because the standard math is.
   751  set +e
   752  cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root1/src/math)
   753  set -e
   754  if [ "$cdir" != "(_$(pwd)/testdata/shadow/root1/src/math) ($GOROOT/src/math)" ]; then
   755  	echo shadowed math is not shadowed: "$cdir"
   756  	ok=false
   757  fi
   758  
   759  # The foo in root1 is "foo".
   760  set +e
   761  cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root1/src/foo)
   762  set -e
   763  if [ "$cdir" != "(foo) ()" ]; then
   764  	echo unshadowed foo is shadowed: "$cdir"
   765  	ok=false
   766  fi
   767  
   768  # The foo in root2 is not "foo" because the foo in root1 got there first.
   769  set +e
   770  cdir=$(./testgo list -f '({{.ImportPath}}) ({{.ConflictDir}})' ./testdata/shadow/root2/src/foo)
   771  set -e
   772  if [ "$cdir" != "(_$(pwd)/testdata/shadow/root2/src/foo) ($(pwd)/testdata/shadow/root1/src/foo)" ]; then
   773  	echo shadowed foo is not shadowed: "$cdir"
   774  	ok=false
   775  fi
   776  
   777  # The error for go install should mention the conflicting directory.
   778  set +e
   779  err=$(./testgo install ./testdata/shadow/root2/src/foo 2>&1)
   780  set -e
   781  if [ "$err" != "go install: no install location for $(pwd)/testdata/shadow/root2/src/foo: hidden by $(pwd)/testdata/shadow/root1/src/foo" ]; then
   782  	echo wrong shadowed install error: "$err"
   783  	ok=false
   784  fi
   785  
   786  # Only succeeds if source order is preserved.
   787  TEST source file name order preserved
   788  ./testgo test testdata/example[12]_test.go || ok=false
   789  
   790  # Check that coverage analysis works at all.
   791  # Don't worry about the exact numbers but require not 0.0%.
   792  checkcoverage() {
   793  	if grep '[^0-9]0\.0%' testdata/cover.txt >/dev/null; then
   794  		echo 'some coverage results are 0.0%'
   795  		ok=false
   796  	fi
   797  	cat testdata/cover.txt
   798  	rm -f testdata/cover.txt
   799  }
   800  	
   801  TEST coverage runs
   802  ./testgo test -short -coverpkg=strings strings regexp >testdata/cover.txt 2>&1 || ok=false
   803  ./testgo test -short -cover strings math regexp >>testdata/cover.txt 2>&1 || ok=false
   804  checkcoverage
   805  
   806  # Check that coverage analysis uses set mode.
   807  TEST coverage uses set mode
   808  if ./testgo test -short -cover encoding/binary -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then
   809  	if ! grep -q 'mode: set' testdata/cover.out; then
   810  		ok=false
   811  	fi
   812  	checkcoverage
   813  else
   814  	ok=false
   815  fi
   816  rm -f testdata/cover.out testdata/cover.txt
   817  
   818  TEST coverage uses atomic mode for -race.
   819  if ./testgo test -short -race -cover encoding/binary -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then
   820  	if ! grep -q 'mode: atomic' testdata/cover.out; then
   821  		ok=false
   822  	fi
   823  	checkcoverage
   824  else
   825  	ok=false
   826  fi
   827  rm -f testdata/cover.out
   828  
   829  TEST coverage uses actual setting to override even for -race.
   830  if ./testgo test -short -race -cover encoding/binary -covermode=count -coverprofile=testdata/cover.out >testdata/cover.txt 2>&1; then
   831  	if ! grep -q 'mode: count' 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 with cgo
   841  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   842  ./testgo test -short -cover ./testdata/cgocover >testdata/cover.txt 2>&1 || ok=false
   843  checkcoverage
   844  
   845  TEST cgo depends on syscall
   846  rm -rf $GOROOT/pkg/*_race
   847  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   848  export GOPATH=$d
   849  mkdir -p $d/src/foo
   850  echo '
   851  package foo
   852  //#include <stdio.h>
   853  import "C"
   854  ' >$d/src/foo/foo.go
   855  ./testgo build -race foo || ok=false
   856  rm -rf $d
   857  unset GOPATH
   858  
   859  TEST cgo shows full path names
   860  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   861  export GOPATH=$d
   862  mkdir -p $d/src/x/y/dirname
   863  echo '
   864  package foo
   865  import "C"
   866  func f() {
   867  ' >$d/src/x/y/dirname/foo.go
   868  if ./testgo build x/y/dirname >$d/err 2>&1; then
   869  	echo build succeeded unexpectedly.
   870  	ok=false
   871  elif ! grep x/y/dirname $d/err >/dev/null; then
   872  	echo error did not use full path.
   873  	cat $d/err
   874  	ok=false
   875  fi
   876  rm -rf $d
   877  unset GOPATH
   878  
   879  TEST 'cgo handles -Wl,$ORIGIN'
   880  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   881  export GOPATH=$d
   882  mkdir -p $d/src/origin
   883  echo '
   884  package origin
   885  // #cgo !darwin LDFLAGS: -Wl,-rpath -Wl,$ORIGIN
   886  // void f(void) {}
   887  import "C"
   888  
   889  func f() { C.f() }
   890  ' >$d/src/origin/origin.go
   891  if ! ./testgo build origin; then
   892  	echo build failed
   893  	ok=false
   894  fi
   895  rm -rf $d
   896  unset GOPATH
   897  
   898  TEST 'Issue 6480: "go test -c -test.bench=XXX fmt" should not hang'
   899  if ! ./testgo test -c -test.bench=XXX fmt; then
   900  	echo build test failed
   901  	ok=false
   902  fi
   903  rm -f fmt.test
   904  
   905  TEST 'Issue 7573: cmd/cgo: undefined reference when linking a C-library using gccgo'
   906  d=$(mktemp -d -t testgoXXX)
   907  export GOPATH=$d
   908  mkdir -p $d/src/cgoref
   909  ldflags="-L alibpath -lalib"
   910  echo "
   911  package main
   912  // #cgo LDFLAGS: $ldflags
   913  // void f(void) {}
   914  import \"C\"
   915  
   916  func main() { C.f() }
   917  " >$d/src/cgoref/cgoref.go
   918  go_cmds="$(./testgo build -n -compiler gccgo cgoref 2>&1 1>/dev/null)"
   919  ldflags_count="$(echo "$go_cmds" | egrep -c "^gccgo.*$(echo $ldflags | sed -e 's/-/\\-/g')" || true)"
   920  if [ "$ldflags_count" -lt 1 ]; then
   921  	echo "No Go-inline "#cgo LDFLAGS:" (\"$ldflags\") passed to gccgo linking stage."
   922  	ok=false
   923  fi
   924  rm -rf $d
   925  unset ldflags_count
   926  unset go_cmds
   927  unset ldflags
   928  unset GOPATH
   929  
   930  TEST list template can use context function
   931  if ! ./testgo list -f "GOARCH: {{context.GOARCH}}"; then 
   932  	echo unable to use context in list template
   933  	ok=false
   934  fi
   935  
   936  TEST 'Issue 7108: cmd/go: "go test" should fail if package does not build'
   937  export GOPATH=$(pwd)/testdata
   938  if ./testgo test notest >/dev/null 2>&1; then
   939  	echo 'go test notest succeeded, but should fail'
   940  	ok=false
   941  fi
   942  unset GOPATH
   943  
   944  TEST 'Issue 6844: cmd/go: go test -a foo does not rebuild regexp'
   945  if ! ./testgo test -x -a -c testdata/dep_test.go 2>deplist; then
   946  	echo "go test -x -a -c testdata/dep_test.go failed"
   947  	ok=false
   948  elif ! grep -q regexp deplist; then
   949  	echo "go test -x -a -c testdata/dep_test.go did not rebuild regexp"
   950  	ok=false
   951  fi
   952  rm -f deplist
   953  rm -f deps.test
   954  
   955  TEST list template can use context function
   956  if ! ./testgo list -f "GOARCH: {{context.GOARCH}}"; then 
   957  	echo unable to use context in list template
   958  	ok=false
   959  fi
   960  
   961  TEST build -i installs dependencies
   962  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   963  export GOPATH=$d
   964  mkdir -p $d/src/x/y/foo $d/src/x/y/bar
   965  echo '
   966  package foo
   967  func F() {}
   968  ' >$d/src/x/y/foo/foo.go
   969  checkbar() {
   970  	desc="$1"
   971  	sleep 1
   972  	touch $d/src/x/y/foo/foo.go
   973  	if ! ./testgo build -v -i x/y/bar &> $d/err; then
   974  		echo build -i "$1" failed
   975  		cat $d/err
   976  		ok=false
   977  	elif ! grep x/y/foo $d/err >/dev/null; then
   978  		echo first build -i "$1" did not build x/y/foo
   979  		cat $d/err
   980  		ok=false
   981  	fi
   982  	if ! ./testgo build -v -i x/y/bar &> $d/err; then
   983  		echo second build -i "$1" failed
   984  		cat $d/err
   985  		ok=false
   986  	elif grep x/y/foo $d/err >/dev/null; then
   987  		echo second build -i "$1" built x/y/foo
   988  		cat $d/err
   989  		ok=false
   990  	fi
   991  }
   992  
   993  echo '
   994  package bar
   995  import "x/y/foo"
   996  func F() { foo.F() }
   997  ' >$d/src/x/y/bar/bar.go
   998  checkbar pkg
   999  
  1000  TEST build -i installs dependencies for command
  1001  echo '
  1002  package main
  1003  import "x/y/foo"
  1004  func main() { foo.F() }
  1005  ' >$d/src/x/y/bar/bar.go
  1006  checkbar cmd
  1007  
  1008  rm -rf $d bar
  1009  unset GOPATH
  1010  
  1011  TEST 'go build in test-only directory fails with a good error'
  1012  if ./testgo build ./testdata/testonly 2>testdata/err.out; then
  1013  	echo "go build ./testdata/testonly succeeded, should have failed"
  1014  	ok=false
  1015  elif ! grep 'no buildable Go' testdata/err.out >/dev/null; then
  1016  	echo "go build ./testdata/testonly produced unexpected error:"
  1017  	cat testdata/err.out
  1018  	ok=false
  1019  fi
  1020  rm -f testdata/err.out
  1021  
  1022  TEST 'go test detects test-only import cycles'
  1023  export GOPATH=$(pwd)/testdata
  1024  if ./testgo test -c testcycle/p3 2>testdata/err.out; then
  1025  	echo "go test testcycle/p3 succeeded, should have failed"
  1026  	ok=false
  1027  elif ! grep 'import cycle not allowed in test' testdata/err.out >/dev/null; then
  1028  	echo "go test testcycle/p3 produced unexpected error:"
  1029  	cat testdata/err.out
  1030  	ok=false
  1031  fi
  1032  rm -f testdata/err.out
  1033  unset GOPATH
  1034  
  1035  TEST 'go test foo_test.go works'
  1036  if ! ./testgo test testdata/standalone_test.go; then
  1037  	echo "go test testdata/standalone_test.go failed"
  1038  	ok=false
  1039  fi
  1040  
  1041  TEST 'go test xtestonly works'
  1042  export GOPATH=$(pwd)/testdata
  1043  ./testgo clean -i xtestonly || ok=false
  1044  if ! ./testgo test xtestonly >/dev/null; then
  1045  	echo "go test xtestonly failed"
  1046  	ok=false
  1047  fi
  1048  unset GOPATH
  1049  
  1050  TEST 'go test builds an xtest containing only non-runnable examples'
  1051  if ! ./testgo test -v ./testdata/norunexample > testdata/std.out; then
  1052  	echo "go test ./testdata/norunexample failed"
  1053  	ok=false
  1054  elif ! grep 'File with non-runnable example was built.' testdata/std.out > /dev/null; then
  1055  	echo "file with non-runnable example was not built"
  1056  	ok=false
  1057  fi
  1058  
  1059  TEST 'go generate handles simple command'
  1060  if ! ./testgo generate ./testdata/generate/test1.go > testdata/std.out; then
  1061  	echo "go test ./testdata/generate/test1.go failed to run"
  1062  	ok=false
  1063  elif ! grep 'Success' testdata/std.out > /dev/null; then
  1064  	echo "go test ./testdata/generate/test1.go generated wrong output"
  1065  	ok=false
  1066  fi
  1067  
  1068  TEST 'go generate handles command alias'
  1069  if ! ./testgo generate ./testdata/generate/test2.go > testdata/std.out; then
  1070  	echo "go test ./testdata/generate/test2.go failed to run"
  1071  	ok=false
  1072  elif ! grep 'Now is the time for all good men' testdata/std.out > /dev/null; then
  1073  	echo "go test ./testdata/generate/test2.go generated wrong output"
  1074  	ok=false
  1075  fi
  1076  
  1077  TEST 'go generate variable substitution'
  1078  if ! ./testgo generate ./testdata/generate/test3.go > testdata/std.out; then
  1079  	echo "go test ./testdata/generate/test3.go failed to run"
  1080  	ok=false
  1081  elif ! grep "$GOARCH test3.go p xyzp/test3.go/123" testdata/std.out > /dev/null; then
  1082  	echo "go test ./testdata/generate/test3.go generated wrong output"
  1083  	ok=false
  1084  fi
  1085  
  1086  TEST go get works with vanity wildcards
  1087  d=$(mktemp -d -t testgoXXX)
  1088  export GOPATH=$d
  1089  if ! ./testgo get -u rsc.io/pdf/...; then
  1090  	ok=false
  1091  elif [ ! -x $d/bin/pdfpasswd ]; then
  1092  	echo did not build rsc.io/pdf/pdfpasswd
  1093  	ok=false
  1094  fi
  1095  unset GOPATH
  1096  rm -rf $d
  1097  
  1098  TEST go vet with external tests
  1099  d=$(mktemp -d -t testgoXXX)
  1100  export GOPATH=$(pwd)/testdata
  1101  if ./testgo vet vetpkg >$d/err 2>&1; then
  1102  	echo "go vet vetpkg passes incorrectly"
  1103  	ok=false
  1104  elif ! grep -q 'missing argument for Printf' $d/err; then
  1105  	echo "go vet vetpkg did not find missing argument for Printf"
  1106  	cat $d/err
  1107  	ok=false
  1108  fi
  1109  unset GOPATH
  1110  rm -rf $d
  1111  
  1112  # clean up
  1113  if $started; then stop; fi
  1114  rm -rf testdata/bin testdata/bin1
  1115  rm -f testgo
  1116  
  1117  if $allok; then
  1118  	echo PASS
  1119  else
  1120  	echo FAIL:
  1121  	echo "$testfail"
  1122  	exit 1
  1123  fi