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" |
Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 17 | "uritemplates" |
| 18 | "rsa" |
Kevin Cheng | ab0b36b | 2018-08-02 14:38:30 -0700 | [diff] [blame] | 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 | } |
Sam Chiu | 9eb214e | 2018-05-09 14:29:35 +0000 | [diff] [blame] | 27 | |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 28 | function print_summary() { |
| 29 | local test_results=$1 |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 30 | local tmp_dir=$(mktemp -d) |
| 31 | local rc_file=${ACLOUD_DIR}/.coveragerc |
Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 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 |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 34 | echo "coverage report available at file://${tmp_dir}/index.html" |
| 35 | |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 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 | |
| 43 | function run_unittests() { |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 44 | local specified_tests=$@ |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 45 | local rc=0 |
Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 46 | local run_cmd="python3 -m coverage run --append" |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 47 | |
| 48 | # clear previously collected coverage data. |
Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 49 | PYTHONPATH=$(get_python_path) python3 -m coverage erase |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 50 | |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 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 | |
Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 55 | # Filter out the tests if specified. |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 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; |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 70 | do |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 71 | if ! PYTHONPATH=$(get_python_path):$PYTHONPATH $run_cmd $t; then |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 72 | rc=1 |
| 73 | echo -e "${RED}$t failed${NC}" |
| 74 | fi |
| 75 | done |
| 76 | |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 77 | print_summary $rc |
Kevin Cheng | 8131d75 | 2018-06-06 14:38:43 -0700 | [diff] [blame] | 78 | cleanup |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 79 | exit $rc |
| 80 | } |
| 81 | |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 82 | function check_env() { |
Dan Willemsen | 18f460b | 2020-05-27 18:39:24 -0700 | [diff] [blame] | 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." |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 89 | exit 1 |
| 90 | fi |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 91 | |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 92 | local missing_py_packages=false |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 93 | for py_lib in {coverage,mock}; |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 94 | do |
Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 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)" |
Kevin Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 97 | missing_py_packages=true |
| 98 | fi |
| 99 | done |
| 100 | if $missing_py_packages; then |
| 101 | exit 1 |
| 102 | fi |
| 103 | } |
Sam Chiu | 6add543 | 2018-05-21 18:03:56 +0800 | [diff] [blame] | 104 | |
Kevin Cheng | 8131d75 | 2018-06-06 14:38:43 -0700 | [diff] [blame] | 105 | function gen_proto_py() { |
| 106 | # Use aprotoc to generate python proto files. |
Dan Willemsen | 18f460b | 2020-05-27 18:39:24 -0700 | [diff] [blame] | 107 | local protoc_cmd=$ANDROID_HOST_OUT/bin/aprotoc |
Kevin Cheng | 8131d75 | 2018-06-06 14:38:43 -0700 | [diff] [blame] | 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 | |
| 114 | function 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 Cheng | 1f471dc | 2018-05-30 00:04:32 -0700 | [diff] [blame] | 122 | check_env |
Kevin Cheng | 8131d75 | 2018-06-06 14:38:43 -0700 | [diff] [blame] | 123 | cleanup |
| 124 | gen_proto_py |
Kevin Cheng | 070ae5c | 2018-08-02 16:03:00 -0700 | [diff] [blame] | 125 | run_unittests $@ |