#!/usr/bin/env bash # # 批量跑 examples/src/bin/ 下的全部示例,任一失败即非零退出。 # # 为什么要这个脚本:示例最大的风险不是写错,是**三个月后编译不过**。 # 把它挂进 tools/ci.sh,crate API 一变这里立刻红,比写多少文档都管用。 # # 判据有两条,缺一不可: # 1. 进程退出码为 0(`main_of` 在 Err 时返回 FAILURE); # 2. 输出里没有 `[!!]` 行 —— `check()` 只打印不中断, # 示例里的「密文 == 明文」断言失败时进程仍然退出 0。 # # 用法:tools/examples.sh 跑全部 # tools/examples.sh 03 09 只跑名字含 03 / 09 的 # set -uo pipefail cd "$(dirname "$0")/.." filters=("$@") total=0 fail=0 failed_names=() for f in examples/src/bin/*.rs; do name=$(basename "$f" .rs) if [ ${#filters[@]} -gt 0 ]; then hit=0 for pat in "${filters[@]}"; do case "$name" in *"$pat"*) hit=1 ;; esac done [ "$hit" -eq 1 ] || continue fi total=$((total + 1)) out=$(cargo run -q -p mpc-examples --bin "$name" 2>&1) rc=$? bad=$(printf '%s\n' "$out" | grep -c '\[!!\]' || true) if [ "$rc" -ne 0 ] || [ "$bad" -ne 0 ]; then fail=$((fail + 1)) failed_names+=("$name") printf ' \033[31mFAIL\033[0m %s\n' "$name" printf '%s\n' "$out" | grep -E '\[!!\]|^失败' | sed 's/^/ /' || true [ "$rc" -ne 0 ] && printf ' 退出码 %s\n' "$rc" else printf ' \033[32m ok \033[0m %s\n' "$name" fi done if [ "$total" -eq 0 ]; then echo "没有匹配的示例:${filters[*]:-}" >&2 exit 2 fi printf '\n' if [ "$fail" -ne 0 ]; then printf '\033[31m%d/%d 个示例失败:%s\033[0m\n' "$fail" "$total" "${failed_names[*]}" exit 1 fi printf '\033[32m%d 个示例全部通过\033[0m\n' "$total"