• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
19prog="$0"
20while [ -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
29done
30oldwd=`pwd`
31progdir=`dirname "${prog}"`
32cd "${progdir}"
33progdir=`pwd`
34prog="${progdir}"/`basename "${prog}"`
35
36if [[ -z "${JAVA_HOME}" ]]; then
37    unix=$(uname | tr '[A-Z]' '[a-z]')
38    JAVA_HOME="${progdir}/../../../prebuilts/jdk/jdk8/${unix}-x86"
39fi
40
41if [[ ! -d "${JAVA_HOME}" ]]; then
42    echo "Missing JAVA_HOME directory: $JAVA_HOME" 1>&2
43    exit 1
44fi
45
46export JAVAC="${JAVA_HOME}/bin/javac"
47if [ "!" -e "$JAVAC" ]; then
48    echo "Missing JAVAC executable: ${JAVAC}" 1>&2
49    exit 1
50fi
51
52info="info.txt"
53run="run"
54expected="expected.txt"
55output="out.txt"
56
57clean_on_exit="yes"
58dev_mode="no"
59update_mode="no"
60tmpdir=/tmp/test-$$
61usage="no"
62
63while [[ "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
75done
76
77if [ "x$1" = "x" ]; then
78    testdir=`basename "$oldwd"`
79else
80    testdir="$1"
81fi
82
83if [ '!' -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"
90fi
91
92if [ "$update_mode" = "yes" -a "$dev_mode" = "yes" ] ; then
93    echo Error: --dev is incompatible with --update. 1>&2
94    usage="yes"
95fi
96
97if [ "$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
110fi
111
112td_info="${testdir}/${info}"
113td_run="${testdir}/${run}"
114td_expected="${testdir}/${expected}"
115
116for 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
121done
122
123# copy the test to a temp dir and run it
124if [ -d "$tmpdir" ]; then
125    rm -rf "$tmpdir" || exit 1
126fi
127output_parent=`dirname ${tmpdir}`
128mkdir -p "${output_parent}" || exit 1
129cp -Rp "$testdir" "$tmpdir" || exit 1
130cd "$tmpdir"
131chmod 755 "$run"
132
133echo "${testdir}: running..." 1>&2
134good="no"
135if [ "$dev_mode" = "yes" ]; then
136    "./$run" 2>&1
137    echo "exit status: $?" 1>&2
138    good="yes"
139elif [ "$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
149else
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
157fi
158
159if [ "$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
167fi
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
181exit 1
182