blob: 66c1e1eab5200ebebc9a4ba3fc7077d964f09205 [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")
71if [ -z "$CLANGCC" ]; then
72 echo "Couldn't find 'clang-cc' program, make sure clang is found in your build directory"
73 exit 1
74fi
75
Reid Spencer5f016e22007-07-11 17:01:13 +000076SCRIPT=$OUTPUT.script
Gabor Greif0f233032008-03-17 13:45:47 +000077TEMPOUTPUT=$OUTPUT.tmp
Gabor Greif1e2db032008-03-20 22:50:54 +000078grep 'RUN:' $FILENAME | \
79 sed -e "s|^.*RUN:\(.*\)$|\1|g" \
Daniel Dunbare494d342009-03-24 06:17:45 +000080 -e "s| clang | $CLANG |g" \
81 -e "s| clang-cc | $CLANGCC |g" \
Gabor Greif1e2db032008-03-20 22:50:54 +000082 -e "s|%s|$SUBST|g" \
Douglas Gregor2cf26342009-04-09 22:27:44 +000083 -e "s|%S|$FILEDIR|g" \
Gabor Greif1e2db032008-03-20 22:50:54 +000084 -e "s|%prcontext|prcontext.tcl|g" \
Nuno Lopesf557fb72008-09-04 18:33:57 +000085 -e "s|%t|$TEMPOUTPUT|g" > $SCRIPT
Reid Spencer5f016e22007-07-11 17:01:13 +000086
Daniel Dunbar607b17b2008-09-04 00:30:11 +000087IS_XFAIL=0
88if (grep -q XFAIL $FILENAME); then
89 IS_XFAIL=1
90 printf "XFAILED '$TESTNAME': "
91 grep XFAIL $FILENAME
92fi
Reid Spencer5f016e22007-07-11 17:01:13 +000093
Gabor Greif1e2db032008-03-20 22:50:54 +000094/bin/sh $SCRIPT > $OUTPUT 2>&1
95SCRIPT_STATUS=$?
96
97if [ -n "$VG" ]; then
Nuno Lopesf557fb72008-09-04 18:33:57 +000098 [ ! -s $OUTPUT.vg ]
99 VG_STATUS=$?
Gabor Greif1e2db032008-03-20 22:50:54 +0000100else
101 VG_STATUS=0
102fi
103
Daniel Dunbar607b17b2008-09-04 00:30:11 +0000104if [ $IS_XFAIL -ne 0 ]; then
105 if [ $SCRIPT_STATUS -ne 0 ]; then
106 SCRIPT_STATUS=0
107 else
108 SCRIPT_STATUS=1
109 fi
110fi
111
Gabor Greif1e2db032008-03-20 22:50:54 +0000112if [ $SCRIPT_STATUS -ne 0 -o $VG_STATUS -ne 0 ]; then
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 echo "******************** TEST '$TESTNAME' FAILED! ********************"
114 echo "Command: "
115 cat $SCRIPT
Gabor Greif1e2db032008-03-20 22:50:54 +0000116 if [ $SCRIPT_STATUS -eq 0 ]; then
117 echo "Output:"
Daniel Dunbar607b17b2008-09-04 00:30:11 +0000118 elif [ $IS_XFAIL -ne 0 ]; then
119 echo "Incorrect Output (Expected Failure):"
Gabor Greif1e2db032008-03-20 22:50:54 +0000120 else
121 echo "Incorrect Output:"
122 fi
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 cat $OUTPUT
Gabor Greif1e2db032008-03-20 22:50:54 +0000124 if [ $VG_STATUS -ne 0 ]; then
125 echo "Valgrind Output:"
Nuno Lopesf557fb72008-09-04 18:33:57 +0000126 cat $OUTPUT.vg
Gabor Greif1e2db032008-03-20 22:50:54 +0000127 fi
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 echo "******************** TEST '$TESTNAME' FAILED! ********************"
Gabor Greif5ca1b5a2008-03-17 12:35:00 +0000129 exit 1
Gabor Greif1e2db032008-03-20 22:50:54 +0000130fi