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        "uritemplates"
18        "rsa"
19    )
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}
27
28function print_summary() {
29    local test_results=$1
30    local tmp_dir=$(mktemp -d)
31    local rc_file=${ACLOUD_DIR}/.coveragerc
32    PYTHONPATH=$(get_python_path) python3 -m coverage report -m
33    PYTHONPATH=$(get_python_path) python3 -m coverage html -d $tmp_dir --rcfile=$rc_file
34    echo "coverage report available at file://${tmp_dir}/index.html"
35
36    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() {
44    local specified_tests=$@
45    local rc=0
46    local run_cmd="python3 -m coverage run --append"
47
48    # clear previously collected coverage data.
49    PYTHONPATH=$(get_python_path) python3 -m coverage erase
50
51    # 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
55    # Filter out the tests if specified.
56    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;
70    do
71        if ! PYTHONPATH=$(get_python_path):$PYTHONPATH $run_cmd $t; then
72            rc=1
73            echo -e "${RED}$t failed${NC}"
74        fi
75    done
76
77    print_summary $rc
78    cleanup
79    exit $rc
80}
81
82function check_env() {
83    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."
89        exit 1
90    fi
91
92    local missing_py_packages=false
93    for py_lib in {coverage,mock};
94    do
95        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)"
97            missing_py_packages=true
98        fi
99    done
100    if $missing_py_packages; then
101        exit 1
102    fi
103}
104
105function gen_proto_py() {
106    # Use aprotoc to generate python proto files.
107    local protoc_cmd=$ANDROID_HOST_OUT/bin/aprotoc
108    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
122check_env
123cleanup
124gen_proto_py
125run_unittests $@
126