Kevin Cheng | a80c753 | 2017-10-25 10:50:15 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (C) 2017 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # A simple helper script that runs all of the atest unit tests. |
Jim Tang | 597265e | 2019-10-22 15:52:57 +0800 | [diff] [blame] | 18 | # There are 2 situations that we take care of: |
| 19 | # 1. User wants to invoke this script directly. |
Kevin Cheng | a80c753 | 2017-10-25 10:50:15 -0700 | [diff] [blame] | 20 | # 2. PREUPLOAD hook invokes this script. |
| 21 | |
Jim Tang | 597265e | 2019-10-22 15:52:57 +0800 | [diff] [blame] | 22 | ATEST_DIR=$(dirname $0) |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 23 | [ "$(uname -s)" == "Darwin" ] && { realpath(){ echo "$(cd $(dirname $1);pwd -P)/$(basename $1)"; }; } |
Jim Tang | 597265e | 2019-10-22 15:52:57 +0800 | [diff] [blame] | 24 | ATEST_REAL_PATH=$(realpath $ATEST_DIR) |
Kevin Cheng | 7edb0b9 | 2017-12-14 15:00:25 -0800 | [diff] [blame] | 25 | RED='\033[0;31m' |
| 26 | GREEN='\033[0;32m' |
| 27 | NC='\033[0m' # No Color |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 28 | COVERAGE=false |
Kevin Cheng | 7edb0b9 | 2017-12-14 15:00:25 -0800 | [diff] [blame] | 29 | |
Jim Tang | 597265e | 2019-10-22 15:52:57 +0800 | [diff] [blame] | 30 | function get_pythonpath() { |
| 31 | echo "$ATEST_REAL_PATH:$PYTHONPATH" |
Kevin Cheng | 7edb0b9 | 2017-12-14 15:00:25 -0800 | [diff] [blame] | 32 | } |
Kevin Cheng | a80c753 | 2017-10-25 10:50:15 -0700 | [diff] [blame] | 33 | |
yangbill | cfedf00 | 2018-12-12 11:36:44 +0800 | [diff] [blame] | 34 | function print_summary() { |
| 35 | local test_results=$1 |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 36 | if [[ $COVERAGE == true ]]; then |
yangbill | cfedf00 | 2018-12-12 11:36:44 +0800 | [diff] [blame] | 37 | coverage report -m |
| 38 | coverage html |
| 39 | fi |
| 40 | if [[ $test_results -eq 0 ]]; then |
| 41 | echo -e "${GREEN}All unittests pass${NC}!" |
| 42 | else |
| 43 | echo -e "${RED}There was a unittest failure${NC}" |
| 44 | fi |
| 45 | } |
| 46 | |
Kevin Cheng | a80c753 | 2017-10-25 10:50:15 -0700 | [diff] [blame] | 47 | function run_atest_unittests() { |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 48 | echo "Running tests..." |
| 49 | local run_cmd="python" |
| 50 | local rc=0 |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 51 | if [[ $COVERAGE == true ]]; then |
| 52 | # Clear previously coverage data. |
| 53 | python -m coverage erase |
| 54 | # Collect coverage data. |
| 55 | run_cmd="coverage run --source $ATEST_REAL_PATH --append" |
Kevin Cheng | a80c753 | 2017-10-25 10:50:15 -0700 | [diff] [blame] | 56 | fi |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 57 | |
| 58 | for test_file in $(find $ATEST_DIR -name "*_unittest.py"); do |
Jim Tang | 597265e | 2019-10-22 15:52:57 +0800 | [diff] [blame] | 59 | if ! PYTHONPATH=$(get_pythonpath) $run_cmd $test_file; then |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 60 | rc=1 |
| 61 | echo -e "${RED}$t failed${NC}" |
| 62 | fi |
| 63 | done |
| 64 | echo |
| 65 | print_summary $rc |
| 66 | return $rc |
Kevin Cheng | a80c753 | 2017-10-25 10:50:15 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | # Let's check if anything is passed in, if not we assume the user is invoking |
| 70 | # script, but if we get a list of files, assume it's the PREUPLOAD hook. |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 71 | read -ra PREUPLOAD_FILES <<< "$@" |
| 72 | if [[ ${#PREUPLOAD_FILES[@]} -eq 0 ]]; then |
| 73 | run_atest_unittests; exit $? |
| 74 | elif [[ "${#PREUPLOAD_FILES[@]}" -eq 1 && "${PREUPLOAD_FILES}" == "coverage" ]]; then |
| 75 | COVERAGE=true run_atest_unittests; exit $? |
Kevin Cheng | a80c753 | 2017-10-25 10:50:15 -0700 | [diff] [blame] | 76 | else |
Jim Tang | 6a0ab7e | 2019-04-11 14:29:59 +0800 | [diff] [blame] | 77 | for f in ${PREUPLOAD_FILES[@]}; do |
| 78 | # We only want to run this unittest if atest files have been touched. |
| 79 | if [[ $f == atest/* ]]; then |
| 80 | run_atest_unittests; exit $? |
| 81 | fi |
| 82 | done |
Kevin Cheng | a80c753 | 2017-10-25 10:50:15 -0700 | [diff] [blame] | 83 | fi |