-
Notifications
You must be signed in to change notification settings - Fork 29
/
culebratester2
executable file
·286 lines (247 loc) · 6.84 KB
/
culebratester2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#! /bin/bash
#
# This can be executed remotely via
#
# bash <(curl -sL https://git.io/JT5nc) [opts...] [args...]
#
# or without the shortened url
#
# bash <(curl -s https://raw.githubusercontent.com/dtmilano/CulebraTester2-public/master/culebratester2) [opts...] [args...]
#
# or locally if you have cloned this repo
#
# ./culebratester2 [opts...] [args...]
#
#
# For interactive use this script also uses "android-select-device" to select the target device.
# It can be installed from https://gist.github.com/dtmilano/4537110'.
# If you have multiple devices connecte and don't want to use "android-select-device" just specify the target serial number
# on the command line either when it's executed remotely or locally
#
# bash <(curl -sL https://git.io/JT5nc) --serialno ABCDE1234 [args...]
#
# ./culebratester2 --serialno ABCDE1234 [args...]
#
# shellcheck disable=SC2086
set -u
ORIGINAL_PKG='com.dtmilano.android.culebratester2'
PKG=${CULEBRATESTER2_PKG:-$ORIGINAL_PKG}
PKGRE=${PKG//\./\\.}
usage() {
printf '%s' "$usage"
exit 1
}
fatal() {
args="$*"
printf '⛔️ %sERROR: %s%s\n' "$red" "$args" "$sgr0" >&2
exit 1
}
_help() {
printf '%s' "$usage"
printf '\n'
printf 'positional arguments:\n'
printf ' forward-port uses adb to forward port %d\n' "$port"
printf ' [--local-port PORT] [--remote-port PORT]\n'
printf ' run-instrumentation runs the instrumentation and starts the server\n'
printf ' start-server starts the server on the device\n'
printf ' install installs on device\n'
printf ' uninstall uninstalls pkgs on device\n'
printf '\n'
printf 'optional arguments:\n'
printf ' -h, --help show this help message and exit\n'
printf ' -n, --dryrun does not execute the commands\n'
printf ' -v, --verbose prints verbose output\n'
printf ' -s, --serialno SERIALNO use the specific device\n'
exit 0
}
check_device() {
if ! adb $D get-state &> /dev/null; then
fatal "Cannot connect to device: $D"
fi
}
check_installed() {
pkgs=$(adb $D shell pm list packages --user 0)
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
fatal 'Cannot get the installed packages.'
fi
n=$(grep -c "$PKGRE" <<<"$pkgs")
case "$n" in
0)
fatal 'app and instrumentation apks should be installed. None found.'
;;
1)
# we are missing one
if ! grep "$PKGRE\.test\$" <<<"$pkgs"; then
fatal 'app found but instrumentation apk has not been installed.'
fi
if ! grep "$PKGRE\$" <<<"$pkgs"; then
fatal 'instrumentation found but app apk has not been installed.'
fi
;;
2)
:
;;
*)
fatal "unexpected number of matching apks installed: $n"
;;
esac
}
forward_port() {
local local_port=$port
local remote_port=$port
while [[ $# -ge 1 ]]; do
case "$1" in
'--local-port')
shift
if [[ $# -lt 1 ]]; then
fatal 'Missing PORT in forward-port'
fi
local_port=$1
;;
'--remote-port')
shift
if [[ $# -lt 1 ]]; then
fatal 'Missing PORT in forward-port'
fi
remote_port=$1
;;
*)
fatal "Invalid option to forward-port: $1"
;;
esac
shift
done
[[ "$verbose" == true ]] && printf 'Forwarding port local:%d => remote:%d\n' "$local_port" "$remote_port"
# adb forward [--no-rebind] LOCAL REMOTE
if ! $dryrun adb $D forward tcp:"$local_port" tcp:"$remote_port"; then
fatal "Could not forward port local:$local_port remote:$remote_port"
fi
}
run_instrumentation() {
printf '%s🐍 Starting CulebraTester2-public service...%s\n' "$green" "$sgr0"
# grant [--user USER_ID] [--all-permissions] PACKAGE PERMISSION
$dryrun adb $D shell pm grant --user 0 "$PKG" 'android.permission.DUMP' && \
$dryrun adb $D shell pm grant --user 0 "$PKG" 'android.permission.PACKAGE_USAGE_STATS' && \
$dryrun adb $D shell pm grant --user 0 "$PKG" 'android.permission.CHANGE_CONFIGURATION' && \
$dryrun adb $D shell am instrument -w -r -e debug false -e class "$ORIGINAL_PKG.UiAutomatorHelper" \
"$PKG.test/androidx.test.runner.AndroidJUnitRunner"
}
uninstall_pkgs() {
$dryrun adb $D uninstall "$PKG.test" && adb $D uninstall "$PKG"
}
if [[ "$0" =~ ^/dev/fd ]]; then
progname='culebratester2'
else
progname="$(basename "$0")"
fi
port=9987
if [[ -n "$TERM" ]] && type tput &>/dev/null; then
smul=$(tput smul)
rmul=$(tput rmul)
red=$(tput setaf 1)
green=$(tput setaf 2)
sgr0=$(tput sgr0)
else
smul=
rmul=
red=
green=
sgr0=
fi
printf -v usage 'usage: %s [-h|--help] [-v|--verbose] [-x|--debig] [-n|-dryrun] [-s|--serialno SERIALNO] {%sf%sorward-port | %sr%sun-instrumentation | %ss%start-server | %si%snstall | %su%sninstall }\n' "$progname" \
"$smul" "$rmul" "$smul" "$rmul" "$smul" "$rmul" "$smul" "$rmul" "$smul" "$rmul"
for arg in "$@"; do
shift
case "$arg" in
'--help')
set -- "$@" '-h'
;;
'--verbose')
set -- "$@" '-v'
;;
'--debug')
set -- "$@" '-x'
;;
'--dryrun')
set -- "$@" '-n'
;;
'--serialno')
set -- "$@" '-s'
;;
*)
set -- "$@" "$arg"
;;
esac
done
# Default behavior
verbose=false
debug=false
dryrun=
serialno=
OPTIND=1
while getopts ":hvxns:" opt
do
case "$opt" in
'h')
_help
;;
'v')
verbose=true
;;
'x')
debug=true
;;
'n')
dryrun='echo'
;;
's')
serialno="$OPTARG"
;;
'?')
usage
;;
esac
done
shift $(( OPTIND - 1 ))
if [[ $# -lt 1 ]]; then
usage
fi
[[ "$debug" == true ]] && set -x
if [[ -n "${serialno}" ]]; then
D="-s ${serialno}"
else
if ! type android-select-device &>/dev/null; then
fatal 'Missing "android-select-device". Install from https://gist.github.com/dtmilano/4537110'
fi
D=$(android-select-device)
fi
case "$1" in
forward-port|f)
shift
forward_port "$@"
;;
run-instrumentation|r)
shift
check_device
run_instrumentation
;;
start-server|s)
shift
check_device
check_installed
forward_port "$@" && run_instrumentation
;;
install|i)
shift
ANDROID_SERIAL="${D#-s }" ./gradlew installDebug installDebugAndroidTest
;;
uninstall|u)
shift
check_device
uninstall_pkgs
;;
*)
usage
;;
esac