#!/bin/bash # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color # 路径与默认配置 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" BUILD_PRESET=${BUILD_PRESET:-clang} IGNORES=("IDS_AuditExpr.tsf") find_test_semantic() { local search_dir="$1" if [ ! -d "$search_dir" ]; then return 1 fi find "$search_dir" -type f -name test_semantic -path "*/test/test_semantic/*" -print -quit 2>/dev/null } TEST_SEMANTIC="${TEST_SEMANTIC_OVERRIDE:-$(find_test_semantic "$REPO_ROOT/build/$BUILD_PRESET")}" # 默认扫描目录(可自行添加或用参数覆盖) DIRECTORIES=( "$REPO_ROOT/test/test_tree_sitter/test" ) VERBOSE=0 STOP_ON_FIRST_FAILURE=1 print_usage() { echo "用法: $0 [选项]" echo "选项:" echo " -v, --verbose 显示详细输出" echo " -c, --continue-on-error 遇到错误继续测试" echo " --preset 指定 build 预设 (默认: ${BUILD_PRESET})" echo " -h, --help 显示此帮助信息" exit 0 } should_ignore() { local file="$1" local base base="$(basename "$file")" for ignore in "${IGNORES[@]}"; do if [[ "$base" == "$ignore" ]]; then return 0 fi done return 1 } # 解析命令行参数 while [[ $# -gt 0 ]]; do case $1 in -v|--verbose) VERBOSE=1 shift ;; -c|--continue-on-error) STOP_ON_FIRST_FAILURE=0 shift ;; --preset) BUILD_PRESET="$2" shift 2 ;; -h|--help) print_usage ;; *) echo -e "${RED}未知选项: $1${NC}" print_usage ;; esac done # 检查可执行文件 if [ -z "$TEST_SEMANTIC" ] || [ ! -f "$TEST_SEMANTIC" ]; then echo -e "${RED}错误: 找不到 test_semantic 可执行文件${NC}" echo "请先在 ${REPO_ROOT}/build 下构建 test_semantic,或通过 TEST_SEMANTIC_OVERRIDE 指定路径" exit 1 fi if [ ! -x "$TEST_SEMANTIC" ]; then echo -e "${RED}错误: test_semantic 不可执行${NC}" echo "请运行: chmod +x \"$TEST_SEMANTIC\"" exit 1 fi total_files=0 tested_files=0 failed_files=0 declare -a failed_file_list echo -e "${BLUE}========================================${NC}" echo -e "${BLUE}开始运行语义分析测试${NC}" echo -e "${BLUE}========================================${NC}" echo -e "${CYAN}测试工具: $TEST_SEMANTIC${NC}" echo -e "${CYAN}Build 预设: $BUILD_PRESET${NC}" echo -e "${CYAN}忽略文件: ${IGNORES[*]}${NC}" if [ $VERBOSE -eq 1 ]; then echo -e "${CYAN}详细模式: 开启${NC}" fi if [ $STOP_ON_FIRST_FAILURE -eq 0 ]; then echo -e "${CYAN}失败后继续: 是${NC}" fi echo "" for dir in "${DIRECTORIES[@]}"; do if [ ! -e "$dir" ]; then echo -e "${YELLOW}警告: 目录不存在: $dir${NC}" continue fi if [ ! -d "$dir" ]; then echo -e "${YELLOW}警告: 不是目录: $dir${NC}" continue fi echo -e "${BLUE}扫描目录: $dir${NC}" while IFS= read -r -d '' file; do if should_ignore "$file"; then echo -e "${YELLOW}跳过忽略文件: $file${NC}" continue fi ((total_files++)) echo -e "${YELLOW}[测试 $total_files]${NC} $file" workspace="$(dirname "$file")" output=$("$TEST_SEMANTIC" "$workspace" "$file" 2>&1) exit_code=$? # 解析失败文件列表,忽略白名单中的失败 mapfile -t failed_loads < <(echo "$output" | grep -o 'Failed to load "[^"]*"' | sed 's/Failed to load "//; s/"$//') filtered_failures=() for f in "${failed_loads[@]}"; do if ! should_ignore "$f"; then filtered_failures+=("$f") fi done if [[ $exit_code -eq 0 && ${#filtered_failures[@]} -eq 0 ]]; then ((tested_files++)) echo -e "${GREEN} ✓ 通过${NC}" if [ $VERBOSE -eq 1 ]; then echo "$output" | sed 's/^/ /' fi else ((failed_files++)) failed_file_list+=("$file") echo -e "${RED} ✗ 失败${NC}" echo -e "${RED}========================================${NC}" echo -e "${RED}测试失败: $file${NC}" echo -e "${RED}退出码: $exit_code${NC}" echo -e "${RED}========================================${NC}" echo -e "${YELLOW}测试输出:${NC}" echo "$output" | sed 's/^/ /' echo -e "${RED}========================================${NC}" if [ $STOP_ON_FIRST_FAILURE -eq 1 ]; then echo -e "${RED}因为测试失败而停止${NC}" exit 1 fi fi echo "" done < <(find -L "$dir" -type f -name "*.tsf" -print0 2>/dev/null | sort -z) done echo -e "${BLUE}========================================${NC}" echo -e "${GREEN}所有测试完成!${NC}" echo -e "${BLUE}========================================${NC}" echo -e "总文件数: ${CYAN}$total_files${NC}" echo -e "已通过: ${GREEN}$tested_files${NC}" echo -e "失败: ${RED}$failed_files${NC}" if [ $failed_files -gt 0 ]; then echo "" echo -e "${RED}失败的文件列表:${NC}" for failed_file in "${failed_file_list[@]}"; do echo -e " ${RED}✗${NC} $failed_file" done fi