github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/testsuite/gotest (about) 1 #!/bin/sh 2 # Copyright 2009 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 # Using all the *_test.go files in the current directory, write out a file 7 # _testmain.go that runs all its tests. Compile everything and run the 8 # tests. 9 # If files are named on the command line, use them instead of *_test.go. 10 11 # Makes egrep,grep work better in general if we put them 12 # in ordinary C mode instead of what the current language is. 13 unset LANG 14 export LC_ALL=C 15 export LC_CTYPE=C 16 17 GC=${GC:-gccgo} 18 GL=${GL:-${GC-gccgo}} 19 GOLIBS=${GOLIBS:-} 20 export GC GL GOLIBS 21 22 NM=${NM:-nm} 23 24 # srcdir is where the source files are found. basedir is where the 25 # source file paths are relative to. 26 # gofiles are the test files. pkgfiles are the source files. 27 srcdir=. 28 basedir=. 29 gofiles="" 30 pkgfiles="" 31 loop=true 32 keep=false 33 pkgpath= 34 prefix= 35 dejagnu=no 36 GOARCH="" 37 timeout=240 38 testname="" 39 bench="" 40 trace=false 41 while $loop; do 42 case "x$1" in 43 x--srcdir) 44 srcdir=$2 45 shift 46 shift 47 ;; 48 x--srcdir=*) 49 srcdir=`echo $1 | sed -e 's/^--srcdir=//'` 50 shift 51 ;; 52 x--basedir) 53 basedir=$2 54 shift 55 shift 56 ;; 57 x--basedir=*) 58 basedir=`echo $1 | sed -e 's/^--basedir=//'` 59 shift 60 ;; 61 x--pkgpath) 62 pkgpath=$2 63 shift 64 shift 65 ;; 66 x--pkgpath=*) 67 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'` 68 shift 69 ;; 70 x--prefix) 71 prefix=$2 72 shift 73 shift 74 ;; 75 x--prefix=*) 76 prefix=`echo $1 | sed -e 's/^--prefix=//'` 77 shift 78 ;; 79 x--keep) 80 keep=true 81 shift 82 ;; 83 x--pkgfiles) 84 pkgfiles=$2 85 shift 86 shift 87 ;; 88 x--pkgfiles=*) 89 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'` 90 shift 91 ;; 92 x--dejagnu) 93 dejagnu=$2 94 shift 95 shift 96 ;; 97 x--dejagnu=*) 98 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'` 99 shift 100 ;; 101 x--goarch) 102 GOARCH=$2 103 shift 104 shift 105 ;; 106 x--goarch=*) 107 GOARCH=`echo $1 | sed -e 's/^--goarch=//'` 108 shift 109 ;; 110 x--timeout) 111 timeout=$2 112 shift 113 shift 114 ;; 115 x--timeout=*) 116 timeout=`echo $1 | sed -e 's/^--timeout=//'` 117 shift 118 ;; 119 x--testname) 120 testname=$2 121 shift 122 shift 123 ;; 124 x--testname=*) 125 testname=`echo $1 | sed -e 's/^--testname=//'` 126 shift 127 ;; 128 x--bench) 129 bench=$2 130 shift 131 shift 132 ;; 133 x--bench=*) 134 bench=`echo $1 | sed -e 's/^--bench=//'` 135 shift 136 ;; 137 x--trace) 138 trace=true 139 shift 140 ;; 141 x-*) 142 loop=false 143 ;; 144 x) 145 loop=false 146 ;; 147 *) 148 gofiles="$gofiles $1" 149 shift 150 ;; 151 esac 152 done 153 154 DIR=gotest$$ 155 rm -rf $DIR 156 mkdir $DIR 157 158 cd $DIR 159 mkdir test 160 cd test 161 162 if test $keep = false; then 163 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15 164 else 165 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15 166 fi 167 168 case "$srcdir" in 169 /*) 170 ;; 171 *) 172 srcdir="../../$srcdir" 173 ;; 174 esac 175 176 SRCDIR=$srcdir 177 export SRCDIR 178 179 case "$basedir" in 180 /*) 181 ;; 182 *) 183 basedir="../../$basedir" 184 ;; 185 esac 186 187 # Link all the files/directories in srcdir into our working directory, 188 # so that the tests do not have to refer to srcdir to find test data. 189 ln -s $srcdir/* . 190 191 # Some tests refer to a ../testdata directory. 192 if test -e $srcdir/../testdata; then 193 rm -f ../testdata 194 abssrcdir=`cd $srcdir && pwd` 195 ln -s $abssrcdir/../testdata ../testdata 196 fi 197 198 # Copy the .go files because io/utils_test.go expects a regular file. 199 case "x$gofiles" in 200 x) 201 case "x$pkgfiles" in 202 x) 203 for f in `cd $srcdir; ls *.go`; do 204 rm -f $f; 205 cp $srcdir/$f . 206 done 207 ;; 208 *) 209 for f in $pkgfiles; do 210 if test -f $basedir/$f; then 211 b=`basename $f` 212 rm -f $b 213 cp $basedir/$f $b 214 elif test -f ../../$f; then 215 b=`basename $f` 216 rm -f $b 217 cp ../../$f $b 218 else 219 echo "file $f not found" 1>&2 220 exit 1 221 fi 222 done 223 for f in `cd $srcdir; ls *_test.go`; do 224 rm -f $f 225 cp $srcdir/$f . 226 done 227 ;; 228 esac 229 ;; 230 *) 231 for f in $gofiles; do 232 b=`basename $f` 233 rm -f $b 234 cp $basedir/$f $b 235 done 236 case "x$pkgfiles" in 237 x) 238 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do 239 rm -f $f 240 cp $srcdir/$f . 241 done 242 ;; 243 *) 244 for f in $pkgfiles; do 245 if test -f $basedir/$f; then 246 b=`basename $f` 247 rm -f $b 248 cp $basedir/$f $b 249 elif test -f ../../$f; then 250 b=`basename $f` 251 rm -f $b 252 cp ../../$f $b 253 else 254 echo "file $f not found" 1>&2 255 exit 1 256 fi 257 done 258 ;; 259 esac 260 ;; 261 esac 262 263 # Some tests expect the _obj directory created by the gc Makefiles. 264 mkdir _obj 265 266 # Some tests expect the _test directory created by the gc Makefiles. 267 mkdir _test 268 269 case "x$gofiles" in 270 x) 271 gofiles=`ls *_test.go 2>/dev/null` 272 ;; 273 *) 274 xgofiles=$gofiles 275 gofiles= 276 for f in $xgofiles; do 277 gofiles="$gofiles `basename $f`" 278 done 279 esac 280 281 case "x$gofiles" in 282 x) 283 echo 'no test files found' 1>&2 284 exit 1 285 ;; 286 esac 287 288 # Run any commands given in sources, like 289 # // gotest: $GC foo.go 290 # to build any test-only dependencies. 291 holdGC="$GC" 292 GC="$GC -g -c -I ." 293 sed -n 's/^\/\/ gotest: //p' $gofiles | sh 294 GC="$holdGC" 295 296 case "x$pkgfiles" in 297 x) 298 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null` 299 ;; 300 *) 301 for f in $pkgfiles; do 302 pkgbasefiles="$pkgbasefiles `basename $f`" 303 done 304 ;; 305 esac 306 307 case "x$pkgfiles" in 308 x) 309 echo 'no source files found' 1>&2 310 exit 1 311 ;; 312 esac 313 314 # Split $gofiles into external gofiles (those in *_test packages) 315 # and internal ones (those in the main package). 316 xgofiles= 317 for f in $gofiles; do 318 package=`grep '^package[ ]' $f | sed 1q` 319 case "$package" in 320 *_test) 321 xgofiles="$xgofiles $f" 322 ;; 323 *) 324 ngofiles="$ngofiles $f" 325 ;; 326 esac 327 done 328 gofiles=$ngofiles 329 330 # External $O file 331 xofile="" 332 havex=false 333 if [ "x$xgofiles" != "x" ]; then 334 xofile="_xtest_.o" 335 havex=true 336 fi 337 338 testmain= 339 if $havex && fgrep 'func TestMain(' $xgofiles >/dev/null 2>&1; then 340 package=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'` 341 testmain="${package}.TestMain" 342 elif test -n "$gofiles" && fgrep 'func TestMain(' $gofiles >/dev/null 2>&1; then 343 package=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'` 344 testmain="${package}.TestMain" 345 fi 346 347 set -e 348 349 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'` 350 351 pkgpatharg= 352 xpkgpatharg= 353 prefixarg= 354 if test -n "$pkgpath"; then 355 pkgpatharg="-fgo-pkgpath=$pkgpath" 356 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test" 357 elif test -n "$prefix"; then 358 prefixarg="-fgo-prefix=$prefix" 359 fi 360 361 if test "$trace" = "true"; then 362 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles 363 fi 364 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles 365 366 if $havex; then 367 mkdir -p `dirname $package` 368 cp _gotest_.o `dirname $package`/lib`basename $package`.a 369 if test "$trace" = "true"; then 370 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles 371 fi 372 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles 373 fi 374 375 # They all compile; now generate the code to call them. 376 377 testname() { 378 # Remove the package from the name used with the -test option. 379 echo $1 | sed 's/^.*\.//' 380 } 381 382 localname() { 383 # The package main has been renamed to __main__ when imported. 384 # Adjust its uses. 385 echo $1 | sed 's/^main\./__main__./' 386 } 387 388 { 389 text="T" 390 case "$GOARCH" in 391 ppc64*) text="[TD]" ;; 392 esac 393 394 symtogo='sed -e s/_test/XXXtest/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/' 395 396 # test functions are named TestFoo 397 # the grep -v eliminates methods and other special names 398 # that have multiple dots. 399 pattern='Test([^a-z].*)?' 400 # The -p option tells GNU nm not to sort. 401 # The -v option tells Solaris nm to sort by value. 402 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo) 403 if [ "x$tests" = x ]; then 404 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2 405 exit 2 406 fi 407 # benchmarks are named BenchmarkFoo. 408 pattern='Benchmark([^a-z].*)?' 409 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo) 410 411 # examples are named ExampleFoo 412 pattern='Example([^a-z].*)?' 413 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '\..*\..*\.' | fgrep -v '$' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo) 414 415 # package spec 416 echo 'package main' 417 echo 418 # imports 419 if echo "$tests" | egrep -v '_test\.' >/dev/null; then 420 echo 'import "./_gotest_"' 421 fi 422 if $havex; then 423 echo 'import "./_xtest_"' 424 fi 425 echo 'import "testing"' 426 echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp 427 if ! test -n "$testmain"; then 428 echo 'import __os__ "os"' 429 fi 430 # test array 431 echo 432 echo 'var tests = []testing.InternalTest {' 433 for i in $tests 434 do 435 n=$(testname $i) 436 if test "$n" != "TestMain"; then 437 j=$(localname $i) 438 echo ' {"'$n'", '$j'},' 439 fi 440 done 441 echo '}' 442 443 # benchmark array 444 # The comment makes the multiline declaration 445 # gofmt-safe even when there are no benchmarks. 446 echo 'var benchmarks = []testing.InternalBenchmark{ //' 447 for i in $benchmarks 448 do 449 n=$(testname $i) 450 j=$(localname $i) 451 echo ' {"'$n'", '$j'},' 452 done 453 echo '}' 454 455 # examples array 456 echo 'var examples = []testing.InternalExample{ //' 457 # This doesn't work because we don't pick up the output. 458 #for i in $examples 459 #do 460 # n=$(testname $i) 461 # j=$(localname $i) 462 # echo ' {"'$n'", '$j', ""},' 463 #done 464 echo '}' 465 466 # body 467 echo \ 468 ' 469 var matchPat string 470 var matchRe *__regexp__.Regexp 471 472 func matchString(pat, str string) (result bool, err error) { 473 if matchRe == nil || matchPat != pat { 474 matchPat = pat 475 matchRe, err = __regexp__.Compile(matchPat) 476 if err != nil { 477 return 478 } 479 } 480 return matchRe.MatchString(str), nil 481 } 482 483 func main() { 484 m := testing.MainStart(matchString, tests, benchmarks, examples) 485 ' 486 if test -n "$testmain"; then 487 echo " ${testmain}(m)" 488 else 489 echo ' __os__.Exit(m.Run())' 490 fi 491 492 echo '}' 493 }>_testmain.go 494 495 case "x$dejagnu" in 496 xno) 497 if test "$trace" = "true"; then 498 echo ${GC} -g -c _testmain.go 499 fi 500 ${GC} -g -c _testmain.go 501 502 if test "$trace" = "true"; then 503 echo ${GL} *.o ${GOLIBS} 504 fi 505 ${GL} *.o ${GOLIBS} 506 507 if test "$bench" = ""; then 508 if test "$trace" = "true"; then 509 echo ./a.out -test.short -test.timeout=${timeout}s "$@" 510 fi 511 ./a.out -test.short -test.timeout=${timeout}s "$@" & 512 pid=$! 513 (sleep `expr $timeout + 10` 514 echo > gotest-timeout 515 echo "timed out in gotest" 1>&2 516 kill -9 $pid) & 517 alarmpid=$! 518 wait $pid 519 status=$? 520 if ! test -f gotest-timeout; then 521 kill $alarmpid 522 fi 523 else 524 if test "$trace" = "true"; then 525 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@" 526 fi 527 ./a.out -test.run=^\$ -test.bench="${bench}" "$@" 528 status=$? 529 fi 530 exit $status 531 ;; 532 xyes) 533 rm -rf ../../testsuite/*.o 534 files=`echo *` 535 for f in $files; do 536 if test "$f" = "_obj" || test "$f" = "_test"; then 537 continue 538 fi 539 rm -rf ../../testsuite/$f 540 if test -f $f; then 541 cp $f ../../testsuite/ 542 else 543 ln -s ../$DIR/test/$f ../../testsuite/ 544 fi 545 done 546 cd ../../testsuite 547 rm -rf _obj _test 548 mkdir _obj _test 549 if test "$testname" != ""; then 550 GOTESTNAME="$testname" 551 export GOTESTNAME 552 fi 553 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test" 554 # Useful when using make check-target-libgo 555 cat libgo.log >> libgo-all.log 556 cat libgo.sum >> libgo-all.sum 557 rm -rf $files 558 ;; 559 esac