• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2# Copyright 2019, The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16RED='\033[0;31m'
17GREEN='\033[0;32m'
18NC='\033[0m' # No Color
19[ "$(uname -s)" == "Darwin" ] && { realpath(){ echo "$(cd $(dirname $1);pwd -P)/$(basename $1)"; }; }
20AIDEGEN_DIR=$(dirname $(realpath $0))
21ASUITE_DIR="$(dirname $AIDEGEN_DIR)"
22ATEST_DIR="$ASUITE_DIR/atest"
23RC_FILE=${AIDEGEN_DIR}/.coveragerc
24MOD_COVERAGE='coverage:import coverage'
25MOD_PROTOBUF='protobuf:from google import protobuf'
26
27function get_python_path() {
28    echo "$PYTHONPATH:$ASUITE_DIR:$ATEST_DIR"
29}
30
31function print_summary() {
32    local test_results=$1
33    local tmp_dir=$(mktemp -d)
34    python3 -m coverage report
35    python3 -m coverage html -d $tmp_dir --rcfile=$RC_FILE
36    echo "Coverage report available at file://${tmp_dir}/index.html"
37
38    if [[ $test_results -eq 0 ]]; then
39        echo -e "${GREEN}All unittests pass${NC}!"
40    else
41        echo -e "${RED}Unittest failure found${NC}!"
42        exit 1
43    fi
44}
45
46function run_unittests() {
47    local specified_tests=$@
48    local rc=0
49
50    # Get all unit tests under asuite/aidegen.
51    local all_tests=$(find $AIDEGEN_DIR -type f -name "*_unittest.py");
52    local tests_to_run=$all_tests
53
54    python3 -m coverage erase
55    for t in $tests_to_run; do
56        echo "Testing" $t
57        if ! PYTHONPATH=$(get_python_path) python3 -m coverage run --append --rcfile=$RC_FILE $t; then
58           rc=1
59           echo -e "${RED}$t failed${NC}"
60        fi
61    done
62
63    print_summary $rc
64    cleanup
65}
66
67function install_module() {
68    local FLAG="--user"
69    local module=$(echo $1| awk -F: '{print $1}')
70    local action=$(echo $1| awk -F: '{print $2}')
71    if ! python3 -c "$action" 2>/dev/null; then
72        echo -en "Module $RED$module$NC is missing. Start to install..."
73        cmd="python3 -m pip install $module $FLAG"
74        eval "$cmd" >/dev/null || { echo Failed to install $module; exit 1; }
75        echo " Done."
76    fi
77}
78
79function check_env() {
80    if [ -z "$ANDROID_BUILD_TOP" ]; then
81        echo "Missing ANDROID_BUILD_TOP env variable. Run 'lunch' first."
82        exit 1
83    fi
84    # Append more necessary modules if needed.
85    for mod in "$MOD_COVERAGE" "$MOD_PROTOBUF"; do
86        install_module "$mod"
87    done
88}
89
90function cleanup() {
91    # Search for *.pyc and delete them.
92    find $AIDEGEN_DIR -name "*.pyc" -exec rm -f {} \;
93}
94
95check_env
96cleanup
97run_unittests "$@"
98