blob: bb20728578c1caca44d2ad1db4afd69ff92f8c2f [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.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011# %S - Replaced with the directory where the input file resides
Reid Spencer5f016e22007-07-11 17:01:13 +000012# %prcontext - prcontext.tcl script
Gabor Greif1e2db032008-03-20 22:50:54 +000013# %t - temporary file name (derived from testcase name)
Reid Spencer5f016e22007-07-11 17:01:13 +000014#
15
16FILENAME=$1
17TESTNAME=$1
18SUBST=$1
Douglas Gregor2cf26342009-04-09 22:27:44 +000019FILEDIR=`dirname $TESTNAME`
Ted Kremenek6a18c762007-11-28 19:16:54 +000020
21OUTPUT=Output/$1.out
Reid Spencer5f016e22007-07-11 17:01:13 +000022
23# create the output directory if it does not already exist
Ted Kremenek6a18c762007-11-28 19:16:54 +000024mkdir -p `dirname $OUTPUT` > /dev/null 2>&1
Reid Spencer5f016e22007-07-11 17:01:13 +000025
26if test $# != 1; then
27 # If more than one parameter is passed in, there must be three parameters:
28 # The filename to read from (already processed), the command used to execute,
29 # and the file to output to.
30 SUBST=$2
31 OUTPUT=$3
32 TESTNAME=$3
33fi
34
35ulimit -t 40
36
Chris Lattner32a39992007-12-12 06:19:22 +000037# Verify the script contains a run line.
38grep -q 'RUN:' $FILENAME || (
39 echo "******************** TEST '$TESTNAME' HAS NO RUN LINE! ********************"
40 exit 1
41)
42
Gabor Greif1e2db032008-03-20 22:50:54 +000043# Run under valgrind if the VG environment variable has been set.
Daniel Dunbar5e380732008-07-25 01:05:44 +000044CLANG=$CLANG
Eli Friedmanfde9fe72008-07-27 05:05:07 +000045if [ ! -n "$CLANG" ]; then
Daniel Dunbar5e380732008-07-25 01:05:44 +000046 CLANG="clang"
47fi
Daniel Dunbare494d342009-03-24 06:17:45 +000048
49# Resolve the path, and Make sure $CLANG actually exists; otherwise
50# ensuing failures are non-obvious.
51CLANG=$(which "$CLANG")
52if [ -z $CLANG ]; then
53 echo "Couldn't find 'clang' program, try setting CLANG in your environment"
54 exit 1
55fi
56
Gabor Greif1e2db032008-03-20 22:50:54 +000057if [ -n "$VG" ]; then
Nuno Lopesf557fb72008-09-04 18:33:57 +000058 rm -f $OUTPUT.vg
59 CLANG="valgrind --leak-check=full --quiet --log-file=$OUTPUT.vg $CLANG"
Gabor Greif1e2db032008-03-20 22:50:54 +000060fi
61
Daniel Dunbare494d342009-03-24 06:17:45 +000062# Assuming $CLANG is correct, use it to derive clang-cc. We expect to
63# be looking in a build directory, so just add '-cc'.
64CLANGCC=$CLANGCC
65if [ ! -n "$CLANGCC" ]; then
66 CLANGCC="$CLANG-cc"
67fi
68
69# Try to sanity check $CLANGCC too
70CLANGCC=$(which "$CLANGCC")
Daniel Dunbar5ddce192009-05-02 20:08:07 +000071# If that failed, ask clang.
72if [ -z "$CLANGCC" ]; then
73 CLANGCC=$($CLANG -print-prog-name=clang-cc)
74fi
Daniel Dunbare494d342009-03-24 06:17:45 +000075if [ -z "$CLANGCC" ]; then
76 echo "Couldn't find 'clang-cc' program, make sure clang is found in your build directory"
77 exit 1
78fi
79
Reid Spencer5f016e22007-07-11 17:01:13 +000080SCRIPT=$OUTPUT.script
Gabor Greif0f233032008-03-17 13:45:47 +000081TEMPOUTPUT=$OUTPUT.tmp
Gabor Greif1e2db032008-03-20 22:50:54 +000082grep 'RUN:' $FILENAME | \
83 sed -e "s|^.*RUN:\(.*\)$|\1|g" \
Daniel Dunbare494d342009-03-24 06:17:45 +000084 -e "s| clang | $CLANG |g" \
85 -e "s| clang-cc | $CLANGCC |g" \
Gabor Greif1e2db032008-03-20 22:50:54 +000086 -e "s|%s|$SUBST|g" \
Douglas Gregor2cf26342009-04-09 22:27:44 +000087 -e "s|%S|$FILEDIR|g" \
Gabor Greif1e2db032008-03-20 22:50:54 +000088 -e "s|%prcontext|prcontext.tcl|g" \
Nuno Lopesf557fb72008-09-04 18:33:57 +000089 -e "s|%t|$TEMPOUTPUT|g" > $SCRIPT
Reid Spencer5f016e22007-07-11 17:01:13 +000090
Daniel Dunbar607b17b2008-09-04 00:30:11 +000091IS_XFAIL=0
92if (grep -q XFAIL $FILENAME); then
93 IS_XFAIL=1
94 printf "XFAILED '$TESTNAME': "
95 grep XFAIL $FILENAME
96fi
Reid Spencer5f016e22007-07-11 17:01:13 +000097
Gabor Greif1e2db032008-03-20 22:50:54 +000098/bin/sh $SCRIPT > $OUTPUT 2>&1
99SCRIPT_STATUS=$?
100
101if [ -n "$VG" ]; then
Nuno Lopesf557fb72008-09-04 18:33:57 +0000102 [ ! -s $OUTPUT.vg ]
103 VG_STATUS=$?
Gabor Greif1e2db032008-03-20 22:50:54 +0000104else
105 VG_STATUS=0
106fi
107
Daniel Dunbar607b17b2008-09-04 00:30:11 +0000108if [ $IS_XFAIL -ne 0 ]; then
109 if [ $SCRIPT_STATUS -ne 0 ]; then
110 SCRIPT_STATUS=0
111 else
112 SCRIPT_STATUS=1
113 fi
114fi
115
Gabor Greif1e2db032008-03-20 22:50:54 +0000116if [ $SCRIPT_STATUS -ne 0 -o $VG_STATUS -ne 0 ]; then
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 echo "******************** TEST '$TESTNAME' FAILED! ********************"
118 echo "Command: "
119 cat $SCRIPT
Gabor Greif1e2db032008-03-20 22:50:54 +0000120 if [ $SCRIPT_STATUS -eq 0 ]; then
121 echo "Output:"
Daniel Dunbar607b17b2008-09-04 00:30:11 +0000122 elif [ $IS_XFAIL -ne 0 ]; then
123 echo "Incorrect Output (Expected Failure):"
Gabor Greif1e2db032008-03-20 22:50:54 +0000124 else
125 echo "Incorrect Output:"
126 fi
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 cat $OUTPUT
Gabor Greif1e2db032008-03-20 22:50:54 +0000128 if [ $VG_STATUS -ne 0 ]; then
129 echo "Valgrind Output:"
Nuno Lopesf557fb72008-09-04 18:33:57 +0000130 cat $OUTPUT.vg
Gabor Greif1e2db032008-03-20 22:50:54 +0000131 fi
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 echo "******************** TEST '$TESTNAME' FAILED! ********************"
Gabor Greif5ca1b5a2008-03-17 12:35:00 +0000133 exit 1
Gabor Greif1e2db032008-03-20 22:50:54 +0000134fi