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
36# Command-line options
37skip_tests=""
38sequential="no"
39usage="no"
40while [[ "$1" == "-"* ]]; do
41  case $1 in
42    --seq) sequential="yes" ;;
43    --skip) skip_tests="$2 $skip_tests"
44            shift ;;
45    *) usage="yes" ;;
46  esac
47  shift
48done
49
50if [ $usage = "yes" ]; then
51    prog=`basename $prog`
52    cat 1>&2 <<END_USAGE
53Usage:
54  $prog [options]   Run all tests with given options.
55Options:
56  --seq             Run tests sequentially (default: parallel)
57  --skip <test>     Skip running specified test
58END_USAGE
59    exit 1
60fi
61
62# Globals for tracking numbers of successes and failures and their names.
63passed=()
64surprised=()
65ignored=()
66failed=()
67skipped=()
68
69# Tests failing and require attention (e.g. 115-merge)
70known_bad="100-local-mismatch 115-merge 119-merge-conflict"
71
72function display_results {
73  printf    "\n\nTest Results\n"
74  printf -- "----------------------------\n"
75  printf    "Pass:                   % 4d\n" ${#passed[@]}
76  printf    "Surprise pass:          % 4d\n" ${#surprised[@]}
77  printf    "Known failures:         % 4d\n" ${#ignored[@]}
78  printf    "Failures:               % 4d\n" ${#failed[@]}
79  printf    "Skipped:                % 4d\n" ${#skipped[@]}
80  printf -- "----------------------------\n"
81  printf    "Elapsed time(s):        % 4d\n" $SECONDS
82
83  list_files "Unexpected successes" ${surprised[@]}
84  list_files "Known failures" ${ignored[@]}
85  list_files "Failures" ${failed[@]}
86  list_files "Skipped" ${skipped[@]}
87
88  needing_attention=$(( ${#failed[@]} + ${#surprised[@]} ))
89  exit ${needing_attention}
90}
91
92function list_files {
93  # Arguments: Title test_name0 test_name1 ... test_nameN
94  echo "$1:"
95  shift
96  if [[ "$1" = "" ]]; then
97    echo "  NONE"
98    return
99  fi
100  while [[ "$1" != "" ]]; do
101    echo "  $1"
102    shift
103  done
104}
105
106function update_result {
107  test_name=$1
108  output=$2
109  result=$3
110
111  if [[ "$known_bad" == *"$test_name"* ]]; then
112    expectFail=1
113  else
114    expectFail=0
115  fi
116  if [ $result = 0 ]; then
117    if [[ $expectFail = 0 ]]; then
118      passed+=(${test_name})
119    else
120      echo "Failing on unexpected success of $test_name"
121      surprised+=(${test_name})
122    fi
123  else
124    if [[ $expectFail = 0 ]]; then
125      failed+=(${test_name})
126    else
127      echo "Ignoring expected failure of $test_name"
128      ignored+=(${test_name})
129      # Clean up when we expect a test to fail.
130      # run-test only does this on success.
131      rm -rf "$output"
132    fi
133  fi
134}
135
136function run_tests {
137  if [[ "$sequential" = "yes" ]]; then
138    for test_name in *; do
139      if [[ "$skip_tests" = *"$test_name"* ]]; then
140          skipped+=(${test_name})
141          continue
142      fi
143      if [ -d "$test_name" -a -r "$test_name" ]; then
144        output=/tmp/$$/$test_name
145        ./run-test --output_dir "$output" "$test_name"
146        update_result $test_name $output $?
147      fi
148    done
149  else
150    i=0
151    for test_name in *; do
152      if [[ "$skip_tests" = *"$test_name"* ]]; then
153          skipped+=(${test_name})
154          continue
155      fi
156      if [ -d "$test_name" -a -r "$test_name" ]; then
157          output=/tmp/$$/$test_name
158          ./run-test --output_dir "$output" "$test_name" &
159          test_pids[i]=$!
160          test_names[test_pids[i]]="$test_name"
161          test_outputs[test_pids[i]]="output"
162          let i+=1
163      fi
164    done
165
166    for pid in ${test_pids[@]}; do
167      wait $pid
168      update_result ${test_names[$pid]} ${test_outputs[$pid]} $?
169    done
170  fi
171}
172
173function handle_interrupt {
174  trap INT
175  display_results
176}
177
178trap handle_interrupt INT
179run_tests
180display_results
181