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