1#!/bin/bash
2
3#
4# Setup.
5#
6
7# Copy the toybox tests across.
8if [[ $(adb shell getprop ro.debuggable) == 1 ]]; then
9  adb shell su root rm -rf /data/local/tmp/toybox-tests/
10fi
11adb shell rm -rf /data/local/tmp/toybox-tests/
12adb shell mkdir /data/local/tmp/toybox-tests/
13adb push tests/ /data/local/tmp/toybox-tests/
14adb push scripts/runtest.sh /data/local/tmp/toybox-tests/
15
16# Make a temporary directory on the device.
17tmp_dir=`adb shell mktemp --directory /data/local/tmp/toybox-tests-tmp.XXXXXXXXXX`
18
19if tty -s; then
20  green="\033[1;32m"
21  red="\033[1;31m"
22  plain="\033[0m"
23else
24  green=""
25  red=""
26  plain=""
27fi
28
29# Force pty allocation (http://b/142798587).
30dash_t="-tt"
31
32test_toy() {
33  toy=$1
34
35  echo
36
37  location=$(adb shell "which $toy")
38  if [ -z "$location" ]; then
39    echo "-- $toy not present"
40    return
41  fi
42
43  echo "-- $toy"
44
45  implementation=$(adb shell "realpath $location")
46  non_toy=false
47  if [ "$implementation" != "/system/bin/toybox" ]; then
48    echo "-- note: $toy is non-toybox implementation"
49    non_toy=true
50  fi
51
52  adb shell $dash_t "\
53      export C=\"\$(which $toy)\"; \
54      export CMDNAME=$toy; \
55      export FILES=/data/local/tmp/toybox-tests/tests/files/ ; \
56      export LANG=en_US.UTF-8; \
57      export VERBOSE=1 ; \
58      mkdir $tmp_dir/$toy && cd $tmp_dir/$toy ; \
59      source /data/local/tmp/toybox-tests/runtest.sh ; \
60      source /data/local/tmp/toybox-tests/tests/$toy.test ; \
61      if [ "\$FAILCOUNT" -ne 0 ]; then exit 1; fi; \
62      cd .. && rm -rf $toy"
63  if [ $? -eq 0 ]; then
64    pass_count=$(($pass_count+1))
65  elif [ "$non_toy" = "true" ]; then
66    non_toy_failures="$non_toy_failures $toy"
67  else
68    failures="$failures $toy"
69  fi
70}
71
72#
73# Run the selected test or all tests.
74#
75
76failures=""
77pass_count=0
78if [ "$#" -eq 0 ]; then
79  # Run all the tests.
80  for t in tests/*.test; do
81    toy=`echo $t | sed 's|tests/||' | sed 's|\.test||'`
82    test_toy $toy
83  done
84else
85  # Just run the tests for the given toys.
86  for toy in "$@"; do
87    test_toy $toy
88  done
89fi
90
91#
92# Show a summary and return a meaningful exit status.
93#
94
95echo
96echo "_________________________________________________________________________"
97echo
98echo -e "${green}PASSED${plain}: $pass_count"
99for failure in $failures; do
100  echo -e "${red}FAILED${plain}: $failure"
101done
102for failure in $non_toy_failures; do
103  echo -e "${red}FAILED${plain}: $failure (ignoring)"
104done
105
106# We should have run *something*...
107if [ $pass_count -eq 0 ]; then exit 1; fi
108# And all failures are bad...
109if [ -n "$failures" ]; then exit 1; fi
110exit 0
111