blob: c1ee76f2b9e91d28ed4360fea24923daae9aae1c [file] [log] [blame]
Tri Vo05f01892017-03-02 11:28:31 -08001#!/bin/bash
2
Kevin Chengb5963882018-05-09 00:06:27 -07003RED='\033[0;31m'
4GREEN='\033[0;32m'
5NC='\033[0m' # No Color
Kevin Chengab0b36b2018-08-02 14:38:30 -07006ACLOUD_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"
Sam Chiub6bf79f2020-06-09 20:47:42 +080017 "uritemplates"
18 "rsa"
Kevin Chengab0b36b2018-08-02 14:38:30 -070019 )
20 for lib in ${third_party_libs[*]};
21 do
22 python_path=$THIRD_PARTY_DIR/$lib:$python_path
23 done
24 python_path=$python_path:$PYTHONPATH
25 echo $python_path
26}
Sam Chiu9eb214e2018-05-09 14:29:35 +000027
Sam Chiu6add5432018-05-21 18:03:56 +080028function print_summary() {
29 local test_results=$1
Kevin Cheng1f471dc2018-05-30 00:04:32 -070030 local tmp_dir=$(mktemp -d)
31 local rc_file=${ACLOUD_DIR}/.coveragerc
Sam Chiub6bf79f2020-06-09 20:47:42 +080032 PYTHONPATH=$(get_python_path) python3 -m coverage report -m
33 PYTHONPATH=$(get_python_path) python3 -m coverage html -d $tmp_dir --rcfile=$rc_file
Kevin Cheng1f471dc2018-05-30 00:04:32 -070034 echo "coverage report available at file://${tmp_dir}/index.html"
35
Sam Chiu6add5432018-05-21 18:03:56 +080036 if [[ $test_results -eq 0 ]]; then
37 echo -e "${GREEN}All unittests pass${NC}!"
38 else
39 echo -e "${RED}There was a unittest failure${NC}"
40 fi
41}
42
43function run_unittests() {
Kevin Cheng070ae5c2018-08-02 16:03:00 -070044 local specified_tests=$@
Sam Chiu6add5432018-05-21 18:03:56 +080045 local rc=0
Sam Chiub6bf79f2020-06-09 20:47:42 +080046 local run_cmd="python3 -m coverage run --append"
Kevin Cheng1f471dc2018-05-30 00:04:32 -070047
48 # clear previously collected coverage data.
Sam Chiub6bf79f2020-06-09 20:47:42 +080049 PYTHONPATH=$(get_python_path) python3 -m coverage erase
Sam Chiu6add5432018-05-21 18:03:56 +080050
Kevin Cheng070ae5c2018-08-02 16:03:00 -070051 # Get all unit tests under tools/acloud.
52 local all_tests=$(find $ACLOUD_DIR -type f -name "*_test.py" ! -name "acloud_test.py");
53 local tests_to_run=$all_tests
54
Sam Chiub6bf79f2020-06-09 20:47:42 +080055 # Filter out the tests if specified.
Kevin Cheng070ae5c2018-08-02 16:03:00 -070056 if [[ ! -z $specified_tests ]]; then
57 tests_to_run=()
58 for t in $all_tests;
59 do
60 for t_pattern in $specified_tests;
61 do
62 if [[ "$t" =~ "$t_pattern" ]]; then
63 tests_to_run=("${tests_to_run[@]}" "$t")
64 fi
65 done
66 done
67 fi
68
69 for t in $tests_to_run;
Sam Chiu6add5432018-05-21 18:03:56 +080070 do
Kevin Cheng1f471dc2018-05-30 00:04:32 -070071 if ! PYTHONPATH=$(get_python_path):$PYTHONPATH $run_cmd $t; then
Sam Chiu6add5432018-05-21 18:03:56 +080072 rc=1
73 echo -e "${RED}$t failed${NC}"
74 fi
75 done
76
Kevin Cheng1f471dc2018-05-30 00:04:32 -070077 print_summary $rc
Kevin Cheng8131d752018-06-06 14:38:43 -070078 cleanup
Sam Chiu6add5432018-05-21 18:03:56 +080079 exit $rc
80}
81
Kevin Cheng1f471dc2018-05-30 00:04:32 -070082function check_env() {
Dan Willemsen18f460b2020-05-27 18:39:24 -070083 if [ -z "$ANDROID_HOST_OUT" ]; then
84 echo "Missing ANDROID_HOST_OUT env variable. Run 'lunch' first."
85 exit 1
86 fi
87 if [ ! -f "$ANDROID_HOST_OUT/bin/aprotoc" ]; then
88 echo "Missing aprotoc. Run 'm aprotoc' first."
Kevin Cheng1f471dc2018-05-30 00:04:32 -070089 exit 1
90 fi
Sam Chiu6add5432018-05-21 18:03:56 +080091
Kevin Cheng1f471dc2018-05-30 00:04:32 -070092 local missing_py_packages=false
Kevin Cheng070ae5c2018-08-02 16:03:00 -070093 for py_lib in {coverage,mock};
Kevin Cheng1f471dc2018-05-30 00:04:32 -070094 do
Sam Chiub6bf79f2020-06-09 20:47:42 +080095 if ! python3 -m pip list | grep $py_lib &> /dev/null; then
96 echo "Missing required python package: $py_lib (python3 -m pip install $py_lib)"
Kevin Cheng1f471dc2018-05-30 00:04:32 -070097 missing_py_packages=true
98 fi
99 done
100 if $missing_py_packages; then
101 exit 1
102 fi
103}
Sam Chiu6add5432018-05-21 18:03:56 +0800104
Kevin Cheng8131d752018-06-06 14:38:43 -0700105function gen_proto_py() {
106 # Use aprotoc to generate python proto files.
Dan Willemsen18f460b2020-05-27 18:39:24 -0700107 local protoc_cmd=$ANDROID_HOST_OUT/bin/aprotoc
Kevin Cheng8131d752018-06-06 14:38:43 -0700108 pushd $ACLOUD_DIR &> /dev/null
109 $protoc_cmd internal/proto/*.proto --python_out=./
110 touch internal/proto/__init__.py
111 popd &> /dev/null
112}
113
114function cleanup() {
115 # Search for *.pyc and delete them.
116 find $ACLOUD_DIR -name "*.pyc" -exec rm -f {} \;
117
118 # Delete the generated proto files too.
119 find $ACLOUD_DIR/internal/proto -name "*.py" -exec rm -f {} \;
120}
121
Kevin Cheng1f471dc2018-05-30 00:04:32 -0700122check_env
Kevin Cheng8131d752018-06-06 14:38:43 -0700123cleanup
124gen_proto_py
Kevin Cheng070ae5c2018-08-02 16:03:00 -0700125run_unittests $@