poc/project/llmclipboard/test_package.py
2025-03-02 18:56:33 +08:00

158 lines
3.9 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
LLMClipboard 打包测试脚本
用于测试打包后的应用程序是否能够正确运行
"""
import os
import sys
import subprocess
import time
import shutil
def print_header(title):
"""打印标题"""
print("\n" + "=" * 60)
print(f" {title} ".center(60, "="))
print("=" * 60)
def run_command(command, cwd=None):
"""运行命令并返回输出"""
print(f"运行命令: {command}")
try:
result = subprocess.run(
command,
shell=True,
cwd=cwd,
capture_output=True,
text=True,
encoding='utf-8'
)
return result.returncode, result.stdout, result.stderr
except Exception as e:
return -1, "", str(e)
def test_build():
"""测试构建过程"""
print_header("测试构建过程")
# 运行build.bat
print("运行build.bat...")
code, stdout, stderr = run_command("build.bat")
if code != 0:
print("构建失败!")
print(f"错误信息: {stderr}")
return False
# 检查dist目录
if not os.path.exists("dist"):
print("构建失败: dist目录不存在")
return False
# 检查可执行文件
if not os.path.exists("dist/LLMClipboard.exe"):
print("构建失败: LLMClipboard.exe不存在")
return False
print("构建成功!")
return True
def test_resources():
"""测试资源文件"""
print_header("测试资源文件")
# 检查resources目录
if not os.path.exists("dist/resources"):
print("资源目录不存在: dist/resources")
return False
# 检查图标文件
if not os.path.exists("dist/resources/icon.png"):
print("图标文件不存在: dist/resources/icon.png")
return False
print("资源文件检查通过!")
return True
def test_executable():
"""测试可执行文件"""
print_header("测试可执行文件")
# 创建测试目录
test_dir = "test_dist"
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
os.makedirs(test_dir)
# 复制可执行文件和资源
print("复制可执行文件和资源到测试目录...")
shutil.copy("dist/LLMClipboard.exe", test_dir)
shutil.copytree("dist/resources", os.path.join(test_dir, "resources"))
# 运行可执行文件
print("运行可执行文件...")
print("注意: 将在5秒后自动关闭应用程序")
# 启动应用程序
process = subprocess.Popen(
os.path.join(test_dir, "LLMClipboard.exe"),
cwd=test_dir
)
# 等待5秒
time.sleep(5)
# 检查进程是否仍在运行
if process.poll() is None:
print("应用程序成功启动并保持运行")
# 终止进程
process.terminate()
process.wait(timeout=5)
result = True
else:
print("应用程序启动失败或已崩溃")
result = False
# 检查是否生成了错误日志
error_log = os.path.join(test_dir, "error_log.txt")
if os.path.exists(error_log):
print(f"发现错误日志: {error_log}")
with open(error_log, 'r', encoding='utf-8') as f:
print(f.read())
result = False
# 清理测试目录
print("清理测试目录...")
shutil.rmtree(test_dir)
return result
def main():
"""主函数"""
print_header("LLMClipboard 打包测试")
# 测试构建
if not test_build():
print("构建测试失败,终止测试")
return 1
# 测试资源文件
if not test_resources():
print("资源文件测试失败,终止测试")
return 1
# 测试可执行文件
if not test_executable():
print("可执行文件测试失败")
return 1
print_header("测试结果")
print("所有测试通过!")
return 0
if __name__ == "__main__":
sys.exit(main())