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 specified.
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_HOST_OUT" ]; then
82        echo "Missing ANDROID_HOST_OUT env variable. Run 'lunch' first."
83        exit 1
84    fi
85    if [ ! -f "$ANDROID_HOST_OUT/bin/aprotoc" ]; then
86        echo "Missing aprotoc. Run 'm aprotoc' first."
87        exit 1
88    fi
89
90    local missing_py_packages=false
91    for py_lib in {coverage,mock};
92    do
93        if ! python -m pip list | grep $py_lib &> /dev/null; then
94            echo "Missing required python package: $py_lib (python -m pip install $py_lib)"
95            missing_py_packages=true
96        fi
97    done
98    if $missing_py_packages; then
99        exit 1
100    fi
101}
102
103function gen_proto_py() {
104    # Use aprotoc to generate python proto files.
105    local protoc_cmd=$ANDROID_HOST_OUT/bin/aprotoc
106    pushd $ACLOUD_DIR &> /dev/null
107    $protoc_cmd internal/proto/*.proto --python_out=./
108    touch internal/proto/__init__.py
109    popd &> /dev/null
110}
111
112function cleanup() {
113    # Search for *.pyc and delete them.
114    find $ACLOUD_DIR -name "*.pyc" -exec rm -f {} \;
115
116    # Delete the generated proto files too.
117    find $ACLOUD_DIR/internal/proto -name "*.py" -exec rm -f {} \;
118}
119
120check_env
121cleanup
122gen_proto_py
123run_unittests $@
124