github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/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 GOPATH
    40  unset GOBIN
    41  
    42  TEST 'file:line in error messages'
    43  # Test that error messages have file:line information at beginning of
    44  # the line. Also test issue 4917: that the error is on stderr.
    45  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
    46  fn=$d/err.go
    47  echo "package main" > $fn
    48  echo 'import "bar"' >> $fn
    49  ./testgo run $fn 2>$d/err.out || true
    50  if ! grep -q "^$fn:" $d/err.out; then
    51  	echo "missing file:line in error message"
    52  	cat $d/err.out
    53  	ok=false
    54  fi
    55  rm -r $d
    56  
    57  # Test local (./) imports.
    58  testlocal() {
    59  	local="$1"
    60  	TEST local imports $2 '(easy)'
    61  	./testgo build -o hello "testdata/$local/easy.go"
    62  	./hello >hello.out
    63  	if ! grep -q '^easysub\.Hello' hello.out; then
    64  		echo "testdata/$local/easy.go did not generate expected output"
    65  		cat hello.out
    66  		ok=false
    67  	fi
    68  	
    69  	TEST local imports $2 '(easysub)'
    70  	./testgo build -o hello "testdata/$local/easysub/main.go"
    71  	./hello >hello.out
    72  	if ! grep -q '^easysub\.Hello' hello.out; then
    73  		echo "testdata/$local/easysub/main.go did not generate expected output"
    74  		cat hello.out
    75  		ok=false
    76  	fi
    77  	
    78  	TEST local imports $2 '(hard)'
    79  	./testgo build -o hello "testdata/$local/hard.go"
    80  	./hello >hello.out
    81  	if ! grep -q '^sub\.Hello' hello.out || ! grep -q '^subsub\.Hello' hello.out ; then
    82  		echo "testdata/$local/hard.go did not generate expected output"
    83  		cat hello.out
    84  		ok=false
    85  	fi
    86  	
    87  	rm -f hello.out hello
    88  	
    89  	# Test that go install x.go fails.
    90  	TEST local imports $2 '(go install should fail)'
    91  	if ./testgo install "testdata/$local/easy.go" >/dev/null 2>&1; then
    92  		echo "go install testdata/$local/easy.go succeeded"
    93  		ok=false
    94  	fi
    95  }
    96  
    97  # Test local imports
    98  testlocal local ''
    99  
   100  # Test local imports again, with bad characters in the directory name.
   101  bad='#$%:, &()*;<=>?\^{}'
   102  rm -rf "testdata/$bad"
   103  cp -R testdata/local "testdata/$bad"
   104  testlocal "$bad" 'with bad characters in path'
   105  rm -rf "testdata/$bad"
   106  
   107  # Test tests with relative imports.
   108  TEST relative imports '(go test)'
   109  if ! ./testgo test ./testdata/testimport; then
   110  	echo "go test ./testdata/testimport failed"
   111  	ok=false
   112  fi
   113  
   114  # Test installation with relative imports.
   115  TEST relative imports '(go test -i)'
   116  if ! ./testgo test -i ./testdata/testimport; then
   117      echo "go test -i ./testdata/testimport failed"
   118      ok=false
   119  fi
   120  
   121  # Test tests with relative imports in packages synthesized
   122  # from Go files named on the command line.
   123  TEST relative imports in command-line package
   124  if ! ./testgo test ./testdata/testimport/*.go; then
   125  	echo "go test ./testdata/testimport/*.go failed"
   126  	ok=false
   127  fi
   128  
   129  # Test that without $GOBIN set, binaries get installed
   130  # into the GOPATH bin directory.
   131  TEST install into GOPATH
   132  rm -rf testdata/bin
   133  if ! GOPATH=$(pwd)/testdata ./testgo install go-cmd-test; then
   134  	echo "go install go-cmd-test failed"
   135  	ok=false
   136  elif ! test -x testdata/bin/go-cmd-test; then
   137  	echo "go install go-cmd-test did not write to testdata/bin/go-cmd-test"
   138  	ok=false
   139  fi
   140  
   141  # And with $GOBIN set, binaries get installed to $GOBIN.
   142  TEST install into GOBIN
   143  if ! GOBIN=$(pwd)/testdata/bin1 GOPATH=$(pwd)/testdata ./testgo install go-cmd-test; then
   144  	echo "go install go-cmd-test failed"
   145  	ok=false
   146  elif ! test -x testdata/bin1/go-cmd-test; then
   147  	echo "go install go-cmd-test did not write to testdata/bin1/go-cmd-test"
   148  	ok=false
   149  fi
   150  
   151  # Without $GOBIN set, installing a program outside $GOPATH should fail
   152  # (there is nowhere to install it).
   153  TEST install without destination
   154  if ./testgo install testdata/src/go-cmd-test/helloworld.go; then
   155  	echo "go install testdata/src/go-cmd-test/helloworld.go should have failed, did not"
   156  	ok=false
   157  fi
   158  
   159  # With $GOBIN set, should install there.
   160  TEST install to GOBIN '(command-line package)'
   161  if ! GOBIN=$(pwd)/testdata/bin1 ./testgo install testdata/src/go-cmd-test/helloworld.go; then
   162  	echo "go install testdata/src/go-cmd-test/helloworld.go failed"
   163  	ok=false
   164  elif ! test -x testdata/bin1/helloworld; then
   165  	echo "go install testdata/src/go-cmd-test/helloworld.go did not write testdata/bin1/helloworld"
   166  	ok=false
   167  fi
   168  
   169  TEST godoc installs into GOBIN
   170  d=$(mktemp -d -t testgoXXX)
   171  export GOPATH=$d
   172  mkdir $d/gobin
   173  GOBIN=$d/gobin ./testgo get code.google.com/p/go.tools/cmd/godoc
   174  if [ ! -x $d/gobin/godoc ]; then
   175  	echo did not install godoc to '$GOBIN'
   176  	GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' code.google.com/p/go.tools/cmd/godoc
   177  	ok=false
   178  fi
   179  
   180  TEST godoc installs into GOROOT
   181  rm -f $GOROOT/bin/godoc
   182  ./testgo install code.google.com/p/go.tools/cmd/godoc
   183  if [ ! -x $GOROOT/bin/godoc ]; then
   184  	echo did not install godoc to '$GOROOT/bin'
   185  	./testgo list -f 'Target: {{.Target}}' code.google.com/p/go.tools/cmd/godoc
   186  	ok=false
   187  fi
   188  
   189  TEST cmd/fix installs into tool
   190  GOOS=$(./testgo env GOOS)
   191  GOARCH=$(./testgo env GOARCH)
   192  rm -f $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix
   193  ./testgo install cmd/fix
   194  if [ ! -x $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix ]; then
   195  	echo 'did not install cmd/fix to $GOROOT/pkg/tool'
   196  	GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' cmd/fix
   197  	ok=false
   198  fi
   199  rm -f $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix
   200  GOBIN=$d/gobin ./testgo install cmd/fix
   201  if [ ! -x $GOROOT/pkg/tool/${GOOS}_${GOARCH}/fix ]; then
   202  	echo 'did not install cmd/fix to $GOROOT/pkg/tool with $GOBIN set'
   203  	GOBIN=$d/gobin ./testgo list -f 'Target: {{.Target}}' cmd/fix
   204  	ok=false
   205  fi
   206  
   207  TEST gopath program installs into GOBIN
   208  mkdir $d/src/progname
   209  echo 'package main; func main() {}' >$d/src/progname/p.go
   210  GOBIN=$d/gobin ./testgo install progname
   211  if [ ! -x $d/gobin/progname ]; then
   212  	echo 'did not install progname to $GOBIN/progname'
   213  	./testgo list -f 'Target: {{.Target}}' cmd/api
   214  	ok=false
   215  fi
   216  rm -f $d/gobin/progname $d/bin/progname
   217  
   218  TEST gopath program installs into GOPATH/bin
   219  ./testgo install progname
   220  if [ ! -x $d/bin/progname ]; then
   221  	echo 'did not install progname to $GOPATH/bin/progname'
   222  	./testgo list -f 'Target: {{.Target}}' progname
   223  	ok=false
   224  fi
   225  
   226  unset GOPATH
   227  rm -rf $d
   228  
   229  # Reject relative paths in GOPATH.
   230  TEST reject relative paths in GOPATH '(command-line package)'
   231  if GOPATH=. ./testgo build testdata/src/go-cmd-test/helloworld.go; then
   232      echo 'GOPATH="." go build should have failed, did not'
   233      ok=false
   234  fi
   235  
   236  TEST reject relative paths in GOPATH 
   237  if GOPATH=:$(pwd)/testdata:. ./testgo build go-cmd-test; then
   238      echo 'GOPATH=":$(pwd)/testdata:." go build should have failed, did not'
   239      ok=false
   240  fi
   241  
   242  # issue 4104
   243  TEST go test with package listed multiple times
   244  if [ $(./testgo test fmt fmt fmt fmt fmt | wc -l) -ne 1 ] ; then
   245      echo 'go test fmt fmt fmt fmt fmt tested the same package multiple times'
   246      ok=false
   247  fi
   248  
   249  # ensure that output of 'go list' is consistent between runs
   250  TEST go list is consistent
   251  ./testgo list std > test_std.list
   252  if ! ./testgo list std | cmp -s test_std.list - ; then
   253  	echo "go list std ordering is inconsistent"
   254  	ok=false
   255  fi
   256  rm -f test_std.list
   257  
   258  # issue 4096. Validate the output of unsuccessful go install foo/quxx 
   259  TEST unsuccessful go install should mention missing package
   260  if [ $(./testgo install 'foo/quxx' 2>&1 | grep -c 'cannot find package "foo/quxx" in any of') -ne 1 ] ; then
   261  	echo 'go install foo/quxx expected error: .*cannot find package "foo/quxx" in any of'
   262  	ok=false
   263  fi 
   264  # test GOROOT search failure is reported
   265  TEST GOROOT search failure reporting
   266  if [ $(./testgo install 'foo/quxx' 2>&1 | egrep -c 'foo/quxx \(from \$GOROOT\)$') -ne 1 ] ; then
   267          echo 'go install foo/quxx expected error: .*foo/quxx (from $GOROOT)'
   268          ok=false
   269  fi
   270  # test multiple GOPATH entries are reported separately
   271  TEST multiple GOPATH entries reported separately
   272  if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/./src/foo/quxx') -ne 2 ] ; then
   273          echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)\n.*testdata/b/src/foo/quxx'
   274          ok=false
   275  fi
   276  # test (from $GOPATH) annotation is reported for the first GOPATH entry
   277  TEST mention GOPATH in first GOPATH entry
   278  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
   279          echo 'go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)'
   280          ok=false
   281  fi
   282  # but not on the second
   283  TEST but not the second entry
   284  if [ $(GOPATH=$(pwd)/testdata/a:$(pwd)/testdata/b ./testgo install 'foo/quxx' 2>&1 | egrep -c 'testdata/b/src/foo/quxx$') -ne 1 ] ; then
   285          echo 'go install foo/quxx expected error: .*testdata/b/src/foo/quxx'
   286          ok=false
   287  fi
   288  # test missing GOPATH is reported
   289  TEST missing GOPATH is reported
   290  if [ $(GOPATH= ./testgo install 'foo/quxx' 2>&1 | egrep -c '\(\$GOPATH not set\)$') -ne 1 ] ; then
   291          echo 'go install foo/quxx expected error: ($GOPATH not set)'
   292          ok=false
   293  fi
   294  
   295  # issue 4186. go get cannot be used to download packages to $GOROOT
   296  # Test that without GOPATH set, go get should fail
   297  TEST without GOPATH, go get fails
   298  d=$(mktemp -d -t testgoXXX)
   299  mkdir -p $d/src/pkg
   300  if GOPATH= GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch ; then 
   301  	echo 'go get code.google.com/p/go.codereview/cmd/hgpatch should not succeed with $GOPATH unset'
   302  	ok=false
   303  fi	
   304  rm -rf $d
   305  
   306  # Test that with GOPATH=$GOROOT, go get should fail
   307  TEST with GOPATH=GOROOT, go get fails
   308  d=$(mktemp -d -t testgoXXX)
   309  mkdir -p $d/src/pkg
   310  if GOPATH=$d GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch ; then
   311          echo 'go get code.google.com/p/go.codereview/cmd/hgpatch should not succeed with GOPATH=$GOROOT'
   312          ok=false
   313  fi
   314  rm -rf $d
   315  
   316  TEST ldflags arguments with spaces '(issue 3941)'
   317  d=$(mktemp -d -t testgoXXX)
   318  cat >$d/main.go<<EOF
   319  package main
   320  var extern string
   321  func main() {
   322  	println(extern)
   323  }
   324  EOF
   325  ./testgo run -ldflags '-X main.extern "hello world"' $d/main.go 2>hello.out
   326  if ! grep -q '^hello world' hello.out; then
   327  	echo "ldflags -X main.extern 'hello world' failed. Output:"
   328  	cat hello.out
   329  	ok=false
   330  fi
   331  rm -rf $d
   332  
   333  TEST go test -cpuprofile leaves binary behind
   334  ./testgo test -cpuprofile strings.prof strings || ok=false
   335  if [ ! -x strings.test ]; then
   336  	echo "go test -cpuprofile did not create strings.test"
   337  	ok=false
   338  fi
   339  rm -f strings.prof strings.test
   340  
   341  TEST symlinks do not confuse go list '(issue 4568)'
   342  old=$(pwd)
   343  tmp=$(cd /tmp && pwd -P)
   344  d=$(TMPDIR=$tmp mktemp -d -t testgoXXX)
   345  mkdir -p $d/src
   346  (
   347  	ln -s $d $d/src/dir1
   348  	cd $d/src/dir1
   349  	echo package p >p.go
   350  	export GOPATH=$d
   351  	if [ "$($old/testgo list -f '{{.Root}}' .)" != "$d" ]; then
   352  		echo Confused by symlinks.
   353  		echo "Package in current directory $(pwd) should have Root $d"
   354  		env|grep WD
   355  		$old/testgo list -json . dir1
   356  		touch $d/failed
   357  	fi		
   358  )
   359  if [ -f $d/failed ]; then
   360  	ok=false
   361  fi
   362  rm -rf $d
   363  
   364  TEST 'install with tags (issue 4515)'
   365  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   366  mkdir -p $d/src/example/a $d/src/example/b $d/bin
   367  cat >$d/src/example/a/main.go <<EOF
   368  package main
   369  func main() {}
   370  EOF
   371  cat >$d/src/example/b/main.go <<EOF
   372  // +build mytag
   373  
   374  package main
   375  func main() {}
   376  EOF
   377  GOPATH=$d ./testgo install -tags mytag example/a example/b || ok=false
   378  if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then
   379  	echo go install example/a example/b did not install binaries
   380  	ok=false
   381  fi
   382  rm -f $d/bin/*
   383  GOPATH=$d ./testgo install -tags mytag example/... || ok=false
   384  if [ ! -x $d/bin/a -o ! -x $d/bin/b ]; then
   385  	echo go install example/... did not install binaries
   386  	ok=false
   387  fi
   388  rm -f $d/bin/*go
   389  export GOPATH=$d
   390  if [ "$(./testgo list -tags mytag example/b...)" != "example/b" ]; then
   391  	echo go list example/b did not find example/b
   392  	ok=false
   393  fi
   394  unset GOPATH
   395  rm -rf $d
   396  
   397  TEST case collisions '(issue 4773)'
   398  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   399  export GOPATH=$d
   400  mkdir -p $d/src/example/a $d/src/example/b
   401  cat >$d/src/example/a/a.go <<EOF
   402  package p
   403  import (
   404  	_ "math/rand"
   405  	_ "math/Rand"
   406  )
   407  EOF
   408  if ./testgo list example/a 2>$d/out; then
   409  	echo go list example/a should have failed, did not.
   410  	ok=false
   411  elif ! grep "case-insensitive import collision" $d/out >/dev/null; then
   412  	echo go list example/a did not report import collision.
   413  	ok=false
   414  fi
   415  cat >$d/src/example/b/file.go <<EOF
   416  package b
   417  EOF
   418  cat >$d/src/example/b/FILE.go <<EOF
   419  package b
   420  EOF
   421  if [ $(ls $d/src/example/b | wc -l) = 2 ]; then
   422  	# case-sensitive file system, let directory read find both files
   423  	args="example/b"
   424  else
   425  	# case-insensitive file system, list files explicitly on command line.
   426  	args="$d/src/example/b/file.go $d/src/example/b/FILE.go"
   427  fi
   428  if ./testgo list $args 2>$d/out; then
   429  	echo go list example/b should have failed, did not.
   430  	ok=false
   431  elif ! grep "case-insensitive file name collision" $d/out >/dev/null; then
   432  	echo go list example/b did not report file name collision.
   433  	ok=false
   434  fi
   435  
   436  TEST go get cover
   437  ./testgo get code.google.com/p/go.tools/cmd/cover || ok=false
   438  
   439  unset GOPATH
   440  rm -rf $d
   441  
   442  # Only succeeds if source order is preserved.
   443  TEST source file name order preserved
   444  ./testgo test testdata/example[12]_test.go || ok=false
   445  
   446  # Check that coverage analysis works at all.
   447  # Don't worry about the exact numbers
   448  TEST coverage runs
   449  ./testgo test -short -coverpkg=strings strings regexp || ok=false
   450  ./testgo test -short -cover strings math regexp || ok=false
   451  
   452  TEST cgo depends on syscall
   453  rm -rf $GOROOT/pkg/*_race
   454  d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
   455  export GOPATH=$d
   456  mkdir -p $d/src/foo
   457  echo '
   458  package foo
   459  //#include <stdio.h>
   460  import "C"
   461  ' >$d/src/foo/foo.go
   462  ./testgo build -race foo || ok=false
   463  rm -rf $d
   464  unset GOPATH
   465  
   466  # clean up
   467  if $started; then stop; fi
   468  rm -rf testdata/bin testdata/bin1
   469  rm -f testgo
   470  
   471  if $allok; then
   472  	echo PASS
   473  else
   474  	echo FAIL
   475  	exit 1
   476  fi