解决测试运行时的相对导入错误,确保测试能够正常执行。 主要修复: 1. 节点模块导入处理: - 为所有节点文件添加导入异常处理 - 在测试环境中提供fallback导入机制 - 定义最小化的fallback类和函数 2. 测试文件路径修复: - 修正测试文件中的sys.path设置 - 将'py'路径改为'../py'以正确定位模块 - 修复test_new_dci_nodes.py中的类名错误 3. 模块兼容性改进: - preview_node.py: 添加完整的导入fallback - image_node.py: 处理utils模块导入 - deb_packager_node.py: 处理file_utils导入 - deb_loader_node.py: 处理相对导入问题 - sample_image_node.py: 添加导入异常处理 - __init__.py: 添加整体导入保护 4. 测试结果改进: - 从7个ERROR减少到0个ERROR - 从15个skipped减少到14个skipped - 只剩1个FAIL(测试逻辑问题,非导入问题) 这些修复确保了测试套件能够在独立环境中正常运行,为CI/CD提供了稳定的基础。 fix: Fix module import issues in test environment Resolve relative import errors during test execution to ensure tests run properly. Key fixes: 1. Node module import handling: - Add import exception handling for all node files - Provide fallback import mechanism in test environment - Define minimal fallback classes and functions 2. Test file path corrections: - Fix sys.path settings in test files - Change 'py' path to '../py' for correct module location - Fix class name error in test_new_dci_nodes.py 3. Module compatibility improvements: - preview_node.py: Add complete import fallback - image_node.py: Handle utils module imports - deb_packager_node.py: Handle file_utils imports - deb_loader_node.py: Handle relative import issues - sample_image_node.py: Add import exception handling - __init__.py: Add overall import protection 4. Test results improvement: - Reduced from 7 ERRORs to 0 ERRORs - Reduced from 15 skipped to 14 skipped - Only 1 FAIL remaining (test logic issue, not import issue) These fixes ensure the test suite runs properly in standalone environments, providing a stable foundation for CI/CD.
180 lines
5.9 KiB
Python
180 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
测试纯Python实现的ar功能
|
|
验证deb包的创建和解析都不依赖外部命令
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import shutil
|
|
|
|
# 添加项目路径
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'py'))
|
|
|
|
from nodes.deb_packager_node import DebPackager
|
|
from nodes.deb_loader_node import DebLoader
|
|
|
|
def test_pure_python_ar_implementation():
|
|
"""测试纯Python ar实现"""
|
|
print("=== 测试纯Python ar实现 ===")
|
|
|
|
# 创建测试环境
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
print(f"测试目录: {temp_dir}")
|
|
|
|
# 1. 创建测试文件
|
|
test_files_dir = os.path.join(temp_dir, "test_files")
|
|
os.makedirs(test_files_dir)
|
|
|
|
# 创建一些测试DCI文件
|
|
test_dci_content = b"DCI test content for pure Python implementation"
|
|
for i in range(3):
|
|
dci_file = os.path.join(test_files_dir, f"test_{i}.dci")
|
|
with open(dci_file, 'wb') as f:
|
|
f.write(test_dci_content + f" {i}".encode())
|
|
|
|
print(f"创建了3个测试DCI文件")
|
|
|
|
# 2. 使用deb packager创建deb包(纯Python实现)
|
|
output_dir = os.path.join(temp_dir, "output")
|
|
os.makedirs(output_dir)
|
|
|
|
packager = DebPackager()
|
|
result = packager._execute_impl(
|
|
local_directory=test_files_dir,
|
|
file_filter="*.dci",
|
|
include_subdirectories=True,
|
|
install_target_path="/usr/share/test/icons",
|
|
output_directory=output_dir,
|
|
package_name="test-dci-package",
|
|
package_version="1.0.0",
|
|
maintainer_name="Test User",
|
|
maintainer_email="test@example.com",
|
|
package_description="Test DCI package for pure Python implementation"
|
|
)
|
|
|
|
deb_path, file_list = result
|
|
print(f"Deb包创建结果: {deb_path}")
|
|
print(f"包含文件数量: {len(file_list)}")
|
|
|
|
# 验证deb文件是否创建成功
|
|
if not os.path.exists(deb_path) or "错误" in deb_path:
|
|
print("❌ Deb包创建失败")
|
|
return False
|
|
|
|
deb_size = os.path.getsize(deb_path)
|
|
print(f"✓ Deb包创建成功: {deb_size} 字节")
|
|
|
|
# 3. 使用deb loader解析deb包(纯Python实现)
|
|
loader = DebLoader()
|
|
load_result = loader._execute_impl(
|
|
deb_file_path=deb_path,
|
|
file_filter="*.dci"
|
|
)
|
|
|
|
binary_data_list, relative_paths, image_list, image_relative_paths = load_result
|
|
|
|
print(f"解析结果:")
|
|
print(f" 二进制数据: {len(binary_data_list)} 个文件")
|
|
print(f" 相对路径: {len(relative_paths)} 个")
|
|
print(f" 图像数据: {len(image_list) if isinstance(image_list, list) else 'tensor'}")
|
|
print(f" 图像路径: {len(image_relative_paths)} 个")
|
|
|
|
# 验证解析结果
|
|
if len(binary_data_list) != 3:
|
|
print(f"❌ 解析的文件数量不正确,期望3个,实际{len(binary_data_list)}个")
|
|
return False
|
|
|
|
# 验证文件内容
|
|
for i, (data, path) in enumerate(zip(binary_data_list, relative_paths)):
|
|
expected_content = test_dci_content + f" {i}".encode()
|
|
if data != expected_content:
|
|
print(f"❌ 文件内容不匹配: {path}")
|
|
return False
|
|
print(f" ✓ 文件内容验证成功: {path}")
|
|
|
|
print("✓ 所有测试通过!纯Python实现工作正常")
|
|
return True
|
|
|
|
def test_ar_format_validation():
|
|
"""测试ar格式验证"""
|
|
print("\n=== 测试ar格式验证 ===")
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
# 创建一个简单的ar归档
|
|
packager = DebPackager()
|
|
|
|
# 创建测试文件
|
|
test_file = os.path.join(temp_dir, "test.txt")
|
|
with open(test_file, 'w') as f:
|
|
f.write("Hello, World!")
|
|
|
|
# 创建ar归档
|
|
ar_path = os.path.join(temp_dir, "test.ar")
|
|
success = packager._create_ar_archive_python(ar_path, ["test.txt"], temp_dir)
|
|
|
|
if not success:
|
|
print("❌ ar归档创建失败")
|
|
return False
|
|
|
|
# 验证ar格式
|
|
with open(ar_path, 'rb') as f:
|
|
magic = f.read(8)
|
|
if magic != b'!<arch>\n':
|
|
print(f"❌ ar魔数不正确: {magic}")
|
|
return False
|
|
|
|
ar_size = os.path.getsize(ar_path)
|
|
print(f"✓ ar归档创建成功: {ar_size} 字节")
|
|
print(f"✓ ar格式验证通过")
|
|
|
|
# 测试解析
|
|
extract_dir = os.path.join(temp_dir, "extract")
|
|
os.makedirs(extract_dir)
|
|
|
|
success = packager._extract_ar_archive_python(ar_path, extract_dir)
|
|
if not success:
|
|
print("❌ ar归档解析失败")
|
|
return False
|
|
|
|
# 验证提取的文件
|
|
extracted_file = os.path.join(extract_dir, "test.txt")
|
|
if not os.path.exists(extracted_file):
|
|
print("❌ 文件未正确提取")
|
|
return False
|
|
|
|
with open(extracted_file, 'r') as f:
|
|
content = f.read()
|
|
if content != "Hello, World!":
|
|
print(f"❌ 文件内容不正确: {content}")
|
|
return False
|
|
|
|
print("✓ ar归档解析验证通过")
|
|
return True
|
|
|
|
def main():
|
|
"""主测试函数"""
|
|
print("开始测试纯Python实现...")
|
|
|
|
# 测试ar格式
|
|
if not test_ar_format_validation():
|
|
print("❌ ar格式测试失败")
|
|
return False
|
|
|
|
# 测试完整的deb包流程
|
|
if not test_pure_python_ar_implementation():
|
|
print("❌ 完整流程测试失败")
|
|
return False
|
|
|
|
print("\n🎉 所有测试通过!纯Python实现完全正常工作")
|
|
print("✓ 不再依赖任何外部命令")
|
|
print("✓ 跨平台兼容性良好")
|
|
print("✓ ar格式完全兼容")
|
|
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
sys.exit(0 if success else 1)
|