Files
d2r-mobile/dm/ios/tools/devbattery_test.sh
T
jhaizhouandhubeiqiao 66c3a2fb70 Fix setup on stock macOS bash, shader step false success, battery gate misreport (#4, #5, #7)
- Brace every $NAME that is directly followed by a non-ASCII character in the shell scripts: bash 3.2 in a UTF-8 locale read the first byte of the following character as part of the variable name and aborted under set -u (#4, #6).
- setup.sh and d2rm_pack.py: the shader translation step is complete only when every entry in the completion marker is ok and every library file exists; the script's exit status is no longer hidden by a pipe (#5).
- devbattery.sh / devlock.sh: a missing local timeout or ideviceinfo is reported as such and stops at once, instead of being reported as an offline device and polled for up to an hour (#7).
- smoke_mac.sh builds its package for iPhone only regardless of the project default.

Reported by @SakuraToErii. The setup.sh part of #4 was also submitted by @hubeiqiao in #6.

Co-authored-by: hubeiqiao <12777592+hubeiqiao@users.noreply.github.com>
2026-10-13 03:17:23 +08:00

33 lines
4.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# devbattery.sh 单测:假读数(DEVBATTERY_FAKE)+ 临时目录,不碰设备。期望值按规则手算:< 30 拒并落标记、标记在时 < 60 拒、≥ 60 撤标记放行、读不到拒
# Unit test for devbattery.sh: fake readings (DEVBATTERY_FAKE) + temporary directory, no device touched. Expected values are computed by hand from the rules: < 30 refuse and drop the marker; with the marker present, < 60 refuse; ≥ 60 remove the marker and pass; unreadable refuse
cd "$(dirname "$0")"; T=$(mktemp -d); export DEVBATTERY_DIR=$T; # 单测一律写临时目录,不碰 $DM_ROOT/run/ios 的真日志与告警 | Unit tests always write to a temporary directory and never touch the real logs and alerts in $DM_ROOT/run/ios
source ./devbattery.sh; bad=0
ck() { local want=$1 got; shift; DEVBATTERY_FAKE="$*" devbattery_gate 2>/dev/null; got=$?; [ "$got" = "$want" ] && echo "通 $* → $got" || { echo "不通 $* → ${got}(期望 ${want})"; bad=1; }; }
ck 0 80 0 1 1 # 80%:放行 | 80%: pass
ck 1 25 1 1 0 # 25%:拒,落标记 | 25%: refuse, drop the marker
[ -f $T/device_low_battery ] && echo "通 低电落标记" || { echo "不通 低电没落标记"; bad=1; }
ck 1 45 1 1 0 # 标记在、45%:拒 | Marker present, 45%: refuse
ck 0 60 1 1 0 # 60%:撤标记放行 | 60%: remove the marker and pass
[ ! -f $T/device_low_battery ] && echo "通 撤标记" || { echo "不通 标记没撤"; bad=1; }
ck 0 45 1 1 0 # 没标记、45%:放行 | No marker, 45%: pass
# 本机缺 timeout:与设备离线分开报,返回 3(PATH 换成只有基本命令的目录,UDID 给个假的;期望按规则:读电量前先找工具,找不到即 3)
# timeout missing locally: reported separately from a device being offline, returns 3 (PATH is replaced by a directory with only basic commands, the UDID is a fake one; expected by rule: the tools are looked up before the battery is read, and not finding them gives 3)
E=$T/emptybin; mkdir -p $E; for c in date mkdir dirname cat head awk; do ln -s "$(command -v $c)" $E/$c; done
( PATH=$E DM_IOS_UDID=fake; unset DEVBATTERY_FAKE; devbattery_gate > $T/miss.out 2>&1; echo $? > $T/miss.rc )
[ "$(cat $T/miss.rc)" = 3 ] && grep -q "本机缺读电量要用的命令" $T/miss.out && ! grep -q "离线" $T/miss.out && echo "通 缺 timeout → 3,报缺命令不报离线" || { echo "不通 缺 timeout → $(cat $T/miss.rc):$(head -1 $T/miss.out)"; bad=1; }
out=$(DEVBATTERY_FAKE="50 0 1 0" devbattery_gate 2>&1); case "$out" in *供电跟不上*) echo "通 插电不充记红";; *) echo "不通 插电不充没记红"; bad=1;; esac
out=$(DEVBATTERY_FAKE="99 0 1 0" devbattery_gate 2>&1); case "$out" in *供电跟不上*) echo "不通 99% 停充被当成供电跟不上"; bad=1;; *) echo "通 99% 停充不记红";; esac
DEVBATTERY_FAKE= DM_IOS_UDID=00000000-0000000000000000 devbattery_gate 2>/dev/null && { echo "不通 读不到却放行"; bad=1; } || echo "通 读不到拒"
grep -q '读不到' $T/battery.log && echo "通 读不到记「读不到」" || { echo "不通 读不到没记对"; bad=1; }
DEVBATTERY_FAKE= DM_IOS_UDID= DM_LOCAL_ENV=/dev/null HOME=$T devbattery_gate 2>/dev/null && { echo "不通 没配 UDID 却放行"; bad=1; } || echo "通 没配 UDID 拒"
grep -q '没配 UDID' $T/battery.log && echo "通 没配 UDID 记「没配 UDID」" || { echo "不通 没配 UDID 没分开记"; bad=1; }
grep -q 通知 $T/device_alert.txt && echo "通 告警文件有记录" || { echo "不通 告警文件没记录"; bad=1; }
# devlock 接入:device.lock 目录取锁前过闸——电量够直接取到;电量低等满上限退出 4、不取锁
# devlock integration: go through the gate before taking the device.lock directory lock - with enough battery the lock is taken directly; with low battery it waits out the full limit, exits 4 and does not take the lock
L=$T/device.lock; export DEVBATTERY_DIR=$T
DM_DEVBATTERY=1 DEVBATTERY_FAKE="80 0 1 0" DM_DEVLOCK_DIR=$L bash -c 'source ./devlock.sh; devlock_acquire 单测 2>/dev/null; [ -d "$DEVLOCK_DIR" ] && devlock_release' && echo "通 devlock 电量够取到锁" || { echo "不通 devlock 电量够没取到"; bad=1; }
DM_DEVBATTERY=1 DEVBATTERY_FAKE="10 0 1 0" DM_DEVBATTERY_WAIT_S=2 DM_DEVBATTERY_POLL_S=1 DM_DEVLOCK_DIR=$L bash -c 'source ./devlock.sh; devlock_acquire 单测 2>/dev/null; echo 不该走到这' >/dev/null; r=$?
[ $r = 4 ] && [ ! -d $L ] && echo "通 devlock 电量低退出 4 且没取锁" || { echo "不通 devlock 电量低 → $r"; bad=1; }
rm -rf "$T"; [ $bad = 0 ] && echo "devbattery 单测全过" || { echo "devbattery 单测有不通"; exit 1; }