Files
wenzi/scripts/check_e2e_consistency.sh

146 lines
4.4 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
set -euo pipefail
PROJECT_DIR="/home/long/project/蚊子"
STATE_DIR="$PROJECT_DIR/logs/e2e-automation"
OUT_FILE="${1:-$STATE_DIR/consistency_latest.md}"
status="PASS"
reason=()
# Helper function to check if a run log is complete (has "runner end")
is_run_complete() {
local run_log="$1"
if [ ! -s "$run_log" ]; then
return 1
fi
# Check for "runner end" marker indicating completion
grep -q "runner end" "$run_log" 2>/dev/null
}
# Helper function to extract timestamp from run log filename
get_run_timestamp() {
local run_log="$1"
# Format: run_YYYYMMDD_HHMMSS.log -> YYYYMMDD_HHMMSS
basename "$run_log" | sed 's/run_//' | sed 's/\.log$//'
}
# Find the latest COMPLETED run log and its corresponding report
# A run is "completed" if it has "runner end" marker
latest_run=""
latest_report=""
latest_ts=""
# List all run logs sorted by modification time (newest first)
while IFS= read -r run_log; do
if is_run_complete "$run_log"; then
latest_run="$run_log"
latest_ts=$(get_run_timestamp "$run_log")
# Try to find matching report by same timestamp
potential_report="$STATE_DIR/report_${latest_ts}.md"
if [ -s "$potential_report" ]; then
latest_report="$potential_report"
else
# Fallback: find any report newer than this run's start
latest_report="$(ls -1t "$STATE_DIR"/report_*.md 2>/dev/null | head -n1 || true)"
fi
break
fi
done < <(ls -1t "$STATE_DIR"/run_*.log 2>/dev/null || true)
# Fallback if no completed run found (use latest files but warn)
if [ -z "$latest_run" ]; then
latest_run="$(ls -1t "$STATE_DIR"/run_*.log 2>/dev/null | head -n1 || true)"
latest_report="$(ls -1t "$STATE_DIR"/report_*.md 2>/dev/null | head -n1 || true)"
if [ -n "$latest_run" ]; then
status="FAIL"
reason+=("无已完成轮次无runner end标记使用最新日志但结果可能不稳定")
fi
fi
if [ -z "$latest_report" ] || [ ! -s "$latest_report" ]; then
status="FAIL"
reason+=("报告缺失或为空")
fi
if [ -z "$latest_run" ] || [ ! -s "$latest_run" ]; then
status="FAIL"
reason+=("runner日志缺失或为空")
fi
# Enhanced regex patterns to handle various report formats:
# - 是否"全部通过": **是**
# - 是否"全部通过": **是Playwright测试/ 部分阻塞Cypress**
# - 是否"全部通过":是
# - 全部通过(是)
# - Playwright E2E测试全部通过 ✓
report_pass="UNKNOWN"
if [ -n "$latest_report" ] && [ -s "$latest_report" ]; then
# Pattern 1: "全部通过" followed by "是" (within same line context)
# Handles: 是否"全部通过": **是**, 是否"全部通过": **是(...**, 全部通过(是), etc.
if grep -Eq '是否"全部通过".*是|全部通过\s*\(是\)|全部通过.*✓' "$latest_report"; then
report_pass="YES"
# Pattern 2: "全部通过" followed by "否"
elif grep -Eq '是否"全部通过".*否|全部通过\s*\(否\)|全部失败' "$latest_report"; then
report_pass="NO"
fi
fi
runner_error="UNKNOWN"
data_contract_ok="UNKNOWN"
if [ -n "$latest_run" ] && [ -s "$latest_run" ]; then
if grep -Eqi 'run finished but not fully passed|error:|runner appears stuck|\[watchdog\].*stuck|\bException\b|\bTraceback\b|\[DATA-CONTRACT\] FAIL' "$latest_run"; then
runner_error="YES"
else
runner_error="NO"
fi
if grep -Eq '\[DATA-CONTRACT\] PASS' "$latest_run"; then
data_contract_ok="YES"
else
data_contract_ok="NO"
fi
fi
if [ "$report_pass" = "YES" ] && [ "$runner_error" = "YES" ]; then
status="FAIL"
reason+=("报告声明通过但runner日志包含失败/异常信号")
fi
if [ "$report_pass" = "UNKNOWN" ]; then
status="FAIL"
reason+=("报告未给出明确通过结论(是/否)")
fi
if [ "$data_contract_ok" != "YES" ]; then
status="FAIL"
reason+=("缺少DATA-CONTRACT通过证据结果可能为假绿")
fi
mkdir -p "$(dirname "$OUT_FILE")"
{
echo "# E2E Consistency Check"
echo
echo "- Status: $status"
echo "- Report: ${latest_report:-N/A}"
echo "- Runner Log: ${latest_run:-N/A}"
echo "- Report Pass Flag: $report_pass"
echo "- Runner Error Signal: $runner_error"
echo "- Data Contract Signal: $data_contract_ok"
echo
echo "## Reasons"
if [ ${#reason[@]} -eq 0 ]; then
echo "- 一致性检查通过"
else
for r in "${reason[@]}"; do
echo "- $r"
done
fi
} > "$OUT_FILE"
if [ "$status" = "PASS" ]; then
exit 0
else
exit 2
fi