test_config_loading.py 1.75 KB
#!/usr/bin/env python3
"""
测试配置文件加载逻辑
"""
import json
import sys
import tempfile
from pathlib import Path

# 模拟PyInstaller环境
sys.frozen = True
sys._MEIPASS = "C:\\Users\\Shady\\PycharmProjects\\GoogleNanoBananaApp"  # 模拟路径

# 导入配置加载逻辑
def test_config_loading():
    """测试配置文件加载的多种路径"""

    # 模拟exe所在目录的config.json
    exe_dir_config = Path(sys.executable).parent / 'config.json' if hasattr(sys, 'executable') else None

    # 模拟_MEIPASS目录的config.json
    meipass_config = Path(sys._MEIPASS) / 'config.json'

    print(f"模拟PyInstaller环境:")
    print(f"  sys.frozen: {getattr(sys, 'frozen', False)}")
    print(f"  sys._MEIPASS: {sys._MEIPASS}")

    # 测试各个路径
    test_paths = []
    if hasattr(sys, 'executable'):
        test_paths.append(("exe目录", Path(sys.executable).parent / 'config.json'))

    test_paths.append(("_MEIPASS目录", Path(sys._MEIPASS) / 'config.json'))
    test_paths.append(("当前目录", Path('.') / 'config.json'))

    for name, path in test_paths:
        if path.exists():
            print(f"[OK] {name}: {path} - 存在")
            try:
                with open(path, 'r', encoding='utf-8') as f:
                    config = json.load(f)
                    db_config = config.get("db_config")
                    if db_config:
                        print(f"  [OK] 包含数据库配置: {db_config.get('host', 'N/A')}")
                    else:
                        print(f"  [FAIL] 未包含数据库配置")
            except Exception as e:
                print(f"  [FAIL] 读取失败: {e}")
        else:
            print(f"[FAIL] {name}: {path} - 不存在")

if __name__ == "__main__":
    test_config_loading()