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