| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | #!/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 Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 11 | #     %S - Replaced with the directory where the input file resides | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12 | #     %prcontext - prcontext.tcl script | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 13 | #     %t - temporary file name (derived from testcase name) | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 14 | # | 
 | 15 |  | 
 | 16 | FILENAME=$1 | 
 | 17 | TESTNAME=$1 | 
 | 18 | SUBST=$1 | 
| Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 19 | FILEDIR=`dirname $TESTNAME` | 
| Ted Kremenek | 6a18c76 | 2007-11-28 19:16:54 +0000 | [diff] [blame] | 20 |  | 
 | 21 | OUTPUT=Output/$1.out | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 |  | 
 | 23 | # create the output directory if it does not already exist | 
| Ted Kremenek | 6a18c76 | 2007-11-28 19:16:54 +0000 | [diff] [blame] | 24 | mkdir -p `dirname $OUTPUT` > /dev/null 2>&1 | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 |  | 
 | 26 | if 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 | 
 | 33 | fi | 
 | 34 |  | 
 | 35 | ulimit -t 40 | 
 | 36 |  | 
| Chris Lattner | 32a3999 | 2007-12-12 06:19:22 +0000 | [diff] [blame] | 37 | # Verify the script contains a run line. | 
 | 38 | grep -q 'RUN:' $FILENAME || (  | 
 | 39 |    echo "******************** TEST '$TESTNAME' HAS NO RUN LINE! ********************" | 
 | 40 |    exit 1 | 
 | 41 | ) | 
 | 42 |  | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 43 | # Run under valgrind if the VG environment variable has been set. | 
| Daniel Dunbar | 5e38073 | 2008-07-25 01:05:44 +0000 | [diff] [blame] | 44 | CLANG=$CLANG | 
| Eli Friedman | fde9fe7 | 2008-07-27 05:05:07 +0000 | [diff] [blame] | 45 | if [ ! -n "$CLANG" ]; then | 
| Daniel Dunbar | 5e38073 | 2008-07-25 01:05:44 +0000 | [diff] [blame] | 46 |     CLANG="clang" | 
 | 47 | fi | 
| Daniel Dunbar | e494d34 | 2009-03-24 06:17:45 +0000 | [diff] [blame] | 48 |  | 
 | 49 | # Resolve the path, and Make sure $CLANG actually exists; otherwise | 
 | 50 | # ensuing failures are non-obvious. | 
 | 51 | CLANG=$(which "$CLANG") | 
 | 52 | if [ -z $CLANG ]; then | 
 | 53 |   echo "Couldn't find 'clang' program, try setting CLANG in your environment" | 
 | 54 |   exit 1 | 
 | 55 | fi | 
 | 56 |  | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 57 | if [ -n "$VG" ]; then | 
| Nuno Lopes | f557fb7 | 2008-09-04 18:33:57 +0000 | [diff] [blame] | 58 |   rm -f $OUTPUT.vg | 
 | 59 |   CLANG="valgrind --leak-check=full --quiet --log-file=$OUTPUT.vg $CLANG" | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 60 | fi | 
 | 61 |  | 
| Daniel Dunbar | e494d34 | 2009-03-24 06:17:45 +0000 | [diff] [blame] | 62 | # Assuming $CLANG is correct, use it to derive clang-cc. We expect to | 
 | 63 | # be looking in a build directory, so just add '-cc'. | 
 | 64 | CLANGCC=$CLANGCC | 
 | 65 | if [ ! -n "$CLANGCC" ]; then | 
 | 66 |     CLANGCC="$CLANG-cc" | 
 | 67 | fi | 
 | 68 |  | 
 | 69 | # Try to sanity check $CLANGCC too | 
 | 70 | CLANGCC=$(which "$CLANGCC") | 
| Daniel Dunbar | 5ddce19 | 2009-05-02 20:08:07 +0000 | [diff] [blame] | 71 | # If that failed, ask clang. | 
 | 72 | if [ -z "$CLANGCC" ]; then | 
 | 73 |     CLANGCC=$($CLANG -print-prog-name=clang-cc) | 
 | 74 | fi | 
