1#!/bin/bash 2 3RED='\033[0;31m' 4GREEN='\033[0;32m' 5NC='\033[0m' # No Color 6ACLOUD_DIR=$(dirname $(realpath $0)) 7TOOLS_DIR=$(dirname $ACLOUD_DIR) 8THIRD_PARTY_DIR=$(dirname $TOOLS_DIR)/external/python 9 10function get_python_path() { 11 local python_path=$TOOLS_DIR 12 local third_party_libs=( 13 "apitools" 14 "dateutil" 15 "google-api-python-client" 16 "oauth2client" 17 ) 18 for lib in ${third_party_libs[*]}; 19 do 20 python_path=$THIRD_PARTY_DIR/$lib:$python_path 21 done 22 python_path=$python_path:$PYTHONPATH 23 echo $python_path 24} 25 26function print_summary() { 27 local test_results=$1 28 local tmp_dir=$(mktemp -d) 29 local rc_file=${ACLOUD_DIR}/.coveragerc 30 PYTHONPATH=$(get_python_path) python -m coverage report -m 31 PYTHONPATH=$(get_python_path) python -m coverage html -d $tmp_dir --rcfile=$rc_file 32 echo "coverage report available at file://${tmp_dir}/index.html" 33 34 if [[ $test_results -eq 0 ]]; then 35 echo -e "${GREEN}All unittests pass${NC}!" 36 else 37 echo -e "${RED}There was a unittest failure${NC}" 38 fi 39} 40 41function run_unittests() { 42 local specified_tests=$@ 43 local rc=0 44 local run_cmd="python -m coverage run --append" 45 46 # clear previously collected coverage data. 47 PYTHONPATH=$(get_python_path) python -m coverage erase 48 49 # Get all unit tests under tools/acloud. 50 local all_tests=$(find $ACLOUD_DIR -type f -name "*_test.py" ! -name "acloud_test.py"); 51 local tests_to_run=$all_tests 52 53 # Filter out the tests if specifed. 54 if [[ ! -z $specified_tests ]]; then 55 tests_to_run=() 56 for t in $all_tests; 57 do 58 for t_pattern in $specified_tests; 59 do 60 if [[ "$t" =~ "$t_pattern" ]]; then 61 tests_to_run=("${tests_to_run[@]}" "$t") 62 fi 63 done 64 done 65 fi 66 67 for t in $tests_to_run; 68 do 69 if ! PYTHONPATH=$(get_python_path):$PYTHONPATH $run_cmd $t; then 70 rc=1 71 echo -e "${RED}$t failed${NC}" 72 fi 73 done 74 75 print_summary $rc 76 cleanup 77 exit $rc 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 86 local missing_py_packages=false 87 for py_lib in {coverage,mock}; 88 do 89 if ! pip list | grep $py_lib &> /dev/null; then 90 echo "Missing required python package: $py_lib (pip install $py_lib)" 91 missing_py_packages=true 92 fi 93 done 94 if $missing_py_packages; then 95 exit 1 96 fi 97} 98 99function gen_proto_py() { 100 # Use aprotoc to generate python proto files. 101 local protoc_cmd=$ANDROID_BUILD_TOP/prebuilts/misc/linux-x86/protobuf/aprotoc 102 pushd $ACLOUD_DIR &> /dev/null 103 $protoc_cmd internal/proto/*.proto --python_out=./ 104 touch internal/proto/__init__.py 105 popd &> /dev/null 106} 107 108function cleanup() { 109 # Search for *.pyc and delete them. 110 find $ACLOUD_DIR -name "*.pyc" -exec rm -f {} \; 111 112 # Delete the generated proto files too. 113 find $ACLOUD_DIR/internal/proto -name "*.py" -exec rm -f {} \; 114} 115 116check_env 117cleanup 118gen_proto_py 119run_unittests $@ 120