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 if [[ -z "${JAVA_HOME}" ]]; then
     37     unix=$(uname | tr '[A-Z]' '[a-z]')
     38     JAVA_HOME="${progdir}/../../../prebuilts/jdk/jdk8/${unix}-x86"
     39 fi
     40 
     41 if [[ ! -d "${JAVA_HOME}" ]]; then
     42     echo "Missing JAVA_HOME directory: $JAVA_HOME" 1>&2
     43     exit 1
     44 fi
     45 
     46 export JAVAC="${JAVA_HOME}/bin/javac"
     47 if [ "!" -e "$JAVAC" ]; then
     48     echo "Missing JAVAC executable: ${JAVAC}" 1>&2
     49     exit 1
     50 fi
     51 
     52 info="info.txt"
     53 run="run"
     54 expected="expected.txt"
     55 output="out.txt"
     56 
     57 clean_on_exit="yes"
     58 dev_mode="no"
     59 update_mode="no"
     60 tmpdir=/tmp/test-$$
     61 usage="no"
     62 
     63 while [[ "x$1" = "x-"* ]]; do
     64     case $1 in
     65         --dev) dev_mode="yes" ;;
     66         --no-clean) clean_on_exit="no" ;;
     67          --output_dir)
     68              tmpdir=$2
     69              shift ;;
     70          --update) update_mode="yes" ;;
     71          --help) usage="yes" ;;
     72          *) usage="yes" ;;
     73      esac
     74      shift
     75 done
     76 
     77 if [ "x$1" = "x" ]; then
     78     testdir=`basename "$oldwd"`
     79 else
     80     testdir="$1"
     81 fi
     82 
     83 if [ '!' -d "$testdir" ]; then
     84     td2=`echo ${testdir}-*`
     85     if [ '!' -d "$td2" ]; then
     86         echo "${testdir}: no such test directory" 1>&2
     87         usage="yes"
     88     fi
     89     testdir="$td2"
     90 fi
     91 
     92 if [ "$update_mode" = "yes" -a "$dev_mode" = "yes" ] ; then
     93     echo Error: --dev is incompatible with --update. 1>&2
     94     usage="yes"
     95 fi
     96 
     97 if [ "$usage" = "yes" ]; then
     98     prog=`basename $prog`
     99     (
    100         echo "usage:"
    101         echo "  $prog --help             Print this message."
    102         echo "  $prog testname           Run test normally."
    103         echo "  $prog --dev testname     Development mode (dump to stdout)."
    104         echo "  $prog --update testname  Update mode (replace expected.txt)."
    105         echo "  Omitting the test name uses the current directory as the test."
    106         echo "options:"
    107         echo "        --output_dir <dir> Use <dir> for the test outputs."
    108     ) 1>&2
    109     exit 1
    110 fi
    111 
    112 td_info="${testdir}/${info}"
    113 td_run="${testdir}/${run}"
    114 td_expected="${testdir}/${expected}"
    115 
    116 for td_file in "$td_info" "$td_run" "$td_expected"; do
    117     if [[ ! -r "$td_file" ]]; then
    118         echo "${testdir}: missing file $td_file" 1>&2
    119         exit 1
    120     fi
    121 done
    122 
    123 # copy the test to a temp dir and run it
    124 if [ -d "$tmpdir" ]; then
    125     rm -rf "$tmpdir" || exit 1
    126 fi
    127 output_parent=`dirname ${tmpdir}`
    128 mkdir -p "${output_parent}" || exit 1
    129 cp -Rp "$testdir" "$tmpdir" || exit 1
    130 cd "$tmpdir"
    131 chmod 755 "$run"
    132 
    133 echo "${testdir}: running..." 1>&2
    134 good="no"
    135 if [ "$dev_mode" = "yes" ]; then
    136     "./$run" 2>&1
    137     echo "exit status: $?" 1>&2
    138     good="yes"
    139 elif [ "$update_mode" = "yes" ]; then
    140     "./$run" >"$output" 2>&1
    141     if [[ $? == 0 ]]; then
    142         good="yes"
    143         mv "$output" "${progdir}/${td_expected}"
    144     else
    145         echo "Test failed during update."
    146         good="no"
    147     fi
    148 
    149 else
    150     "./$run" >"$output" 2>&1
    151     cmp -s "$expected" "$output"
    152     if [ "$?" = "0" ]; then
    153         # output == expected
    154         good="yes"
    155         echo "$testdir"': succeeded!' 1>&2
    156     fi
    157 fi
    158 
    159 if [ "$good" = "yes" ]; then
    160     cd "$oldwd"
    161     if [ "$clean_on_exit" = "yes" ]; then
    162         rm -rf "$tmpdir"
    163     else
    164         echo "Test artifacts left in $tmpdir"
    165     fi
    166     exit 0
    167 fi
    168 
    169 (
    170     echo "${testdir}: FAILED!"
    171     echo ' '
    172     echo '#################### info'
    173     cat "$info" | sed 's/^/# /g'
    174     echo '#################### diffs'
    175     diff -u "$expected" "$output"
    176     echo '####################'
    177     echo ' '
    178     echo "files left in $tmpdir"
    179 ) 1>&2
    180 
    181 exit 1
    182