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