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