Files
smart-home-control-system/scripts/acceptance.sh
T
sambu 09bbe23697 fix(main): 补齐 SIGINT/SIGTERM 处理,网页模式可优雅退出;新增覆盖率脚本与 A8 验收
- main.c 注册信号处理(异步信号安全,仅置停止标志):Ctrl+C/kill 后主循环正常收尾,
  配置/设备表/日志落盘,gcov 数据也能写出。此前 SIGINT 走默认动作直接终止进程
  (退出码 -2),"Ctrl+C 退出"的提示与事实不符,优雅停机路径从未被执行
- tools/coverage.sh:coverage 预设 → 跑 CTest → 回放/菜单/网页三条路径 → gcov 逐文件汇总
  (本机无 gcovr/lcov;GCC14 的 gcov 期望 <名>.gcno 而 CMake 产出 <名>.c.gcno,脚本用软链兼容)
- scripts/acceptance.sh 增补 A8(优雅退出 + 三份数据落盘),验收 21 项全通过
- 覆盖率:45.6% → 63.7%(网页层从 0% 到 55~95%,menu.c 仍低,待交互测试补齐)
2026-09-14 20:43:46 +08:00

86 lines
4.9 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.
#!/usr/bin/env bash
# 验收脚本:一键跑通 A1~A8 验收项并打印结论
# 用法:bash scripts/acceptance.sh
set -uo pipefail
cd "$(dirname "$0")/.."
PASS=0; FAIL=0
ok() { printf ' [通过] %s\n' "$1"; PASS=$((PASS+1)); }
bad() { printf ' [失败] %s\n' "$1"; FAIL=$((FAIL+1)); }
echo "== A1 debug 预设构建(告警即错误) =="
if cmake --preset debug >/dev/null && cmake --build --preset debug >/tmp/shs_build.log 2>&1; then
if grep -qE 'warning:' /tmp/shs_build.log; then bad "构建出现告警(见 /tmp/shs_build.log)"; else ok "零告警构建成功"; fi
else bad "构建失败(见 /tmp/shs_build.log)"; fi
echo "== A2 release 预设构建 =="
if cmake --preset release >/dev/null && cmake --build --preset release >/tmp/shs_build_rel.log 2>&1; then ok "release 构建成功"; else bad "release 构建失败"; fi
echo "== A3 头文件接口检查 =="
if bash tools/header_check.sh >/tmp/shs_headers.log 2>&1; then ok "全部头文件可独立编译"; else bad "头文件检查失败"; fi
echo "== A4 CTest 全量测试 =="
if ctest --preset debug --output-on-failure >/tmp/shs_ctest.log 2>&1; then
ok "$(grep -cE 'Test #' /tmp/shs_ctest.log || echo 0) 个测试全部通过"
else bad "测试未全部通过(见 /tmp/shs_ctest.log)"; fi
echo "== A5 ASan/UBSan 预设 =="
if cmake --preset asan >/dev/null && cmake --build --preset asan >/tmp/shs_asan_build.log 2>&1 && ctest --preset asan >/tmp/shs_asan.log 2>&1; then
ok "ASan/UBSan 下测试通过"
else bad "ASan/UBSan 检查失败"; fi
echo "== A6 回放演示 =="
if ./build/debug/smart_home --replay scripts/demo.txt --data /tmp/shs_demo_data >/tmp/shs_demo.log 2>&1; then
ok "演示脚本执行完成(输出 /tmp/shs_demo.log)"
else bad "演示脚本执行失败"; fi
echo "== A7 网页接口冒烟(17 条路由抽检) =="
./build/debug/smart_home --web --port 18080 --data /tmp/shs_web_data >/tmp/shs_web.log 2>&1 &
WEB_PID=$!; sleep 1.5
WEB=http://127.0.0.1:18080
code() { curl -s -o /tmp/shs_body.txt -w '%{http_code}' --noproxy '*' "$1"; }
size() { curl -s -o /tmp/shs_body.txt -w '%{size_download}' --noproxy '*' "$1"; }
# 静态三件套
if [ "$(code $WEB/)" = "200" ] && grep -q "<!DOCTYPE html>" /tmp/shs_body.txt; then ok "首页 HTML 可访问"; else bad "首页不可访问"; fi
if [ "$(size $WEB/app.js)" -gt 3000 ]; then ok "内嵌脚本 /app.js 完整下发(>3KB,分段字面量拼装生效)"; else bad "/app.js 体积异常"; fi
if [ "$(size $WEB/style.css)" -gt 1000 ]; then ok "内嵌样式 /style.css 完整下发"; else bad "/style.css 体积异常"; fi
# 只读接口
if curl -s --noproxy '*' $WEB/api/devices | grep -q '"客厅主灯"' && curl -s --noproxy '*' $WEB/api/devices | grep -q '"count":8'; then ok "GET /api/devices 返回 8 台设备"; else bad "/api/devices 数据异常"; fi
if curl -s --noproxy '*' $WEB/api/sensors | grep -q '"valid":true'; then ok "GET /api/sensors 采样有效(HAL I2C 字节序正确)"; else bad "/api/sensors 采样无效"; fi
for path in /api/log /api/report /api/config /api/selftest; do
if [ "$(code $WEB$path)" = "200" ]; then ok "GET $path 返回 200"; else bad "GET $path 异常"; fi
done
# 写接口
if curl -s --noproxy '*' -X POST -H 'Content-Type: application/json' -d '{"id":1,"on":true}' $WEB/api/device/power | grep -q '"power":true'; then ok "POST /api/device/power 生效"; else bad "电源控制接口异常"; fi
if curl -s --noproxy '*' -X POST -H 'Content-Type: application/json' -d '{"dev":1,"cmd":2,"value":1}' $WEB/api/frame | grep -q '"crc_ok":true'; then ok "POST /api/frame 帧 CRC 校验通过"; else bad "协议帧接口异常"; fi
# 负例:离线设备拒绝开机、未知路径 404
if curl -s --noproxy '*' -X POST -H 'Content-Type: application/json' -d '{"id":8,"on":true}' $WEB/api/device/power | grep -q '"ok":false'; then ok "负例:离线设备开机被拒(返回 ok:false)"; else bad "离线设备负例未生效"; fi
if [ "$(code $WEB/api/nonexistent)" = "404" ]; then ok "负例:未知路径返回 404"; else bad "404 兜底异常"; fi
kill $WEB_PID 2>/dev/null || true
wait $WEB_PID 2>/dev/null || true
echo "== A8 优雅退出与数据落盘 =="
A8DIR=$(mktemp -d)
./build/debug/smart_home --web --port 18585 --data "$A8DIR/data" >/tmp/shs_a8.log 2>&1 &
A8PID=$!; sleep 1.5
curl -s -m 3 --noproxy '*' -o /dev/null "http://127.0.0.1:18585/api/devices"
kill -INT "$A8PID" 2>/dev/null
for _ in 1 2 3 4 5 6 7 8 9 10; do kill -0 "$A8PID" 2>/dev/null || break; sleep 0.2; done
wait "$A8PID" 2>/dev/null
A8RC=$?
if [ "$A8RC" -eq 0 ]; then ok "网页模式 SIGINT 优雅退出(退出码 0)"; else bad "优雅退出失败(退出码 $A8RC)"; fi
if [ -f "$A8DIR/data/devices.dat" ] && [ -f "$A8DIR/data/system.log" ] && [ -f "$A8DIR/data/smart_home.ini" ]; then
ok "退出时设备表 / 日志 / 配置均已落盘"
else
bad "退出未正确落盘($A8DIR/data)"
fi
rm -rf "$A8DIR"
echo
echo "验收汇总:通过 $PASS 项,失败 $FAIL 项"
[ "$FAIL" -eq 0 ]