diff --git a/scripts/run_all_tests.sh b/scripts/run_all_tests.sh index 2b3ffb0..2bf4cfc 100755 --- a/scripts/run_all_tests.sh +++ b/scripts/run_all_tests.sh @@ -2,11 +2,41 @@ # 批量测试所有.sy文件的语法解析 -test_dir="/home/white/nudt-compiler-cpp/test/test_case/functional" -compiler="/home/white/nudt-compiler-cpp/build/bin/compiler" +# 获取脚本所在目录(假设脚本在项目根目录或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 "错误:编译器不存在,请先构建项目" + echo "错误:编译器不存在: $compiler" + echo "请先构建项目,或设置 COMPILER_PATH 环境变量指向编译器" + exit 1 +fi + +# 检查测试目录是否存在 +if [ ! -d "$test_dir" ]; then + echo "错误:测试目录不存在: $test_dir" + echo "请设置 TEST_DIR 环境变量指向测试用例目录" exit 1 fi @@ -14,35 +44,43 @@ success_count=0 failed_count=0 failed_tests=() +echo "编译器: $compiler" +echo "测试目录: $test_dir" +echo "" echo "开始测试所有.sy文件的语法解析..." -echo "=" +echo "========================================" # 获取所有.sy文件并排序 -for test_file in $(find "$test_dir" -name "*.sy" | sort); do - echo "测试: $(basename "$test_file")" +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 " ✓ 成功" + echo "✓ 成功" ((success_count++)) else - echo " ✗ 失败" + echo "✗ 失败" ((failed_count++)) - failed_tests+=($(basename "$test_file")) + # 保存相对路径而不是仅文件名,便于定位 + failed_tests+=("${test_file#$PROJECT_ROOT/}") fi -done +done < <(find "$test_dir" -name "*.sy" | sort) -echo "=" +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 \ No newline at end of file