261 lines
7.4 KiB
Python
261 lines
7.4 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
脚本稳定性验证工具
|
||
检查脚本的各项功能是否正常
|
||
"""
|
||
|
||
import sys
|
||
import os
|
||
import importlib
|
||
import subprocess
|
||
import time
|
||
|
||
def check_dependencies():
|
||
"""检查依赖库是否安装"""
|
||
print("=== 检查依赖库 ===")
|
||
dependencies = ['pyautogui', 'pygetwindow']
|
||
missing = []
|
||
|
||
for dep in dependencies:
|
||
try:
|
||
importlib.import_module(dep)
|
||
print(f"✓ {dep}")
|
||
except ImportError:
|
||
print(f"✗ {dep} 未安装")
|
||
missing.append(dep)
|
||
|
||
if missing:
|
||
print(f"\n缺少依赖库,请运行: pip install {' '.join(missing)}")
|
||
return False
|
||
return True
|
||
|
||
def check_python_version():
|
||
"""检查Python版本"""
|
||
print("\n=== 检查Python版本 ===")
|
||
version = sys.version_info
|
||
print(f"Python版本: {sys.version.split()[0]}")
|
||
|
||
if version.major == 3 and version.minor >= 6:
|
||
print("✓ Python版本符合要求 (3.6+)")
|
||
return True
|
||
else:
|
||
print("✗ 需要Python 3.6或更高版本")
|
||
return False
|
||
|
||
def check_file_structure():
|
||
"""检查文件结构"""
|
||
print("\n=== 检查文件结构 ===")
|
||
required_files = ['main.py', 'requirements.txt', 'README.md']
|
||
missing = []
|
||
|
||
for file in required_files:
|
||
if os.path.exists(file):
|
||
print(f"✓ {file}")
|
||
else:
|
||
print(f"✗ {file} 不存在")
|
||
missing.append(file)
|
||
|
||
if missing:
|
||
print(f"\n缺少文件: {', '.join(missing)}")
|
||
return False
|
||
return True
|
||
|
||
def check_syntax():
|
||
"""检查语法错误"""
|
||
print("\n=== 检查语法 ===")
|
||
try:
|
||
with open('main.py', 'r', encoding='utf-8') as f:
|
||
source = f.read()
|
||
compile(source, 'main.py', 'exec')
|
||
print("✓ main.py 语法正确")
|
||
return True
|
||
except SyntaxError as e:
|
||
print(f"✗ 语法错误: {e}")
|
||
return False
|
||
|
||
def check_imports():
|
||
"""检查导入"""
|
||
print("\n=== 检查导入 ===")
|
||
try:
|
||
# 尝试导入主模块
|
||
import main
|
||
print("✓ 可以导入main模块")
|
||
|
||
# 检查类是否存在
|
||
from main import FishingBot
|
||
print("✓ FishingBot类存在")
|
||
|
||
# 清理
|
||
del main
|
||
return True
|
||
except Exception as e:
|
||
print(f"✗ 导入错误: {e}")
|
||
return False
|
||
|
||
def run_simulation():
|
||
"""运行模拟测试(不实际操作窗口)"""
|
||
print("\n=== 运行模拟测试 ===")
|
||
|
||
try:
|
||
# 创建模拟的FishingBot,但替换实际操作方法
|
||
from main import FishingBot
|
||
|
||
class MockFishingBot(FishingBot):
|
||
def __init__(self, window_title="测试窗口", interval=1.0):
|
||
super().__init__(window_title, interval)
|
||
self.actions = []
|
||
|
||
def find_window(self):
|
||
self.actions.append("find_window")
|
||
print(" 模拟: 查找窗口")
|
||
return type('MockWindow', (), {'title': '测试窗口', 'activate': lambda: None})()
|
||
|
||
def activate_window(self, window):
|
||
self.actions.append("activate_window")
|
||
print(" 模拟: 激活窗口")
|
||
return True
|
||
|
||
def send_fishing_message(self):
|
||
self.actions.append("send_fishing_message")
|
||
print(" 模拟: 发送钓鱼消息")
|
||
return True
|
||
|
||
def send_reel_message(self):
|
||
self.actions.append("send_reel_message")
|
||
print(" 模拟: 发送收杆消息")
|
||
return True
|
||
|
||
bot = MockFishingBot("测试窗口", 0.1)
|
||
|
||
# 运行一个周期
|
||
print(" 运行一个周期...")
|
||
success = bot.run_cycle()
|
||
|
||
if success:
|
||
print("✓ 模拟周期成功完成")
|
||
print(f" 执行的操作: {', '.join(bot.actions)}")
|
||
return True
|
||
else:
|
||
print("✗ 模拟周期失败")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"✗ 模拟测试错误: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return False
|
||
|
||
def check_logging():
|
||
"""检查日志配置"""
|
||
print("\n=== 检查日志配置 ===")
|
||
try:
|
||
import logging
|
||
from main import logger
|
||
|
||
# 检查logger是否配置
|
||
if logger.handlers:
|
||
print("✓ 日志处理器已配置")
|
||
|
||
# 检查是否可以记录日志
|
||
logger.info("测试日志消息")
|
||
print("✓ 可以记录日志")
|
||
|
||
# 检查日志文件
|
||
if os.path.exists('fishing_bot.log'):
|
||
print("✓ 日志文件存在")
|
||
else:
|
||
print("⚠ 日志文件不存在(首次运行后会创建)")
|
||
|
||
return True
|
||
else:
|
||
print("✗ 日志处理器未配置")
|
||
return False
|
||
except Exception as e:
|
||
print(f"✗ 日志检查错误: {e}")
|
||
return False
|
||
|
||
def run_command_test():
|
||
"""运行命令行测试"""
|
||
print("\n=== 运行命令行测试 ===")
|
||
|
||
tests = [
|
||
("python main.py --help", "显示帮助信息"),
|
||
("python main.py --test", "测试模式"),
|
||
]
|
||
|
||
all_passed = True
|
||
|
||
for cmd, description in tests:
|
||
print(f" 测试: {description}")
|
||
try:
|
||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=10)
|
||
if result.returncode == 0:
|
||
print(f" ✓ 命令执行成功: {cmd}")
|
||
else:
|
||
print(f" ✗ 命令执行失败: {cmd}")
|
||
print(f" 错误: {result.stderr[:200]}")
|
||
all_passed = False
|
||
except subprocess.TimeoutExpired:
|
||
print(f" ✗ 命令超时: {cmd}")
|
||
all_passed = False
|
||
except Exception as e:
|
||
print(f" ✗ 命令异常: {e}")
|
||
all_passed = False
|
||
|
||
return all_passed
|
||
|
||
def main():
|
||
"""主验证函数"""
|
||
print("开始验证脚本稳定性...\n")
|
||
|
||
checks = [
|
||
("Python版本", check_python_version),
|
||
("文件结构", check_file_structure),
|
||
("依赖库", check_dependencies),
|
||
("语法", check_syntax),
|
||
("导入", check_imports),
|
||
("日志配置", check_logging),
|
||
("模拟测试", run_simulation),
|
||
("命令行测试", run_command_test),
|
||
]
|
||
|
||
results = []
|
||
|
||
for name, check_func in checks:
|
||
try:
|
||
success = check_func()
|
||
results.append((name, success))
|
||
except Exception as e:
|
||
print(f"检查 '{name}' 时出错: {e}")
|
||
results.append((name, False))
|
||
|
||
# 汇总结果
|
||
print("\n" + "="*50)
|
||
print("验证结果汇总:")
|
||
print("="*50)
|
||
|
||
passed = 0
|
||
total = len(results)
|
||
|
||
for name, success in results:
|
||
status = "✓ 通过" if success else "✗ 失败"
|
||
print(f"{name:20} {status}")
|
||
if success:
|
||
passed += 1
|
||
|
||
print("="*50)
|
||
print(f"总计: {passed}/{total} 项检查通过")
|
||
|
||
if passed == total:
|
||
print("\n✅ 所有检查通过!脚本稳定性良好。")
|
||
print("建议进一步测试:")
|
||
print("1. 在实际窗口中运行测试模式: python main.py --test")
|
||
print("2. 运行短期循环: python main.py --cycles 5 --interval 2")
|
||
print("3. 检查日志文件: fishing_bot.log")
|
||
return 0
|
||
else:
|
||
print(f"\n⚠ {total - passed} 项检查未通过,请根据上述输出修复问题。")
|
||
return 1
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main()) |