146 lines
4.2 KiB
Python
146 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
文件重命名工具测试脚本
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
from file_renamer import FileRenamer
|
|
|
|
|
|
def create_test_files(directory):
|
|
"""创建测试文件"""
|
|
test_files = [
|
|
"test_file_1.txt",
|
|
"test_file_2.txt",
|
|
"document_2023-01-01.pdf",
|
|
"image_001.jpg",
|
|
"IMAGE_002.JPG",
|
|
"Report.DOCX"
|
|
]
|
|
|
|
for filename in test_files:
|
|
filepath = os.path.join(directory, filename)
|
|
with open(filepath, 'w', encoding='utf-8') as f:
|
|
f.write(f"这是测试文件 {filename} 的内容")
|
|
|
|
|
|
def test_simple_rename(renamer, test_dir):
|
|
"""测试简单重命名功能"""
|
|
print("测试简单重命名功能...")
|
|
|
|
# 预览模式
|
|
count = renamer.simple_rename(test_dir, "test", "example", preview=True)
|
|
print(f"预览模式重命名了 {count} 个文件")
|
|
|
|
# 实际重命名
|
|
count = renamer.simple_rename(test_dir, "test", "example", preview=False)
|
|
print(f"实际重命名了 {count} 个文件")
|
|
|
|
# 验证结果
|
|
files = os.listdir(test_dir)
|
|
renamed_files = [f for f in files if f.startswith("example")]
|
|
print(f"重命名后的文件: {renamed_files}")
|
|
|
|
|
|
def test_regex_rename(renamer, test_dir):
|
|
"""测试正则表达式重命名功能"""
|
|
print("\n测试正则表达式重命名功能...")
|
|
|
|
# 预览模式
|
|
count = renamer.regex_rename(test_dir, r"\d+", "#", preview=True)
|
|
print(f"预览模式重命名了 {count} 个文件")
|
|
|
|
# 实际重命名
|
|
count = renamer.regex_rename(test_dir, r"\d+", "#", preview=False)
|
|
print(f"实际重命名了 {count} 个文件")
|
|
|
|
|
|
def test_prefix_suffix(renamer, test_dir):
|
|
"""测试前缀/后缀功能"""
|
|
print("\n测试前缀/后缀功能...")
|
|
|
|
# 添加前缀
|
|
count = renamer.add_prefix(test_dir, "PRE_", preview=False)
|
|
print(f"添加前缀重命名了 {count} 个文件")
|
|
|
|
# 添加后缀
|
|
count = renamer.add_suffix(test_dir, "_SUF", preview=False)
|
|
print(f"添加后缀重命名了 {count} 个文件")
|
|
|
|
|
|
def test_enumerate(renamer, test_dir):
|
|
"""测试编号功能"""
|
|
print("\n测试编号功能...")
|
|
|
|
count = renamer.enumerate_files(test_dir, prefix="FILE_", start_number=1, digits=3, preview=False)
|
|
print(f"编号重命名了 {count} 个文件")
|
|
|
|
|
|
def test_case(renamer, test_dir):
|
|
"""测试大小写转换功能"""
|
|
print("\n测试大小写转换功能...")
|
|
|
|
# 转换为小写
|
|
count = renamer.change_case(test_dir, "lower", preview=False)
|
|
print(f"小写转换重命名了 {count} 个文件")
|
|
|
|
# 转换为大写
|
|
count = renamer.change_case(test_dir, "upper", preview=False)
|
|
print(f"大写转换重命名了 {count} 个文件")
|
|
|
|
|
|
def test_undo(renamer):
|
|
"""测试撤销功能"""
|
|
print("\n测试撤销功能...")
|
|
|
|
result = renamer.undo_last_rename()
|
|
if result:
|
|
print("成功撤销一次操作")
|
|
else:
|
|
print("撤销操作失败或无操作可撤销")
|
|
|
|
|
|
def main():
|
|
"""主测试函数"""
|
|
print("开始测试文件重命名工具...")
|
|
|
|
# 创建临时测试目录
|
|
test_dir = tempfile.mkdtemp(prefix="renamer_test_")
|
|
print(f"创建测试目录: {test_dir}")
|
|
|
|
try:
|
|
# 创建测试文件
|
|
create_test_files(test_dir)
|
|
print(f"创建测试文件: {os.listdir(test_dir)}")
|
|
|
|
# 创建重命名工具实例
|
|
renamer = FileRenamer(os.path.join(test_dir, "test_rename_log.txt"))
|
|
|
|
# 运行各项测试
|
|
test_simple_rename(renamer, test_dir)
|
|
test_regex_rename(renamer, test_dir)
|
|
test_prefix_suffix(renamer, test_dir)
|
|
test_enumerate(renamer, test_dir)
|
|
test_case(renamer, test_dir)
|
|
test_undo(renamer)
|
|
|
|
print("\n测试完成!")
|
|
print(f"最终文件列表: {os.listdir(test_dir)}")
|
|
|
|
except Exception as e:
|
|
print(f"测试过程中出现错误: {e}")
|
|
finally:
|
|
# 清理测试目录
|
|
try:
|
|
shutil.rmtree(test_dir)
|
|
print(f"已清理测试目录: {test_dir}")
|
|
except Exception as e:
|
|
print(f"清理测试目录时出错: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |