1#!/usr/bin/env bash
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
6eval $(go tool dist env)
7export GOARCH GOOS GOROOT
8export E=
9
10case X"$GOARCH" in
11Xamd64)
12	export A=6
13	;;
14X386)
15	export A=8
16	;;
17Xarm)
18	export A=5
19	export E="$GORUN"
20	;;
21*)
22	echo 1>&2 run: unsupported '$GOARCH'
23	exit 1
24esac
25
26export G="${A}g ${GCFLAGS}"
27export L=${A}l
28export GOTRACEBACK=0
29export LANG=C
30unset GREP_OPTIONS	# in case user has a non-standard set
31
32unset GOROOT_FINAL  # breaks ./ imports
33
34failed=0
35
36PATH=${GOBIN:-$GOROOT/bin}:`pwd`:/bin:/usr/bin:/usr/local/bin
37
38# TODO: We add the tool directory to the PATH to avoid thinking about a better way.
39PATH="$GOTOOLDIR:$PATH"
40
41RUNFILE="${TMPDIR:-/tmp}/gorun-$$-$USER"
42TMP1FILE="${TMPDIR:-/tmp}/gotest1-$$-$USER"
43TMP2FILE="${TMPDIR:-/tmp}/gotest2-$$-$USER"
44
45# don't run the machine out of memory: limit individual processes to 4GB.
46# on thresher, 3GB suffices to run the tests; with 2GB, peano fails.
47ulimit -v 4000000
48
49# no core files please
50ulimit -c 0
51
52true >pass.out >times.out
53
54exclude=false	# exclude nothing
55golden=golden.out
56
57rm -f tmp.go  # generated by some tests, left behind if interrupted
58
59filterout() {
60	grep '^'"$2"'$' $1 >/dev/null
61}
62
63for dir in . ken chan interface syntax dwarf safe fixedbugs bugs
64do
65	echo
66	echo '==' $dir'/'
67	for i in $(ls $dir/*.go 2>/dev/null)
68	do (
69		if $exclude $i; then
70			exit 0  # continues for loop
71		fi
72		export F=$(basename $i .go)
73		export D=$dir
74		echo '. ./testlib' >"$RUNFILE"
75		sed '/^\/\//!q' $i | sed 's@//@@; $d' |sed 's|./\$A.out|$E &|g' >>"$RUNFILE"
76		if ! { time -p bash -c "bash '$RUNFILE' >'$TMP1FILE' 2>&1" ; } 2>"$TMP2FILE"
77		then
78			echo
79			echo "===========" $i
80			cat "$TMP1FILE"
81			echo >&2 fail: $i
82			echo "# $i	# fail" >>pass.out
83		elif test -s "$TMP1FILE"
84		then
85			echo
86			echo "===========" $i
87			cat "$TMP1FILE"
88			if grep -q '^BUG' "$TMP1FILE"
89			then
90				if [ $dir != bugs ]
91				then
92					echo >&2 bug: $i
93				fi
94				echo "# $i	# fail, BUG" >>pass.out
95			else
96				echo $i >>pass.out
97			fi
98		elif [ $dir = "bugs" ]
99		then
100			echo $i succeeded with no output.
101		else
102			echo $i >>pass.out
103		fi
104		echo $(awk 'NR==1{print $2}' "$TMP2FILE") $D/$F >>times.out
105		rm -f $F.$A $A.out tmp.go
106	) done
107done | # clean up some stack noise
108	egrep -v '^(r[0-9a-z]+|[cfg]s)  +0x'  |
109	sed '/tmp.*Bus error/s/.*Bus/Bus/; /tmp.*Trace.BPT/s/.*Trace/Trace/
110		s!'"$RUNFILE"'!$RUNFILE!g
111		s/^PC=0x[0-9a-f]*/pc: xxx/
112		s/^pc: 0x[0-9a-f]*/pc: xxx/
113		s/PC=0x[0-9a-f]*/PC=xxx/
114		/^Trace\/breakpoint trap/d
115		/^Trace\/BPT trap/d
116		/RUNFILE/ s/line 1: *[0-9]*/line 1: PID/
117		/^\$RUNFILE: line 1: PID Trace\/breakpoint trap/d
118		/Segmentation fault/d
119		/^qemu: uncaught target signal 11 (Segmentation fault) - exiting/d' > run.out
120
121rm -f "$RUNFILE" "$TMP1FILE" "$TMP2FILE" *.$A *.a $A.out
122diffmsg=""
123if ! diff $golden run.out
124then
125	diffmsg="; test output differs"
126	failed=1
127fi
128
129notinbugs=$(sed '/^== bugs/q' run.out | grep -c '^BUG')
130inbugs=$(sed '1,/^== bugs/d' run.out | grep -c '^BUG')
131
132echo 2>&1 $inbugs known bugs';' $notinbugs unexpected bugs$diffmsg
133
134if [ "$failed" != "0" ]; then
135	echo FAILED
136fi
137
138exit $failed
139