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 update_mode="no"
     48 tmpdir=/tmp/test-$$
     49 usage="no"
     50 
     51 while [[ "x$1" = "x-"* ]]; do
     52     case $1 in
     53          --dev) dev_mode="yes" ;;
     54          --output_dir)
     55              tmpdir=$2
     56              shift ;;
     57          --update) update_mode="yes" ;;
     58          --help) usage="yes" ;;
     59          *) usage="yes" ;;
     60      esac
     61      shift
     62 done
     63 
     64 if [ "x$1" = "x" ]; then
     65     testdir=`basename "$oldwd"`
     66 else
     67     testdir="$1"
     68 fi
     69 
     70 if [ '!' -d "$testdir" ]; then
     71     td2=`echo ${testdir}-*`
     72     if [ '!' -d "$td2" ]; then
     73         echo "${testdir}: no such test directory" 1>&2
     74         usage="yes"
     75     fi
     76     testdir="$td2"
     77 fi
     78 
     79 if [ "$update_mode" = "yes" -a "$dev_mode" = "yes" ] ; then
     80     echo Error: --dev is incompatible with --update. 1>&2
     81     usage="yes"
     82 fi
     83 
     84 if [ "$usage" = "yes" ]; then
     85     prog=`basename $prog`
     86     (
     87         echo "usage:"
     88         echo "  $prog --help             Print this message."
     89         echo "  $prog testname           Run test normally."
     90         echo "  $prog --dev testname     Development mode (dump to stdout)."
     91         echo "  $prog --update testname  Update mode (replace expected.txt)."
     92         echo "  Omitting the test name uses the current directory as the test."
     93         echo "options:"
     94         echo "        --output_dir <dir> Use <dir> for the test outputs."
     95     ) 1>&2
     96     exit 1
     97 fi
     98 
     99 td_info="$testdir"/"$info"
    100 td_run="$testdir"/"$run"
    101 td_expected="$testdir"/"$expected"
    102 
    103 if [ '!' '(' -r "$td_info" -a -r "$td_run" -a -r "$td_expected" ')' ]; then
    104     echo "${testdir}: missing files" 1>&2
    105     exit 1
    106 fi
    107 
    108 # copy the test to a temp dir and run it
    109 if [ -d "$tmpdir" ]; then
    110     rm -rf "$tmpdir" || exit 1
    111 fi
    112 output_parent=`dirname ${tmpdir}`
    113 mkdir -p "${output_parent}" || exit 1
    114 cp -Rp "$testdir" "$tmpdir" || exit 1
    115 cd "$tmpdir"
    116 chmod 755 "$run"
    117 
    118 echo "${testdir}: running..." 1>&2
    119 good="no"
    120 if [ "$dev_mode" = "yes" ]; then
    121     "./$run" 2>&1
    122     echo "exit status: $?" 1>&2
    123     good="yes"
    124 elif [ "$update_mode" = "yes" ]; then
    125     "./$run" >"${progdir}/$td_expected" 2>&1
    126     good="yes"
    127 else
    128     "./$run" >"$output" 2>&1
    129     cmp -s "$expected" "$output"
    130     if [ "$?" = "0" ]; then
    131         # output == expected
    132         good="yes"
    133         echo "$testdir"': succeeded!' 1>&2
    134     fi
    135 fi
    136 
    137 if [ "$good" = "yes" ]; then
    138     cd "$oldwd"
    139     rm -rf "$tmpdir"
    140     exit 0
    141 fi
    142 
    143 (
    144     echo "${testdir}: FAILED!"
    145     echo ' '
    146     echo '#################### info'
    147     cat "$info" | sed 's/^/# /g'
    148     echo '#################### diffs'
    149     diff -u "$expected" "$output"
    150     echo '####################'
    151     echo ' '
    152     echo "files left in $tmpdir"
    153 ) 1>&2
    154 
    155 exit 1
    156