summaryrefslogtreecommitdiff
path: root/dx/tests/run-test
blob: cf80cc0939cbad6aececaa5eef10325a0fcb364e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
#
# Copyright (C) 2007 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Set up prog to be the path of this script, including following symlinks,
# and set up progdir to be the fully-qualified pathname of its directory.
prog="$0"
while [ -h "${prog}" ]; do
    newProg=`/bin/ls -ld "${prog}"`
    newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
    if expr "x${newProg}" : 'x/' >/dev/null; then
        prog="${newProg}"
    else
        progdir=`dirname "${prog}"`
        prog="${progdir}/${newProg}"
    fi
done
oldwd=`pwd`
progdir=`dirname "${prog}"`
cd "${progdir}"
progdir=`pwd`
prog="${progdir}"/`basename "${prog}"`

if [[ -z "${JAVA_HOME}" ]]; then
    unix=$(uname | tr '[A-Z]' '[a-z]')
    JAVA_HOME="${progdir}/../../../prebuilts/jdk/jdk11/${unix}-x86"
fi

if [[ ! -d "${JAVA_HOME}" ]]; then
    echo "Missing JAVA_HOME directory: $JAVA_HOME" 1>&2
    exit 1
fi

export JAVAC="${JAVA_HOME}/bin/javac"
if [ "!" -e "$JAVAC" ]; then
    echo "Missing JAVAC executable: ${JAVAC}" 1>&2
    exit 1
fi

export JAVA="${JAVA_HOME}/bin/java"
if [ "!" -e "$JAVA" ]; then
    echo "Missing JAVA executable: ${JAVA}" 1>&2
    exit 1
fi

info="info.txt"
run="run"
expected="expected.txt"
output="out.txt"

clean_on_exit="yes"
dev_mode="no"
update_mode="no"
tmpdir=/tmp/test-$$
usage="no"

while [[ "x$1" = "x-"* ]]; do
    case $1 in
        --dev) dev_mode="yes" ;;
        --no-clean) clean_on_exit="no" ;;
         --output_dir)
             tmpdir=$2
             shift ;;
         --update) update_mode="yes" ;;
         --help) usage="yes" ;;
         *) usage="yes" ;;
     esac
     shift
done

if [ "x$1" = "x" ]; then
    testdir=`basename "$oldwd"`
else
    testdir="$1"
fi

if [ '!' -d "$testdir" ]; then
    td2=`echo ${testdir}-*`
    if [ '!' -d "$td2" ]; then
        echo "${testdir}: no such test directory" 1>&2
        usage="yes"
    fi
    testdir="$td2"
fi

if [ "$update_mode" = "yes" -a "$dev_mode" = "yes" ] ; then
    echo Error: --dev is incompatible with --update. 1>&2
    usage="yes"
fi

if [ "$usage" = "yes" ]; then
    prog=`basename $prog`
    (
        echo "usage:"
        echo "  $prog --help             Print this message."
        echo "  $prog testname           Run test normally."
        echo "  $prog --dev testname     Development mode (dump to stdout)."
        echo "  $prog --update testname  Update mode (replace expected.txt)."
        echo "  Omitting the test name uses the current directory as the test."
        echo "options:"
        echo "        --output_dir <dir> Use <dir> for the test outputs."
    ) 1>&2
    exit 1
fi

td_info="${testdir}/${info}"
td_run="${testdir}/${run}"
td_expected="${testdir}/${expected}"

for td_file in "$td_info" "$td_run" "$td_expected"; do
    if [[ ! -r "$td_file" ]]; then
        echo "${testdir}: missing file $td_file" 1>&2
        exit 1
    fi
done

# copy the test to a temp dir and run it
if [ -d "$tmpdir" ]; then
    rm -rf "$tmpdir" || exit 1
fi
output_parent=`dirname ${tmpdir}`
mkdir -p "${output_parent}" || exit 1
cp -Rp "$testdir" "$tmpdir" || exit 1
cd "$tmpdir"
chmod 755 "$run"

echo "${testdir}: running..." 1>&2
good="no"
if [ "$dev_mode" = "yes" ]; then
    "./$run" 2>&1
    echo "exit status: $?" 1>&2
    good="yes"
elif [ "$update_mode" = "yes" ]; then
    "./$run" >"$output" 2>&1
    if [[ $? == 0 ]]; then
        good="yes"
        mv "$output" "${progdir}/${td_expected}"
    else
        echo "Test failed during update."
        good="no"
    fi

else
    "./$run" >"$output" 2>&1
    cmp -s "$expected" "$output"
    if [ "$?" = "0" ]; then
        # output == expected
        good="yes"
        echo "$testdir"': succeeded!' 1>&2
    fi
fi

if [ "$good" = "yes" ]; then
    cd "$oldwd"
    if [ "$clean_on_exit" = "yes" ]; then
        rm -rf "$tmpdir"
    else
        echo "Test artifacts left in $tmpdir"
    fi
    exit 0
fi

(
    echo "${testdir}: FAILED!"
    echo ' '
    echo '#################### info'
    cat "$info" | sed 's/^/# /g'
    echo '#################### diffs'
    diff -u "$expected" "$output"
    echo '####################'
    echo ' '
    echo "files left in $tmpdir"
) 1>&2

exit 1