diff --git a/tests/templates/validate_python_templates.sh b/tests/templates/validate_python_templates.sh index f97a7d1b..87cd9d50 100644 --- a/tests/templates/validate_python_templates.sh +++ b/tests/templates/validate_python_templates.sh @@ -53,17 +53,21 @@ validate_toml_syntax() { if python3 << EOF import sys -try: - import tomli -except ImportError: +parser = None +for module_name in ("tomli", "tomllib", "toml"): try: - import tomllib as tomli + parser = __import__(module_name) + break except ImportError: - import toml as tomli + continue + +if parser is None: + print("缺少 TOML 解析器(需要 tomli、tomllib 或 toml)", file=sys.stderr) + sys.exit(2) try: with open("$file", "rb") as f: - tomli.load(f) + parser.load(f) sys.exit(0) except Exception as e: print(f"TOML 语法错误: {e}", file=sys.stderr) @@ -74,8 +78,14 @@ EOF VALIDATION_PASSED=$((VALIDATION_PASSED + 1)) return 0 else - echo " ❌ $description: TOML 语法错误" - echo "TOML 语法错误: $file" >> "$ERRORS_FILE" + status=$? + if [ "$status" -eq 2 ]; then + echo " ❌ $description: 缺少 TOML 解析器" + echo "缺少 TOML 解析器: $file" >> "$ERRORS_FILE" + else + echo " ❌ $description: TOML 语法错误" + echo "TOML 语法错误: $file" >> "$ERRORS_FILE" + fi VALIDATION_FAILED=$((VALIDATION_FAILED + 1)) return 1 fi