Files
mask-ddpm/example/test_windows_compatibility.py
2026-01-22 17:39:31 +08:00

140 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""测试Windows兼容性修改"""
import os
import sys
from pathlib import Path
def test_path_operations():
"""测试路径操作"""
print("=== 测试路径操作 ===")
# 测试Path对象
base_dir = Path(__file__).resolve().parent
print(f"Base directory: {base_dir}")
print(f"Base directory exists: {base_dir.exists()}")
# 测试路径拼接
test_path = base_dir / "test_file.txt"
print(f"Test path: {test_path}")
print(f"Test path is absolute: {test_path.is_absolute()}")
# 测试路径解析
print(f"Parent: {test_path.parent}")
print(f"Name: {test_path.name}")
return True
def test_file_operations():
"""测试文件操作"""
print("\n=== 测试文件操作 ===")
base_dir = Path(__file__).resolve().parent
test_file = base_dir / "test_windows_compat.txt"
try:
# 测试写入UTF-8编码
with open(test_file, "w", encoding="utf-8") as f:
f.write("测试Windows兼容性\n")
f.write("Test Windows compatibility\n")
f.write("中文字符测试\n")
print(f"文件写入成功: {test_file}")
# 测试读取
with open(test_file, "r", encoding="utf-8") as f:
content = f.read()
print(f"文件读取成功,内容长度: {len(content)}")
# 清理
test_file.unlink()
print("测试文件已清理")
return True
except Exception as e:
print(f"文件操作失败: {e}")
return False
def test_imports():
"""测试导入"""
print("\n=== 测试导入 ===")
modules_to_test = [
"json",
"csv",
"gzip",
"os",
"sys",
"pathlib",
"argparse",
"subprocess",
]
all_success = True
for module_name in modules_to_test:
try:
__import__(module_name)
print(f"{module_name} 导入成功")
except ImportError as e:
print(f"{module_name} 导入失败: {e}")
all_success = False
return all_success
def test_path_resolution():
"""测试路径解析"""
print("\n=== 测试路径解析 ===")
# 测试相对路径解析
rel_path = Path("./test/../example")
abs_path = rel_path.resolve()
print(f"相对路径: {rel_path}")
print(f"解析后绝对路径: {abs_path}")
# 测试路径拼接
path1 = Path("C:/Users/test")
path2 = Path("documents/file.txt")
combined = path1 / path2
print(f"路径拼接: {combined}")
return True
def main():
print("开始Windows兼容性测试...")
print(f"Python版本: {sys.version}")
print(f"操作系统: {os.name}")
print(f"当前目录: {os.getcwd()}")
tests = [
("路径操作", test_path_operations),
("文件操作", test_file_operations),
("模块导入", test_imports),
("路径解析", test_path_resolution),
]
results = []
for test_name, test_func in tests:
try:
success = test_func()
results.append((test_name, success))
except Exception as e:
print(f"{test_name} 测试异常: {e}")
results.append((test_name, False))
print("\n=== 测试结果汇总 ===")
all_passed = True
for test_name, success in results:
status = "✓ 通过" if success else "✗ 失败"
print(f"{test_name}: {status}")
if not success:
all_passed = False
if all_passed:
print("\n所有测试通过代码应该可以在Windows上正常运行。")
else:
print("\n部分测试失败,需要进一步检查。")
return all_passed
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)