Atest: parse PREUPLOAD_FILES in unittest script.

Previously the unittest script ineffective parses PREUPLOAD_FILES from
repohook which resulted in running atest unittest even the changed codes
are non-atest. This CL enables the unittest script reading
PREUPLOAD_FILES as an array correctly so that uploading tradefederation
codes won't trigger atest unittests.

Bug: 130245590

Test: source build/envsetup.sh && lunch
      cd tools/tradefederation/core
      # manually run unittest.
      atest/run_atest_unittests.sh  # without coverage
      atest/run_atest_unittests.sh coverage # print code coverage statistic.
      atest/run_atest_unittests.sh src/com/android/tradefed/host/HostOptions.java
      # won't trigger atest unittests.

Change-Id: I8dc23de5e2fc30e1d1f579431142aa0e54169d71
diff --git a/atest/run_atest_unittests.sh b/atest/run_atest_unittests.sh
index 6759456..09310cf 100755
--- a/atest/run_atest_unittests.sh
+++ b/atest/run_atest_unittests.sh
@@ -20,22 +20,22 @@
 #   2. PREUPLOAD hook invokes this script.
 
 ATEST_DIR=`dirname $0`/
+[ "$(uname -s)" == "Darwin" ] && { realpath(){ echo "$(cd $(dirname $1);pwd -P)/$(basename $1)"; }; }
 ATEST_REAL_PATH=`realpath $ATEST_DIR`
-PREUPLOAD_FILES=$@
 RED='\033[0;31m'
 GREEN='\033[0;32m'
 NC='\033[0m' # No Color
+COVERAGE=false
 
 function set_pythonpath() {
-  if ! echo $PYTHONPATH | grep -q $ATEST_REAL_PATH; then
-    PYTHONPATH=$ATEST_REAL_PATH:$PYTHONPATH
-  fi
+    if ! echo $PYTHONPATH | grep -q $ATEST_REAL_PATH; then
+        PYTHONPATH=$ATEST_REAL_PATH:$PYTHONPATH
+    fi
 }
 
 function print_summary() {
     local test_results=$1
-    local coverage_run=$2
-    if [[ $coverage_run == "coverage" ]]; then
+    if [[ $COVERAGE == true ]]; then
         coverage report -m
         coverage html
     fi
@@ -47,49 +47,40 @@
 }
 
 function run_atest_unittests() {
-  echo "Running tests..."
-  local coverage_run=$1
-  local run_cmd="python"
-  local rc=0
-  set_pythonpath $coverage_run
-  if [[ $coverage_run == "coverage" ]]; then
-      # Clear previously coverage data.
-      python -m coverage erase
-      # Collected coverage data.
-      run_cmd="coverage run --source $ATEST_REAL_PATH --append"
-  fi
-
-  for test_file in $(find $ATEST_DIR -name "*_unittest.py"); do
-    if ! $run_cmd $test_file; then
-      rc=1
-      echo -e "${RED}$t failed${NC}"
+    echo "Running tests..."
+    local run_cmd="python"
+    local rc=0
+    set_pythonpath
+    if [[ $COVERAGE == true ]]; then
+        # Clear previously coverage data.
+        python -m coverage erase
+        # Collect coverage data.
+        run_cmd="coverage run --source $ATEST_REAL_PATH --append"
     fi
-  done
-  echo
-  print_summary $rc $coverage_run
-  return $rc
+
+    for test_file in $(find $ATEST_DIR -name "*_unittest.py"); do
+        if ! $run_cmd $test_file; then
+          rc=1
+          echo -e "${RED}$t failed${NC}"
+        fi
+    done
+    echo
+    print_summary $rc
+    return $rc
 }
 
 # Let's check if anything is passed in, if not we assume the user is invoking
 # script, but if we get a list of files, assume it's the PREUPLOAD hook.
-if [[ -z $PREUPLOAD_FILES ]]; then
-  run_atest_unittests
-  exit $?
+read -ra PREUPLOAD_FILES <<< "$@"
+if [[ ${#PREUPLOAD_FILES[@]} -eq 0 ]]; then
+    run_atest_unittests; exit $?
+elif [[ "${#PREUPLOAD_FILES[@]}" -eq 1 && "${PREUPLOAD_FILES}" == "coverage" ]]; then
+    COVERAGE=true run_atest_unittests; exit $?
 else
-  for f in $PREUPLOAD_FILES; do
-    # We only want to run this unittest if atest files have been touched.
-    if [[ $f == atest/* ]]; then
-      run_atest_unittests
-      exit $?
-    fi
-  done
+    for f in ${PREUPLOAD_FILES[@]}; do
+        # We only want to run this unittest if atest files have been touched.
+        if [[ $f == atest/* ]]; then
+            run_atest_unittests; exit $?
+        fi
+    done
 fi
-
-case "$1" in
-    'coverage')
-        run_atest_unittests "coverage"
-        ;;
-    *)
-        run_atest_unittests
-        ;;
-esac