🐛 fix(tests): report missing toml parser clearly

This commit is contained in:
csh 2026-05-19 10:30:02 +08:00
parent c0729c7933
commit 55cda3bd42
1 changed files with 18 additions and 8 deletions

View File

@ -53,17 +53,21 @@ validate_toml_syntax() {
if python3 << EOF if python3 << EOF
import sys import sys
try: parser = None
import tomli for module_name in ("tomli", "tomllib", "toml"):
except ImportError:
try: try:
import tomllib as tomli parser = __import__(module_name)
break
except ImportError: except ImportError:
import toml as tomli continue
if parser is None:
print("缺少 TOML 解析器(需要 tomli、tomllib 或 toml", file=sys.stderr)
sys.exit(2)
try: try:
with open("$file", "rb") as f: with open("$file", "rb") as f:
tomli.load(f) parser.load(f)
sys.exit(0) sys.exit(0)
except Exception as e: except Exception as e:
print(f"TOML 语法错误: {e}", file=sys.stderr) print(f"TOML 语法错误: {e}", file=sys.stderr)
@ -74,8 +78,14 @@ EOF
VALIDATION_PASSED=$((VALIDATION_PASSED + 1)) VALIDATION_PASSED=$((VALIDATION_PASSED + 1))
return 0 return 0
else else
echo "$description: TOML 语法错误" status=$?
echo "TOML 语法错误: $file" >> "$ERRORS_FILE" 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)) VALIDATION_FAILED=$((VALIDATION_FAILED + 1))
return 1 return 1
fi fi