Files
tsl-devkit/lsp-server/test/test_ast/test.sh
T
csh 7aa60e78f8 🐛 fix(tree_sitter): parse anonymous procedures and property accessors
accept anonymous procedure expressions, keyword-prefixed identifiers,

and read/write accessor nodes in the grammar and generated parsers.

sync ast tests, parser scripts, sample fixtures, and playbook

config with the updated parser behavior.
2026-05-24 11:07:30 +08:00

241 lines
6.9 KiB
Bash

#!/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 预设(默认 clang-ninja,可通过 BUILD_PRESET 或 --preset 覆盖)
BUILD_PRESET=${BUILD_PRESET:-clang-ninja}
IGNORES=(
"IDS_AuditExpr.tsf"
"PptxVba.tsf"
"XlsxVBA.tsf"
"$HOME/windows_share/tsf/funcext/Common/Tools/UTslDoc.tsf"
"$HOME/windows_share/tsf/funcext/Common/Tools/createTreehtml.tsf"
)
find_test_ast() {
local search_dir="$1"
if [ ! -d "$search_dir" ]; then
return 1
fi
find "$search_dir" -type f -name test_ast \( -path "*/test/test_ast" -o -path "*/test/test_ast/*" \) -print -quit 2>/dev/null
}
# 需要测试的目录列表(可以自行添加)
DIRECTORIES=(
"$REPO_ROOT/test/test_tree_sitter/test"
"$HOME/windows_share/tsf"
# "./another_directory"
)
# 是否显示详细输出(设置为1显示详细信息,0只显示摘要)
VERBOSE=0
# 是否在第一个失败时停止(设置为1则停止,0继续测试)
STOP_ON_FIRST_FAILURE=1
# 打印使用说明
print_usage() {
echo "用法: $0 [选项]"
echo "选项:"
echo " -v, --verbose 显示详细的测试输出"
echo " -c, --continue-on-error 遇到错误继续测试"
echo " --preset <name> 指定 build 预设 (默认: codex)"
echo " -h, --help 显示此帮助信息"
exit 0
}
should_ignore() {
local file="$1"
local base
base="$(basename "$file")"
for ignore in "${IGNORES[@]}"; do
if [[ "$ignore" == */* ]]; then
if [[ "$file" == "$ignore" ]]; then
return 0
fi
continue
fi
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
# 解析参数后再定位 test_ast
TEST_AST="${TEST_AST_OVERRIDE:-$(find_test_ast "$REPO_ROOT/build/$BUILD_PRESET")}"
# 检查 test_ast 是否存在
if [ -z "$TEST_AST" ] || [ ! -f "$TEST_AST" ]; then
echo -e "${RED}错误: 找不到 test_ast 可执行文件${NC}"
echo "请先在 ${REPO_ROOT}/build 下构建 test_ast,或通过 TEST_AST_OVERRIDE 指定路径"
exit 1
fi
# 检查 test_ast 是否可执行
if [ ! -x "$TEST_AST" ]; then
echo -e "${RED}错误: test_ast 不可执行${NC}"
echo "请运行: chmod +x \"$TEST_AST\""
exit 1
fi
# 统计变量
total_files=0
tested_files=0
failed_files=0
declare -a failed_file_list
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}开始测试 TSF 文件${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e "${CYAN}测试工具: $TEST_AST${NC}"
echo -e "${CYAN}Build 预设: $BUILD_PRESET${NC}"
echo -e "${CYAN}跟随软链接: 是${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
# 显示目录信息(包括软链接信息)
if [ -L "$dir" ]; then
real_path=$(readlink -f "$dir")
echo -e "${BLUE}扫描目录: $dir ${CYAN}(软链接 -> $real_path)${NC}"
else
echo -e "${BLUE}扫描目录: $dir${NC}"
fi
# 递归查找所有.tsf文件(-L 选项跟随符号链接)
while IFS= read -r -d '' file; do
if should_ignore "$file"; then
echo -e "${YELLOW}跳过忽略文件: $file${NC}"
continue
fi
((total_files++))
# 显示文件路径(包括软链接信息)
if [ -L "$file" ]; then
real_file=$(readlink -f "$file")
echo -e "${YELLOW}[测试 $total_files]${NC} $file ${CYAN}(软链接)${NC}"
else
echo -e "${YELLOW}[测试 $total_files]${NC} $file"
fi
# 运行test_ast并捕获输出
output=$("$TEST_AST" "$file" 2>&1)
exit_code=$?
# 检查是否包含 "Status: SUCCESS"
if echo "$output" | grep -q "SUCCESS"; then
((tested_files++))
echo -e "${GREEN} ✓ 通过 (Status: SUCCESS)${NC}"
# 如果是详细模式,显示输出摘要
if [ $VERBOSE -eq 1 ]; then
echo "$output" | grep -E "(File:|Size:|Statements:|Errors:|Status:)" | 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
echo ""
if [ $failed_files -eq 0 ]; then
echo -e "${GREEN}✓ 全部通过${NC}"
exit 0
else
echo -e "${RED}✗ 存在 $failed_files 个失败${NC}"
exit 1
fi