Home | History | Annotate | Download | only in 3.1.1
      1 #!/bin/bash
      2 #
      3 # Runs robolectric tests.
      4 
      5 set -euo pipefail
      6 
      7 # Terminate with a fatal error.
      8 function fatal() {
      9   echo "Fatal: $*"
     10   exit 113
     11 }
     12 
     13 # Ensures that the given variable is set.
     14 function validate_var() {
     15   local name="$1"; shift || fatal "Missing argument: name"
     16   test $# = 0 || fatal "Too many arguments"
     17 
     18   eval [[ -n \${${name}+dummy} ]] || {
     19     echo "Variable not set: $name";
     20     return 1;
     21   }
     22 }
     23 
     24 # Ensures that all the required variables are set.
     25 function validate_vars() {
     26   test $# = 0 || fatal "Too many arguments"
     27 
     28   validate_var 'PRIVATE_INTERMEDIATES'
     29   validate_var 'PRIVATE_JARS'
     30   validate_var 'PRIVATE_JAVA_ARGS'
     31   validate_var 'PRIVATE_ROBOLECTRIC_PATH'
     32   validate_var 'PRIVATE_ROBOLECTRIC_SCRIPT_PATH'
     33   validate_var 'PRIVATE_RUN_INDIVIDUALLY'
     34   validate_var 'PRIVATE_TARGET_MESSAGE'
     35   validate_var 'PRIVATE_TESTS'
     36   validate_var 'PRIVATE_TIMEOUT'
     37 }
     38 
     39 # Remove leading and trailing spaces around the given argument.
     40 function strip() {
     41   local value="$1"; shift || fatal "Missing argument: value"
     42   test $# = 0 || fatal "Too many arguments"
     43 
     44   echo "$value" | sed -e 's/^ *//' -e 's/ *$//'
     45 }
     46 
     47 # Normalizes a list of paths and turns it into a colon-separated list.
     48 function normalize-path-list() {
     49   echo "$@" | sed -e 's/^ *//' -e 's/ *$//' -e 's/  */ /g' -e 's/ /:/g'
     50 }
     51 
     52 function junit() {
     53   # This adds the lib folder to the cp.
     54   local classpath="$(strip "$(normalize-path-list "${PRIVATE_JARS}")")"
     55   local command=(
     56     "${PRIVATE_ROBOLECTRIC_SCRIPT_PATH}/java-timeout"
     57     "${PRIVATE_TIMEOUT}"
     58     ${PRIVATE_JAVA_ARGS}
     59     -Drobolectric.dependency.dir="${PRIVATE_ROBOLECTRIC_PATH}"
     60     -Drobolectric.offline=true
     61     -Drobolectric.logging=stdout
     62     -cp "$classpath"
     63     org.junit.runner.JUnitCore
     64   )
     65   echo "${command[@]}" "$@"
     66   "${command[@]}" "$@"
     67 }
     68 
     69 function runtests() {
     70   local tests="$1"; shift || fatal "Missing argument: tests"
     71   test $# = 0 || fatal "Too many arguments"
     72 
     73   if [[ "$(strip "${PRIVATE_RUN_INDIVIDUALLY}")" = 'true' ]]; then
     74     local result=0
     75     for test in ${tests}; do
     76       echo "-------------------------------------------------------------------"
     77       echo "Running $test:"
     78       junit "${test}"
     79     done
     80     return "$result"
     81   else
     82     echo "-------------------------------------------------------------------"
     83     echo "Running $tests:"
     84     junit $tests  # Contains a space-separated list of tests.
     85   fi
     86 }
     87 
     88 # Run the robolectric tests with retries for flaky tests.
     89 function run() {
     90   test $# = 0 || fatal "Too many arguments"
     91 
     92   [ "${PRIVATE_TARGET_MESSAGE}" == '' ] || echo "${PRIVATE_TARGET_MESSAGE}"
     93   local tests="${PRIVATE_TESTS}"
     94   if [ "$tests" = '' ]; then
     95     # Somehow there are no tests to run. Assume this is failure.
     96     echo "No tests to run."
     97     exit 1
     98   fi
     99   local output="${PRIVATE_INTERMEDIATES}/output.out"
    100   local failed="${PRIVATE_INTERMEDIATES}/failed.out"
    101   local result=0
    102   runtests "${tests}" >"$output" 2>&1 || result="$?"
    103   echo "$output"
    104   cat "$output"
    105   if [ "$result" = 0 ]; then
    106     return "$result"
    107   fi
    108   "${PRIVATE_ROBOLECTRIC_SCRIPT_PATH}/list_failed.sh" <"$output" >"$failed"
    109   return "$result"
    110 }
    111 
    112 function main() {
    113   test $# = 0 || fatal "Too many arguments"
    114 
    115   validate_vars
    116   run
    117 }
    118 
    119 main "$@"
    120