blob: 3a92df57ff99baabd291f4734da49faa5fd6d359 [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"
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}
Sam Chiu9eb214e2018-05-09 14:29:35 +000025
Sam Chiu6add5432018-05-21 18:03:56 +080026function print_summary() {
27 local test_results=$1
Kevin Cheng1f471dc2018-05-30 00:04:32 -070028 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
Sam Chiu6add5432018-05-21 18:03:56 +080034 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() {
Kevin Cheng070ae5c2018-08-02 16:03:00 -070042 local specified_tests=$@
Sam Chiu6add5432018-05-21 18:03:56 +080043 local rc=0
Kevin Cheng1f471dc2018-05-30 00:04:32 -070044 local run_cmd="python -m coverage run --append"
45
46 # clear previously collected coverage data.
47 PYTHONPATH=$(get_python_path) python -m coverage erase
Sam Chiu6add5432018-05-21 18:03:56 +080048
Kevin Cheng070ae5c2018-08-02 16:03:00 -070049 # 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;
Sam Chiu6add5432018-05-21 18:03:56 +080068 do
Kevin Cheng1f471dc2018-05-30 00:04:32 -070069 if ! PYTHONPATH=$(get_python_path):$PYTHONPATH $run_cmd $t; then
Sam Chiu6add5432018-05-21 18:03:56 +080070 rc=1
71 echo -e "${RED}$t failed${NC}"
72 fi
73 done
74
Kevin Cheng1f471dc2018-05-30 00:04:32 -070075 print_summary $rc
Kevin Cheng8131d752018-06-06 14:38:43 -070076 cleanup
Sam Chiu6add5432018-05-21 18:03:56 +080077 exit $rc
78}
79
Kevin Cheng1f471dc2018-05-30 00:04:32 -070080function 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
Sam Chiu6add5432018-05-21 18:03:56 +080085
Kevin Cheng1f471dc2018-05-30 00:04:32 -070086 local missing_py_packages=false
Kevin Cheng070ae5c2018-08-02 16:03:00 -070087 for py_lib in {coverage,mock};
Kevin Cheng1f471dc2018-05-30 00:04:32 -070088 do
89 if ! pip list --format=legacy | 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}
Sam Chiu6add5432018-05-21 18:03:56 +080098
Kevin Cheng8131d752018-06-06 14:38:43 -070099function 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
Kevin Cheng1f471dc2018-05-30 00:04:32 -0700116check_env
Kevin Cheng8131d752018-06-06 14:38:43 -0700117cleanup
118gen_proto_py
Kevin Cheng070ae5c2018-08-02 16:03:00 -0700119run_unittests $@