Home | History | Annotate | Download | only in tests
      1 #!/bin/bash
      2 #
      3 # Copyright (C) 2007 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #     http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 # Set up prog to be the path of this script, including following symlinks,
     18 # and set up progdir to be the fully-qualified pathname of its directory.
     19 prog="$0"
     20 while [ -h "${prog}" ]; do
     21     newProg=`/bin/ls -ld "${prog}"`
     22     newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
     23     if expr "x${newProg}" : 'x/' >/dev/null; then
     24         prog="${newProg}"
     25     else
     26         progdir=`dirname "${prog}"`
     27         prog="${progdir}/${newProg}"
     28     fi
     29 done
     30 oldwd=`pwd`
     31 progdir=`dirname "${prog}"`
     32 cd "${progdir}"
     33 progdir=`pwd`
     34 prog="${progdir}"/`basename "${prog}"`
     35 
     36 export JAVAC="${progdir}/../../../prebuilt/common/openjdk/bin/javac"
     37 if [ "!" -e "$JAVAC" ]; then
     38     JAVAC="javac"
     39 fi
     40 
     41 info="info.txt"
     42 run="run"
     43 expected="expected.txt"
     44 output="out.txt"
     45 
     46 dev_mode="no"
     47 if [ "x$1" = "x--dev" ]; then
     48     dev_mode="yes"
     49     shift
     50 fi
     51 
     52 update_mode="no"
     53 if [ "x$1" = "x--update" ]; then
     54     update_mode="yes"
     55     shift
     56 fi
     57 
     58 usage="no"
     59 if [ "x$1" = "x--help" ]; then
     60     usage="yes"
     61 else
     62     if [ "x$1" = "x" ]; then
     63         testdir=`basename "$oldwd"`
     64     else
     65         testdir="$1"
     66     fi
     67 
     68     if [ '!' -d "$testdir" ]; then
     69         td2=`echo ${testdir}-*`
     70         if [ '!' -d "$td2" ]; then
     71             echo "${testdir}: no such test directory" 1>&2
     72             usage="yes"
     73         fi
     74         testdir="$td2"
     75     fi
     76 fi
     77 
     78 if [ "$usage" = "yes" ]; then
     79     prog=`basename $prog`
     80     (
     81         echo "usage:"
     82         echo "  $prog --help             Print this message."
     83         echo "  $prog testname           Run test normally."
     84         echo "  $prog --dev testname     Development mode (dump to stdout)."
     85         echo "  $prog --update testname  Update mode (replace expected.txt)."
     86         echo "  Omitting the test name uses the current directory as the test."
     87     ) 1>&2
     88     exit 1
     89 fi
     90 
     91 td_info="$testdir"/"$info"
     92 td_run="$testdir"/"$run"
     93 td_expected="$testdir"/"$expected"
     94 
     95 tmpdir=/tmp/test-$$
     96 
     97 if [ '!' '(' -r "$td_info" -a -r "$td_run" -a -r "$td_expected" ')' ]; then
     98     echo "${testdir}: missing files" 1>&2
     99     exit 1
    100 fi
    101 
    102 # copy the test to a temp dir and run it
    103 
    104 echo "${testdir}: running..." 1>&2
    105 
    106 rm -rf "$tmpdir"
    107 cp -Rp "$testdir" "$tmpdir"
    108 cd "$tmpdir"
    109 chmod 755 "$run"
    110 
    111 #PATH="${progdir}/../build/bin:${PATH}"
    112 
    113 good="no"
    114 if [ "$dev_mode" = "yes" ]; then
    115     "./$run" 2>&1
    116     echo "exit status: $?" 1>&2
    117     good="yes"
    118 elif [ "$update_mode" = "yes" ]; then
    119     "./$run" >"${progdir}/$td_expected" 2>&1
    120     good="yes"
    121 else
    122     "./$run" >"$output" 2>&1
    123     cmp -s "$expected" "$output"
    124     if [ "$?" = "0" ]; then
    125         # output == expected
    126         good="yes"
    127         echo "$testdir"': succeeded!' 1>&2
    128     fi
    129 fi
    130 
    131 if [ "$good" = "yes" ]; then
    132     cd "$oldwd"
    133     rm -rf "$tmpdir"
    134     exit 0
    135 fi
    136 
    137 (
    138     echo "${testdir}: FAILED!"
    139     echo ' '
    140     echo '#################### info'
    141     cat "$info" | sed 's/^/# /g'
    142     echo '#################### diffs'
    143     diff -u "$expected" "$output"
    144     echo '####################'
    145     echo ' '
    146     echo "files left in $tmpdir"
    147 ) 1>&2
    148 
    149 exit 1
    150