blob: 754ab30060395f66416df8d9c96203ab620ed497 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001#!/bin/sh
2#
3# TestRunner.sh - This script is used to run arbitrary unit tests. Unit
4# tests must contain the command used to run them in the input file, starting
5# immediately after a "RUN:" string.
6#
7# This runner recognizes and replaces the following strings in the command:
8#
9# %s - Replaced with the input name of the program, or the program to
10# execute, as appropriate.
Reid Spencer5f016e22007-07-11 17:01:13 +000011# %prcontext - prcontext.tcl script
Gabor Greif1e2db032008-03-20 22:50:54 +000012# %t - temporary file name (derived from testcase name)
Reid Spencer5f016e22007-07-11 17:01:13 +000013#
14
15FILENAME=$1
16TESTNAME=$1
17SUBST=$1
Ted Kremenek6a18c762007-11-28 19:16:54 +000018
19OUTPUT=Output/$1.out
Reid Spencer5f016e22007-07-11 17:01:13 +000020
21# create the output directory if it does not already exist
Ted Kremenek6a18c762007-11-28 19:16:54 +000022mkdir -p `dirname $OUTPUT` > /dev/null 2>&1
Reid Spencer5f016e22007-07-11 17:01:13 +000023
24if test $# != 1; then
25 # If more than one parameter is passed in, there must be three parameters:
26 # The filename to read from (already processed), the command used to execute,
27 # and the file to output to.
28 SUBST=$2
29 OUTPUT=$3
30 TESTNAME=$3
31fi
32
33ulimit -t 40
34
Chris Lattner32a39992007-12-12 06:19:22 +000035# Verify the script contains a run line.
36grep -q 'RUN:' $FILENAME || (
37 echo "******************** TEST '$TESTNAME' HAS NO RUN LINE! ********************"
38 exit 1
39)
40
Gabor Greif1e2db032008-03-20 22:50:54 +000041# Run under valgrind if the VG environment variable has been set.
Daniel Dunbar5e380732008-07-25 01:05:44 +000042CLANG=$CLANG
Eli Friedmanfde9fe72008-07-27 05:05:07 +000043if [ ! -n "$CLANG" ]; then
Daniel Dunbar5e380732008-07-25 01:05:44 +000044 CLANG="clang"
45fi
Daniel Dunbare494d342009-03-24 06:17:45 +000046
47# Resolve the path, and Make sure $CLANG actually exists; otherwise
48# ensuing failures are non-obvious.
49CLANG=$(which "$CLANG")
50if [ -z $CLANG ]; then
51 echo "Couldn't find 'clang' program, try setting CLANG in your environment"
52 exit 1
53fi
54
Gabor Greif1e2db032008-03-20 22:50:54 +000055if [ -n "$VG" ]; then
Nuno Lopesf557fb72008-09-04 18:33:57 +000056 rm -f $OUTPUT.vg
57 CLANG="valgrind --leak-check=full --quiet --log-file=$OUTPUT.vg $CLANG"
Gabor Greif1e2db032008-03-20 22:50:54 +000058fi
59
Daniel Dunbare494d342009-03-24 06:17:45 +000060# Assuming $CLANG is correct, use it to derive clang-cc. We expect to
61# be looking in a build directory, so just add '-cc'.
62CLANGCC=$CLANGCC
63if [ ! -n "$CLANGCC" ]; then
64 CLANGCC="$CLANG-cc"
65fi
66
67# Try to sanity check $CLANGCC too
68CLANGCC=$(which "$CLANGCC")
69if [ -z "$CLANGCC" ]; then
70 echo "Couldn't find 'clang-cc' program, make sure clang is found in your build directory"
71 exit 1
72fi
73
Reid Spencer5f016e22007-07-11 17:01:13 +000074SCRIPT=$OUTPUT.script
Gabor Greif0f233032008-03-17 13:45:47 +000075TEMPOUTPUT=$OUTPUT.tmp
Gabor Greif1e2db032008-03-20 22:50:54 +000076grep 'RUN:' $FILENAME | \
77 sed -e "s|^.*RUN:\(.*\)$|\1|g" \
Daniel Dunbare494d342009-03-24 06:17:45 +000078 -e "s| clang | $CLANG |g" \
79 -e "s| clang-cc | $CLANGCC |g" \
Gabor Greif1e2db032008-03-20 22:50:54 +000080 -e "s|%s|$SUBST|g" \
Gabor Greif1e2db032008-03-20 22:50:54 +000081 -e "s|%prcontext|prcontext.tcl|g" \
Nuno Lopesf557fb72008-09-04 18:33:57 +000082 -e "s|%t|$TEMPOUTPUT|g" > $SCRIPT
Reid Spencer5f016e22007-07-11 17:01:13 +000083
Daniel Dunbar607b17b2008-09-04 00:30:11 +000084IS_XFAIL=0
85if (grep -q XFAIL $FILENAME); then
86 IS_XFAIL=1
87 printf "XFAILED '$TESTNAME': "
88 grep XFAIL $FILENAME
89fi
Reid Spencer5f016e22007-07-11 17:01:13 +000090
Gabor Greif1e2db032008-03-20 22:50:54 +000091/bin/sh $SCRIPT > $OUTPUT 2>&1
92SCRIPT_STATUS=$?
93
94if [ -n "$VG" ]; then
Nuno Lopesf557fb72008-09-04 18:33:57 +000095 [ ! -s $OUTPUT.vg ]
96 VG_STATUS=$?
Gabor Greif1e2db032008-03-20 22:50:54 +000097else
98 VG_STATUS=0
99fi
100
Daniel Dunbar607b17b2008-09-04 00:30:11 +0000101if [ $IS_XFAIL -ne 0 ]; then
102 if [ $SCRIPT_STATUS -ne 0 ]; then
103 SCRIPT_STATUS=0
104 else
105 SCRIPT_STATUS=1
106 fi
107fi
108
Gabor Greif1e2db032008-03-20 22:50:54 +0000109if [ $SCRIPT_STATUS -ne 0 -o $VG_STATUS -ne 0 ]; then
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 echo "******************** TEST '$TESTNAME' FAILED! ********************"
111 echo "Command: "
112 cat $SCRIPT
Gabor Greif1e2db032008-03-20 22:50:54 +0000113 if [ $SCRIPT_STATUS -eq 0 ]; then
114 echo "Output:"
Daniel Dunbar607b17b2008-09-04 00:30:11 +0000115 elif [ $IS_XFAIL -ne 0 ]; then
116 echo "Incorrect Output (Expected Failure):"
Gabor Greif1e2db032008-03-20 22:50:54 +0000117 else
118 echo "Incorrect Output:"
119 fi
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 cat $OUTPUT
Gabor Greif1e2db032008-03-20 22:50:54 +0000121 if [ $VG_STATUS -ne 0 ]; then
122 echo "Valgrind Output:"
Nuno Lopesf557fb72008-09-04 18:33:57 +0000123 cat $OUTPUT.vg
Gabor Greif1e2db032008-03-20 22:50:54 +0000124 fi
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 echo "******************** TEST '$TESTNAME' FAILED! ********************"
Gabor Greif5ca1b5a2008-03-17 12:35:00 +0000126 exit 1
Gabor Greif1e2db032008-03-20 22:50:54 +0000127fi