Tri Vo | 05f0189 | 2017-03-02 11:28:31 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 3 | RED='\033[0;31m' |
| 4 | GREEN='\033[0;32m' |
| 5 | NC='\033[0m' # No Color |
Kevin Cheng | ab0b36b | 2018-08-02 14:38:30 -0700 | [diff] [blame] | 6 | ACLOUD_DIR=$(dirname $(realpath $0)) |
| 7 | TOOLS_DIR=$(dirname $ACLOUD_DIR) |
| 8 | THIRD_PARTY_DIR=$(dirname $TOOLS_DIR)/external/python |
| 9 | |
| 10 | function 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 Chiu | 9eb214e | 2018-05-09 14:29:35 +0000 | [diff] [blame] | 25 | |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 26 | function print_summary() { |
| 27 | local test_results=$1 |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 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 | |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 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 | |
| 41 | function run_unittests() { |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 42 | local specified_tests=$@ |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 43 | local rc=0 |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 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 |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 48 | |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 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; |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 68 | do |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 69 | if ! PYTHONPATH=$(get_python_path):$PYTHONPATH $run_cmd $t; then |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 70 | rc=1 |
| 71 | echo -e "${RED}$t failed${NC}" |
| 72 | fi |
| 73 | done |
| 74 | |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 75 | print_summary $rc |
Kevin Cheng | 8131d75 | 2018-06-06 14:38:43 -0700 | [diff] [blame] | 76 | cleanup |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 77 | exit $rc |
| 78 | } |
| 79 | |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 80 | function 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 Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 85 | |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 86 | local missing_py_packages=false |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 87 | for py_lib in {coverage,mock}; |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 88 | 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 Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 98 | |
Kevin Cheng | 8131d75 | 2018-06-06 14:38:43 -0700 | [diff] [blame] | 99 | function 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 | |
| 108 | function 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 Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 116 | check_env |
Kevin Cheng | 8131d75 | 2018-06-06 14:38:43 -0700 | [diff] [blame] | 117 | cleanup |
| 118 | gen_proto_py |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 119 | run_unittests $@ |