• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright 2014 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
17readonly PREFIX="#####"
18readonly RETRIES=2
19test_prefix=
20
21function checkArgOrExit() {
22  if [[ $# -lt 2 ]]; then
23    echo "Missing argument for option $1" >&2
24    exit 1
25  fi
26}
27
28function usageAndExit() {
29  cat >&2 << EOF
30  all_tests.sh - test runner with support for flake testing
31
32  all_tests.sh [options]
33
34  options:
35  -h, --help                     show this menu
36  -p, --prefix=TEST_PREFIX       specify a prefix for the tests to be run
37EOF
38  exit 0
39}
40
41function maybePlural() {
42  # $1 = integer to use for plural check
43  # $2 = singular string
44  # $3 = plural string
45  if [ $1 -ne 1 ]; then
46    echo "$3"
47  else
48    echo "$2"
49  fi
50}
51
52function runTest() {
53  local cmd="$1"
54
55  if $cmd; then
56    return 0
57  fi
58
59  for iteration in $(seq $RETRIES); do
60    if ! $cmd; then
61      echo >&2 "'$cmd' failed more than once, giving up"
62      return 1
63    fi
64  done
65
66  echo >&2 "Warning: '$cmd' FLAKY, passed $RETRIES/$((RETRIES + 1))"
67}
68
69# Parse arguments
70while [ -n "$1" ]; do
71  case "$1" in
72    -h|--help)
73      usageAndExit
74      ;;
75    -p|--prefix)
76      checkArgOrExit $@
77      test_prefix=$2
78      shift 2
79      ;;
80    *)
81      echo "Unknown option $1" >&2
82      echo >&2
83      usageAndExit
84      ;;
85  esac
86done
87
88# Find the relevant tests
89if [[ -z $test_prefix ]]; then
90  readonly tests=$(eval "find . -name '*_test.py' -type f -executable")
91else
92  readonly tests=$(eval "find . -name '$test_prefix*' -type f -executable")
93fi
94
95# Give some readable status messages
96readonly count=$(echo $tests | wc -w)
97if [[ -z $test_prefix ]]; then
98  echo "$PREFIX Found $count $(maybePlural $count test tests)."
99else
100  echo "$PREFIX Found $count $(maybePlural $count test tests) with prefix $test_prefix."
101fi
102
103exit_code=0
104
105i=0
106for test in $tests; do
107  i=$((i + 1))
108  echo ""
109  echo "$PREFIX $test ($i/$count)"
110  echo ""
111  runTest $test || exit_code=$(( exit_code + 1 ))
112  echo ""
113done
114
115echo "$PREFIX $exit_code failed $(maybePlural $exit_code test tests)."
116exit $exit_code
117