| Daniel Dunbar | e494d34 | 2009-03-24 06:17:45 +0000 | [diff] [blame] | 75 | if [ -z "$CLANGCC" ]; then | 
 | 76 |   echo "Couldn't find 'clang-cc' program, make sure clang is found in your build directory" | 
 | 77 |   exit 1 | 
 | 78 | fi | 
 | 79 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | SCRIPT=$OUTPUT.script | 
| Gabor Greif | 0f23303 | 2008-03-17 13:45:47 +0000 | [diff] [blame] | 81 | TEMPOUTPUT=$OUTPUT.tmp | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 82 | grep 'RUN:' $FILENAME | \ | 
 | 83 |   sed -e "s|^.*RUN:\(.*\)$|\1|g" \ | 
| Daniel Dunbar | e494d34 | 2009-03-24 06:17:45 +0000 | [diff] [blame] | 84 |       -e "s| clang | $CLANG |g" \ | 
 | 85 |       -e "s| clang-cc | $CLANGCC |g" \ | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 86 |       -e "s|%s|$SUBST|g" \ | 
| Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 87 |       -e "s|%S|$FILEDIR|g" \ | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 88 |       -e "s|%prcontext|prcontext.tcl|g" \ | 
| Nuno Lopes | f557fb7 | 2008-09-04 18:33:57 +0000 | [diff] [blame] | 89 |       -e "s|%t|$TEMPOUTPUT|g" > $SCRIPT | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 |  | 
| Daniel Dunbar | 607b17b | 2008-09-04 00:30:11 +0000 | [diff] [blame] | 91 | IS_XFAIL=0 | 
 | 92 | if (grep -q XFAIL $FILENAME); then | 
 | 93 |     IS_XFAIL=1 | 
 | 94 |     printf "XFAILED '$TESTNAME': " | 
 | 95 |     grep XFAIL $FILENAME | 
 | 96 | fi | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 |  | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 98 | /bin/sh $SCRIPT > $OUTPUT 2>&1 | 
 | 99 | SCRIPT_STATUS=$? | 
 | 100 |  | 
 | 101 | if [ -n "$VG" ]; then | 
| Nuno Lopes | f557fb7 | 2008-09-04 18:33:57 +0000 | [diff] [blame] | 102 |   [ ! -s $OUTPUT.vg ] | 
 | 103 |   VG_STATUS=$? | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 104 | else | 
 | 105 |   VG_STATUS=0 | 
 | 106 | fi | 
 | 107 |  | 
| Daniel Dunbar | 607b17b | 2008-09-04 00:30:11 +0000 | [diff] [blame] | 108 | if [ $IS_XFAIL -ne 0 ]; then | 
 | 109 |     if [ $SCRIPT_STATUS -ne 0 ]; then | 
 | 110 |         SCRIPT_STATUS=0 | 
 | 111 |     else | 
 | 112 |         SCRIPT_STATUS=1 | 
 | 113 |     fi | 
 | 114 | fi | 
 | 115 |  | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 116 | if [ $SCRIPT_STATUS -ne 0 -o $VG_STATUS -ne 0 ]; then | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 |   echo "******************** TEST '$TESTNAME' FAILED! ********************" | 
 | 118 |   echo "Command: " | 
 | 119 |   cat $SCRIPT | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 120 |   if [ $SCRIPT_STATUS -eq 0 ]; then | 
 | 121 |     echo "Output:" | 
| Daniel Dunbar | 607b17b | 2008-09-04 00:30:11 +0000 | [diff] [blame] | 122 |   elif [ $IS_XFAIL -ne 0 ]; then | 
 | 123 |     echo "Incorrect Output (Expected Failure):" | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 124 |   else | 
 | 125 |     echo "Incorrect Output:" | 
 | 126 |   fi | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 |   cat $OUTPUT | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 128 |   if [ $VG_STATUS -ne 0 ]; then | 
 | 129 |     echo "Valgrind Output:" | 
| Nuno Lopes | f557fb7 | 2008-09-04 18:33:57 +0000 | [diff] [blame] | 130 |     cat $OUTPUT.vg | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 131 |   fi | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 |   echo "******************** TEST '$TESTNAME' FAILED! ********************" | 
| Gabor Greif | 5ca1b5a | 2008-03-17 12:35:00 +0000 | [diff] [blame] | 133 |   exit 1 | 
| Gabor Greif | 1e2db03 | 2008-03-20 22:50:54 +0000 | [diff] [blame] | 134 | fi |