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