blob: 160b533d89d82698ee205a57e8d2db282836056b [file] [log] [blame]
James Lemieux2a2559e2018-01-28 02:56:27 -08001#!/bin/bash
2#
3# Runs robolectric tests.
4
5set -euo pipefail
6
7# Terminate with a fatal error.
8function fatal() {
9 echo "Fatal: $*"
10 exit 113
11}
12
13# Ensures that the given variable is set.
14function 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.
25function 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 validate_var 'XML_OUTPUT_FILE'
39 validate_var 'TEST_WORKSPACE'
40}
41
42# Remove leading and trailing spaces around the given argument.
43function strip() {
44 local value="$1"; shift || fatal "Missing argument: value"
45 test $# = 0 || fatal "Too many arguments"
46
47 echo "$value" | sed -e 's/^ *//' -e 's/ *$//'
48}
49
50# Normalizes a list of paths and turns it into a colon-separated list.
51function normalize-path-list() {
52 echo "$@" | sed -e 's/^ *//' -e 's/ *$//' -e 's/ */ /g' -e 's/ /:/g'
53}
54
55function junit() {
56 # This adds the lib folder to the cp.
57 local classpath="$(strip "$(normalize-path-list "${PRIVATE_JARS}")")"
58 local command=(
59 "${PRIVATE_ROBOLECTRIC_SCRIPT_PATH}/java-timeout"
60 "${PRIVATE_TIMEOUT}"
61 ${PRIVATE_JAVA_ARGS}
62 -Drobolectric.dependency.dir="${PRIVATE_ROBOLECTRIC_PATH}"
63 -Drobolectric.offline=true
64 -Drobolectric.logging=stdout
65 -cp "$classpath"
66 com.android.junitxml.JUnitXmlRunner
67 )
68 echo "${command[@]}" "$@"
69 "${command[@]}" "$@"
70}
71
72function runtests() {
73 local tests="$1"; shift || fatal "Missing argument: tests"
74 test $# = 0 || fatal "Too many arguments"
75
76 if [[ "$(strip "${PRIVATE_RUN_INDIVIDUALLY}")" = 'true' ]]; then
77 local result=0
78 for test in ${tests}; do
79 echo "-------------------------------------------------------------------"
80 echo "Running $test:"
81 junit "${test}"
82 done
83 return "$result"
84 else
85 echo "-------------------------------------------------------------------"
86 echo "Running $tests:"
87 junit $tests # Contains a space-separated list of tests.
88 fi
89}
90
91# Run the robolectric tests
92function run() {
93 test $# = 0 || fatal "Too many arguments"
94
95 [ "${PRIVATE_TARGET_MESSAGE}" == '' ] || echo "${PRIVATE_TARGET_MESSAGE}"
96 local tests="${PRIVATE_TESTS}"
97 if [ "$tests" = '' ]; then
98 # Somehow there are no tests to run. Assume this is failure.
99 echo "No tests to run."
100 exit 1
101 fi
102 local output="${PRIVATE_INTERMEDIATES}/output.out"
103 local failed="${PRIVATE_INTERMEDIATES}/failed.out"
104 local result=0
105 runtests "${tests}" >"$output" 2>&1 || result="$?"
106 echo "$output"
107 cat "$output"
108 if [ "$result" = 0 ]; then
109 return "$result"
110 fi
111 "${PRIVATE_ROBOLECTRIC_SCRIPT_PATH}/list_failed.sh" <"$output" >"$failed"
112 return "$result"
113}
114
115function main() {
116 test $# = 0 || fatal "Too many arguments"
117
118 validate_vars
119 run
120}
121
122main "$@"