gotest 11 KB

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