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.
18# We have 2 situations we take care of:
19#   1. User wants to invoke this script by itself.
20#   2. PREUPLOAD hook invokes this script.
21
22ATEST_DIR=`dirname $0`/
23PREUPLOAD_FILES=$@
24RED='\033[0;31m'
25GREEN='\033[0;32m'
26NC='\033[0m' # No Color
27
28function set_pythonpath() {
29  local path_to_check=`realpath $ATEST_DIR`
30  if ! echo $PYTHONPATH | grep -q $path_to_check; then
31    PYTHONPATH=$path_to_check:$PYTHONPATH
32  fi
33}
34
35function run_atest_unittests() {
36  set_pythonpath
37  local rc=0
38  for test_file in $(find $ATEST_DIR -name "*_unittest.py");
39  do
40    if ! $test_file; then
41      rc=1
42    fi
43  done
44
45  echo
46  if [[ $rc -eq 0 ]]; then
47    echo -e "${GREEN}All unittests pass${NC}!"
48  else
49    echo -e "${RED}There was a unittest failure${NC}"
50  fi
51  return $rc
52}
53
54# Let's check if anything is passed in, if not we assume the user is invoking
55# script, but if we get a list of files, assume it's the PREUPLOAD hook.
56if [[ -z $PREUPLOAD_FILES ]]; then
57  run_atest_unittests
58  exit $?
59else
60  for f in $PREUPLOAD_FILES;
61  do
62    # We only want to run this unittest if atest files have been touched.
63    if [[ $f == atest/* ]]; then
64      run_atest_unittests
65      exit $?
66    fi
67  done
68fi
69