summaryrefslogtreecommitdiffstats
path: root/host/commands/emugen/tests/run-tests.sh
blob: 67409edcc2353f64787b6b88e47db8255b2a70f6 (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
#!/bin/sh

# Copyright 2014 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 -e

export LANG=C
export LC_ALL=C

PROGDIR=$(dirname "$0")
PROGNAME=$(basename "$0")

fatal () {
    echo "ERROR: $@"
    exit 1
}

OPT_EMUGEN=
OPT_HELP=
OPT_OUT_DIR=
OPT_TOOL=

for OPT; do
    OPTARG=$(expr "x$OPT" : "x[^=]*=\\(.*\\)" || true)
    case $OPT in
    --help|-h|-?)
        OPT_HELP=true
        ;;
    --emugen=*)
        OPT_EMUGEN=$OPTARG
        ;;
    --out-dir=*)
        OPT_OUT_DIR=$OPTARG
        ;;
    --tool=*)
        OPT_TOOL=$OPTARG
        ;;
    -*)
        fatal "Invalid option '$OPT', see --help."
        ;;
    *)
        fatal "This script doesn't take arguments, see --help."
        ;;
    esac
done

if [ "$OPT_HELP" ]; then
    cat <<EOF
Usage: $PROGNAME [options]

Run the emugen test suite. This scripts looks for sub-directories
named t.<number>/input, and uses them as input to 'emugen'. It then
compares the output to t.<number>/expected/ content.

Valid options:
    --help|-h|-?         Print this help.
    --out-dir=<dir>      Generate outputs into <dir>.
    --emugen=<program>   Emugen program path, if not in path.
    --tool=<tool>        Launch visual diff tool in case of differences.
EOF
    exit 0
fi

# Find emugen program
EMUGEN=
if [ "$OPT_EMUGEN" ]; then
    EMUGEN=$OPT_EMUGEN
else
    EMUGEN=$(which emugen 2>/dev/null || true)
    if [ -z "$EMUGEN" ]; then
        fatal "Cannot find 'emugen' program in PATH, use --emugen=<program> option."
    fi
    echo "Auto-config: --emugen=$EMUGEN"
fi
if [ ! -f "$EMUGEN" ]; then
    fatal "Emugen program doesn't exist: $EMUGEN"
fi

# Create output directory.
OUT_DIR=
if [ "$OPT_OUT_DIR" ]; then
    OUT_DIR=$OPT_OUT_DIR
else
    OUT_DIR=/tmp/$USER-emugen-testing
    echo "Auto-config: --out-dir=$OUT_DIR"
fi
mkdir -p "$OUT_DIR" && rm -rf "$OUT_DIR/emugen"

OUT_DIR=$OUT_DIR/emugen

# Find test directories
TEST_DIRS=$(cd "$PROGDIR" && find . -name "t.*" | sed -e 's|^\./||')
for TEST_DIR in $TEST_DIRS; do
    IN=$PROGDIR/$TEST_DIR/input
    PREFIXES=$(cd $IN && find . -name "*.in" | sed -e 's|^\./||g' -e 's|\.in$||g')
    OUT=$OUT_DIR/$TEST_DIR
    mkdir -p "$OUT/encoder"
    mkdir -p "$OUT/decoder"
    mkdir -p "$OUT/wrapper"
    for PREFIX in $PREFIXES; do
        echo "Processing $IN/foo.*"
        $EMUGEN -i "$PROGDIR/$TEST_DIR/input" -D "$OUT/decoder" -E "$OUT/encoder" -W "$OUT/wrapper" $PREFIX
    done
    if ! diff -qr "$PROGDIR/$TEST_DIR/expected" "$OUT"; then
        if [ "$OPT_TOOL" ]; then
            $OPT_TOOL "$PROGDIR/$TEST_DIR/expected" "$OUT"
        else
            echo "ERROR: Invalid differences between actual and expected output!"
            diff -burN "$PROGDIR/$TEST_DIR/expected" "$OUT"
            exit 1
        fi
    fi
done

echo "All good!"
exit 0