Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4efed3fd authored by Scott James Remnant's avatar Scott James Remnant
Browse files

Clean up run_unit_tests wordiness

Preserve word boundaries of command-line arguments, e.g. when the
script is called with "foo bar" an argument (including quotes) don't
break it up into "foo" "bar" before passing to adb shell, etc.

Fixed a few other style issues while in there, and removed the sole
bashism so the script can just use an ordinary shell.

Change-Id: I7f9b2ea08cd7fa35b692d8f7c49006bde6e562eb
parent 7744ec61
Loading
Loading
Loading
Loading
+36 −23
Original line number Diff line number Diff line
#!/usr/bin/env bash

CR=$'\n'
#!/bin/sh

known_tests=(
  net_test_btcore
@@ -10,16 +8,17 @@ known_tests=(
)

usage() {
  echo "Usage: $0 --help"
  echo "       $0 [-i <iterations>] [-s <specific device>] [--all] [<test name>[.<filter>] ...] [--<arg> ...]"
  binary="$(basename "$0")"
  echo "Usage: ${binary} --help"
  echo "       ${binary} [-i <iterations>] [-s <specific device>] [--all] [<test name>[.<filter>] ...] [--<arg> ...]"
  echo
  echo "Unknown long arguments are passed to the test."
  echo
  echo "Known test names:"

  for name in ${known_tests[*]}
  for name in "${known_tests[@]}"
  do
    echo "    $name"
    echo "    ${name}"
  done
}

@@ -27,7 +26,8 @@ iterations=1
device=
tests=()
test_args=()
while [ $# -gt 0 ]; do
while [ $# -gt 0 ]
do
  case "$1" in
    -h|--help)
      usage
@@ -38,7 +38,7 @@ while [ $# -gt 0 ]; do
      if [ $# -eq 0 ]; then
        echo "error: number of iterations expected" 1>&2
        usage
        exit 1
        exit 2
      fi
      iterations=$(( $1 ))
      shift
@@ -48,13 +48,13 @@ while [ $# -gt 0 ]; do
      if [ $# -eq 0 ]; then
        echo "error: no device specified" 1>&2
        usage
        exit 1
        exit 2
      fi
      device="$1"
      shift
      ;;
    --all)
      tests+=( ${known_tests[*]} )
      tests+=( "${known_tests[@]}" )
      shift
      ;;
    --*)
@@ -62,42 +62,55 @@ while [ $# -gt 0 ]; do
      shift
      ;;
    *)
      tests+=( $1 )
      tests+=( "$1" )
      shift
      ;;
  esac
done

if [ ${#tests[*]} -eq 0 ]; then
  tests+=( ${known_tests[*]} )
if [ "${#tests[@]}" -eq 0 ]; then
  tests+=( "${known_tests[@]}" )
fi

adb="adb${device:+ -s $device}"
adb=( "adb" )
if [ -n "${device}" ]; then
  adb+=( "-s" "${device}" )
fi

failed_tests=''
for spec in ${tests[*]}
failed_tests=()
for spec in "${tests[@]}"
do
  name="${spec%%.*}"
  binary="/data/nativetest/${name}/${name}"

  push_command=( "${adb[@]}" push {"${ANDROID_PRODUCT_OUT}",}"${binary}" )
  test_command=( "${adb[@]}" shell "${binary}" )
  if [ "${name}" != "${spec}" ]; then
    filter="${spec#*.}"
    test_command+=( "--gtest_filter=${filter}" )
  fi
  echo "--- $name ---"
  test_command+=( "${test_args[@]}" )

  echo "--- ${name} ---"
  echo "pushing..."
  $adb push {$ANDROID_PRODUCT_OUT,}/data/nativetest/$name/$name
  "${push_command[@]}"
  echo "running..."
  failed_count=0
  for i in $(seq 1 ${iterations})
  do
    $adb shell data/nativetest/$name/$name${filter:+ "--gtest_filter=${filter}"} ${test_args[*]} || failed_count=$(( $failed_count + 1 ))
    "${test_command[@]}" || failed_count=$(( $failed_count + 1 ))
  done

  if [ $failed_count != 0 ]; then
    failed_tests="$failed_tests$CR!!! FAILED TEST: $name ${failed_count}/${iterations} !!!";
    failed_tests+=( "${name} ${failed_count}/${iterations}" )
  fi
done

if [ "$failed_tests" != "" ]; then
  echo "$failed_tests";
if [ "${#failed_tests[@]}" -ne 0 ]; then
  for failed_test in "${failed_tests[@]}"
  do
    echo "!!! FAILED TEST: ${failed_test} !!!"
  done
  exit 1
fi