You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/bin/bash
# 批量测试所有.sy文件的语法解析
# 获取脚本所在目录假设脚本在项目根目录或scripts目录下
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 尝试定位项目根目录
# 情况1: 脚本在项目根目录
if [ -f "$SCRIPT_DIR/build/bin/compiler" ]; then
PROJECT_ROOT="$SCRIPT_DIR"
# 情况2: 脚本在项目根目录下的 scripts/ 目录
elif [ -f "$SCRIPT_DIR/../build/bin/compiler" ]; then
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
# 情况3: 使用环境变量(如果设置了)
elif [ -n "$COMPILER_PROJECT_ROOT" ]; then
PROJECT_ROOT="$COMPILER_PROJECT_ROOT"
else
echo "错误:无法定位项目根目录"
echo "请将脚本放在项目根目录或 scripts/ 目录下,"
echo "或设置环境变量 COMPILER_PROJECT_ROOT"
exit 1
fi
# 设置默认路径,支持通过环境变量覆盖
test_dir="${TEST_DIR:-$PROJECT_ROOT/test/test_case/functional}"
compiler="${COMPILER_PATH:-$PROJECT_ROOT/build/bin/compiler}"
# 检查编译器是否存在
if [ ! -f "$compiler" ]; then
echo "错误:编译器不存在: $compiler"
echo "请先构建项目,或设置 COMPILER_PATH 环境变量指向编译器"
exit 1
fi
# 检查测试目录是否存在
if [ ! -d "$test_dir" ]; then
echo "错误:测试目录不存在: $test_dir"
echo "请设置 TEST_DIR 环境变量指向测试用例目录"
exit 1
fi
success_count=0
failed_count=0
failed_tests=()
echo "编译器: $compiler"
echo "测试目录: $test_dir"
echo ""
echo "开始测试所有.sy文件的语法解析..."
echo "========================================"
# 获取所有.sy文件并排序
while IFS= read -r test_file; do
echo -n "测试: $(basename "$test_file") ... "
# 运行解析测试,将输出重定向到/dev/null
"$compiler" --emit-parse-tree "$test_file" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "✓ 成功"
((success_count++))
else
echo "✗ 失败"
((failed_count++))
# 保存相对路径而不是仅文件名,便于定位
failed_tests+=("${test_file#$PROJECT_ROOT/}")
fi
done < <(find "$test_dir" -name "*.sy" | sort)
echo "========================================"
echo "测试完成!"
echo "总测试数: $((success_count + failed_count))"
echo "成功: $success_count"
echo "失败: $failed_count"
if [ $failed_count -gt 0 ]; then
echo ""
echo "失败的测试用例:"
for test in "${failed_tests[@]}"; do
echo " - $test"
done
exit 1
fi
exit 0