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 for td_file in "$td_info" "$td_run" "$td_expected"; do 104 if [[ ! -r "$td_file" ]]; then 105 echo "${testdir}: missing file $td_file" 1>&2 106 exit 1 107 fi 108 done 109 110 # copy the test to a temp dir and run it 111 if [ -d "$tmpdir" ]; then 112 rm -rf "$tmpdir" || exit 1 113 fi 114 output_parent=`dirname ${tmpdir}` 115 mkdir -p "${output_parent}" || exit 1 116 cp -Rp "$testdir" "$tmpdir" || exit 1 117 cd "$tmpdir" 118 chmod 755 "$run" 119 120 echo "${testdir}: running..." 1>&2 121 good="no" 122 if [ "$dev_mode" = "yes" ]; then 123 "./$run" 2>&1 124 echo "exit status: $?" 1>&2 125 good="yes" 126 elif [ "$update_mode" = "yes" ]; then 127 "./$run" >"$output" 2>&1 128 if [[ $? == 0 ]]; then 129 good="yes" 130 mv "$output" "${progdir}/${td_expected}" 131 else 132 echo "Test failed during update." 133 good="no" 134 fi 135 136 else 137 "./$run" >"$output" 2>&1 138 cmp -s "$expected" "$output" 139 if [ "$?" = "0" ]; then 140 # output == expected 141 good="yes" 142 echo "$testdir"': succeeded!' 1>&2 143 fi 144 fi 145 146 if [ "$good" = "yes" ]; then 147 cd "$oldwd" 148 rm -rf "$tmpdir" 149 exit 0 150 fi 151 152 ( 153 echo "${testdir}: FAILED!" 154 echo ' ' 155 echo '#################### info' 156 cat "$info" | sed 's/^/# /g' 157 echo '#################### diffs' 158 diff -u "$expected" "$output" 159 echo '####################' 160 echo ' ' 161 echo "files left in $tmpdir" 162 ) 1>&2 163 164 exit 1 165