• 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)"
22CORE_DIR="$(dirname $ASUITE_DIR)/tradefederation/core"
23ATEST_DIR="$CORE_DIR/atest"
24RC_FILE=${AIDEGEN_DIR}/.coveragerc
25MOD_COVERAGE='coverage:import coverage'
26MOD_PROTOBUF='protobuf:from google import protobuf'
27
28function get_python_path() {
29    echo "$PYTHONPATH:$CORE_DIR:$ATEST_DIR:$ASUITE_DIR"
30}
31
32function print_summary() {
33    local test_results=$1
34    local tmp_dir=$(mktemp -d)
35    python3 -m coverage report
36    python3 -m coverage html -d $tmp_dir --rcfile=$RC_FILE
37    echo "Coverage report available at file://${tmp_dir}/index.html"
38
39    if [[ $test_results -eq 0 ]]; then
40        echo -e "${GREEN}All unittests pass${NC}!"
41    else
42        echo -e "${RED}Unittest failure found${NC}!"
43        exit 1
44    fi
45}
46
47function run_unittests() {
48    local specified_tests=$@
49    local rc=0
50
51    # Get all unit tests under tools/acloud.
52    local all_tests=$(find $AIDEGEN_DIR -type f -name "*_unittest.py");
53    local tests_to_run=$all_tests
54
55    python3 -m coverage erase
56    for t in $tests_to_run; do
57        echo "Testing" $t
58        if ! PYTHONPATH=$(get_python_path) python3 -m coverage run --append --rcfile=$RC_FILE $t; then
59            rc=1
60            echo -e "${RED}$t failed${NC}"
61        fi
62    done
63
64    print_summary $rc
65    cleanup
66}
67
68function install_module() {
69    local FLAG="--user"
70    local module=$(echo $1| awk -F: '{print $1}')
71    local action=$(echo $1| awk -F: '{print $2}')
72    if ! python3 -c "$action" 2>/dev/null; then
73        echo -en "Module $RED$module$NC is missing. Start to install..."
74        cmd="python3 -m pip install $module $FLAG"
75        eval "$cmd" >/dev/null || { echo Failed to install $module; exit 1; }
76        echo " Done."
77    fi
78}
79
80function check_env() {
81    if [ -z "$ANDROID_BUILD_TOP" ]; then
82        echo "Missing ANDROID_BUILD_TOP env variable. Run 'lunch' first."
83        exit 1
84    fi
85    # Append more necessary modules if needed.
86    for mod in "$MOD_COVERAGE" "$MOD_PROTOBUF"; do
87        install_module "$mod"
88    done
89}
90
91function cleanup() {
92    # Search for *.pyc and delete them.
93    find $AIDEGEN_DIR -name "*.pyc" -exec rm -f {} \;
94}
95
96check_env
97cleanup
98run_unittests "$@"
99