1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env bash 2*c2e18aaaSAndroid Build Coastguard Worker 3*c2e18aaaSAndroid Build Coastguard Worker# Copyright (C) 2017 The Android Open Source Project 4*c2e18aaaSAndroid Build Coastguard Worker# 5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*c2e18aaaSAndroid Build Coastguard Worker# 9*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*c2e18aaaSAndroid Build Coastguard Worker# 11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 16*c2e18aaaSAndroid Build Coastguard Worker 17*c2e18aaaSAndroid Build Coastguard Worker# A simple helper script that runs all of the atest unit tests. 18*c2e18aaaSAndroid Build Coastguard Worker# There are 2 situations that we take care of: 19*c2e18aaaSAndroid Build Coastguard Worker# 1. User wants to invoke this script directly. 20*c2e18aaaSAndroid Build Coastguard Worker# 2. PREUPLOAD hook invokes this script. 21*c2e18aaaSAndroid Build Coastguard Worker 22*c2e18aaaSAndroid Build Coastguard WorkerATEST_DIR=$(dirname $0) 23*c2e18aaaSAndroid Build Coastguard Worker[ "$(uname -s)" == "Darwin" ] && { realpath(){ echo "$(cd $(dirname $1);pwd -P)/$(basename $1)"; }; } 24*c2e18aaaSAndroid Build Coastguard WorkerATEST_REAL_PATH=$(realpath $ATEST_DIR) 25*c2e18aaaSAndroid Build Coastguard WorkerRED='\033[0;31m' 26*c2e18aaaSAndroid Build Coastguard WorkerGREEN='\033[0;32m' 27*c2e18aaaSAndroid Build Coastguard WorkerNC='\033[0m' # No Color 28*c2e18aaaSAndroid Build Coastguard WorkerCOVERAGE=false 29*c2e18aaaSAndroid Build Coastguard WorkerPYTHON=python3 30*c2e18aaaSAndroid Build Coastguard WorkerPIP=pip3 31*c2e18aaaSAndroid Build Coastguard Worker 32*c2e18aaaSAndroid Build Coastguard Workerfunction python3_checker() { 33*c2e18aaaSAndroid Build Coastguard Worker if ! which $PYTHON >/dev/null 2>&1; then 34*c2e18aaaSAndroid Build Coastguard Worker echo "python3 not found."; exit 1 35*c2e18aaaSAndroid Build Coastguard Worker fi 36*c2e18aaaSAndroid Build Coastguard Worker} 37*c2e18aaaSAndroid Build Coastguard Worker 38*c2e18aaaSAndroid Build Coastguard Workerfunction get_pythonpath() { 39*c2e18aaaSAndroid Build Coastguard Worker echo "$ATEST_REAL_PATH:$PYTHONPATH" 40*c2e18aaaSAndroid Build Coastguard Worker} 41*c2e18aaaSAndroid Build Coastguard Worker 42*c2e18aaaSAndroid Build Coastguard Workerfunction print_summary() { 43*c2e18aaaSAndroid Build Coastguard Worker local test_results=$1 44*c2e18aaaSAndroid Build Coastguard Worker if [[ $COVERAGE == true ]]; then 45*c2e18aaaSAndroid Build Coastguard Worker coverage report --show-missing 46*c2e18aaaSAndroid Build Coastguard Worker coverage html 47*c2e18aaaSAndroid Build Coastguard Worker fi 48*c2e18aaaSAndroid Build Coastguard Worker if [[ $test_results -eq 0 ]]; then 49*c2e18aaaSAndroid Build Coastguard Worker echo -e "${GREEN}All unittests pass${NC}!" 50*c2e18aaaSAndroid Build Coastguard Worker else 51*c2e18aaaSAndroid Build Coastguard Worker echo -e "${RED}There was a unittest failure${NC}" 52*c2e18aaaSAndroid Build Coastguard Worker fi 53*c2e18aaaSAndroid Build Coastguard Worker} 54*c2e18aaaSAndroid Build Coastguard Worker 55*c2e18aaaSAndroid Build Coastguard Workerfunction module_checker() { 56*c2e18aaaSAndroid Build Coastguard Worker mods_to_check=(coverage) 57*c2e18aaaSAndroid Build Coastguard Worker for mod in ${mods_to_check[@]}; do 58*c2e18aaaSAndroid Build Coastguard Worker if ! $PIP freeze | grep $mod >/dev/null 2>&1; then 59*c2e18aaaSAndroid Build Coastguard Worker $PIP install -U --user $mod 60*c2e18aaaSAndroid Build Coastguard Worker fi 61*c2e18aaaSAndroid Build Coastguard Worker if ! (head -n1 $(which $mod) | grep -q $PYTHON); then 62*c2e18aaaSAndroid Build Coastguard Worker sed -i "1 s/python/$PYTHON/" $(which $mod) 63*c2e18aaaSAndroid Build Coastguard Worker fi 64*c2e18aaaSAndroid Build Coastguard Worker done 65*c2e18aaaSAndroid Build Coastguard Worker} 66*c2e18aaaSAndroid Build Coastguard Worker 67*c2e18aaaSAndroid Build Coastguard Workerfunction run_atest_unittests() { 68*c2e18aaaSAndroid Build Coastguard Worker python3_checker 69*c2e18aaaSAndroid Build Coastguard Worker echo "Running tests..." 70*c2e18aaaSAndroid Build Coastguard Worker local run_cmd=$PYTHON 71*c2e18aaaSAndroid Build Coastguard Worker local rc=0 72*c2e18aaaSAndroid Build Coastguard Worker if [[ $COVERAGE == true ]]; then 73*c2e18aaaSAndroid Build Coastguard Worker module_checker 74*c2e18aaaSAndroid Build Coastguard Worker # Clear previously coverage data. 75*c2e18aaaSAndroid Build Coastguard Worker $PYTHON -m coverage erase 76*c2e18aaaSAndroid Build Coastguard Worker # Collect coverage data. 77*c2e18aaaSAndroid Build Coastguard Worker run_cmd="coverage run --source $ATEST_REAL_PATH --append" 78*c2e18aaaSAndroid Build Coastguard Worker fi 79*c2e18aaaSAndroid Build Coastguard Worker 80*c2e18aaaSAndroid Build Coastguard Worker for test_file in $(find $ATEST_DIR -name "*_unittest.py"); do 81*c2e18aaaSAndroid Build Coastguard Worker if ! PYTHONPATH=$(get_pythonpath) $run_cmd $test_file; then 82*c2e18aaaSAndroid Build Coastguard Worker rc=1 83*c2e18aaaSAndroid Build Coastguard Worker echo -e "${RED}$t failed${NC}" 84*c2e18aaaSAndroid Build Coastguard Worker fi 85*c2e18aaaSAndroid Build Coastguard Worker done 86*c2e18aaaSAndroid Build Coastguard Worker echo 87*c2e18aaaSAndroid Build Coastguard Worker print_summary $rc 88*c2e18aaaSAndroid Build Coastguard Worker return $rc 89*c2e18aaaSAndroid Build Coastguard Worker} 90*c2e18aaaSAndroid Build Coastguard Worker 91*c2e18aaaSAndroid Build Coastguard Worker# Let's check if anything is passed in, if not we assume the user is invoking 92*c2e18aaaSAndroid Build Coastguard Worker# script, but if we get a list of files, assume it's the PREUPLOAD hook. 93*c2e18aaaSAndroid Build Coastguard Workerread -ra PREUPLOAD_FILES <<< "$@" 94*c2e18aaaSAndroid Build Coastguard Workerif [[ ${#PREUPLOAD_FILES[@]} -eq 0 ]]; then 95*c2e18aaaSAndroid Build Coastguard Worker run_atest_unittests; exit $? 96*c2e18aaaSAndroid Build Coastguard Workerelif [[ "${#PREUPLOAD_FILES[@]}" -eq 1 && "${PREUPLOAD_FILES}" == "coverage" ]]; then 97*c2e18aaaSAndroid Build Coastguard Worker COVERAGE=true run_atest_unittests; exit $? 98*c2e18aaaSAndroid Build Coastguard Workerelse 99*c2e18aaaSAndroid Build Coastguard Worker for f in ${PREUPLOAD_FILES[@]}; do 100*c2e18aaaSAndroid Build Coastguard Worker # We only want to run this unittest if atest files have been touched. 101*c2e18aaaSAndroid Build Coastguard Worker if [[ $f == atest/* ]]; then 102*c2e18aaaSAndroid Build Coastguard Worker run_atest_unittests; exit $? 103*c2e18aaaSAndroid Build Coastguard Worker fi 104*c2e18aaaSAndroid Build Coastguard Worker done 105*c2e18aaaSAndroid Build Coastguard Workerfi 106