04d6871e8d
1. 移除folderzip和folderback模块中的默认导出函数与测试用例 2. 重构跨平台命令获取逻辑,统一使用platform_common工具函数 3. 移动IGNORE_PATTERNS常量到公共模块,精简packtool和autofmt代码 4. 重构平台检查逻辑,复用ensure_platform减少重复代码 5. 优化pdftool模块的依赖检查逻辑,提取公共检查函数 6. 修复测试用例中的字符串断言冗余内容
148 lines
5.0 KiB
Python
148 lines
5.0 KiB
Python
"""Tests for cli.folderback module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from pyflowx.ops import folderback as files
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# remove_dump
|
|
# ---------------------------------------------------------------------- #
|
|
class TestRemoveDump:
|
|
"""Test remove_dump function."""
|
|
|
|
def test_remove_dump_no_files(self, tmp_path: Path) -> None:
|
|
"""Should handle no zip files."""
|
|
src = tmp_path / "source"
|
|
src.mkdir()
|
|
dst = tmp_path / "backup"
|
|
dst.mkdir()
|
|
|
|
files.remove_dump(src, dst, 5)
|
|
# Should not raise error
|
|
|
|
def test_remove_dump_within_limit(self, tmp_path: Path) -> None:
|
|
"""Should not remove files within limit."""
|
|
src = tmp_path / "source"
|
|
src.mkdir()
|
|
dst = tmp_path / "backup"
|
|
dst.mkdir()
|
|
|
|
# Create some zip files
|
|
for i in range(3):
|
|
zip_file = dst / f"source_20240101_12000{i}.zip"
|
|
zip_file.write_bytes(b"ZIP content")
|
|
|
|
files.remove_dump(src, dst, 5)
|
|
# All files should remain
|
|
assert len(list(dst.glob("*.zip"))) == 3
|
|
|
|
def test_remove_dump_exceeds_limit(self, tmp_path: Path) -> None:
|
|
"""Should remove oldest files when exceeds limit."""
|
|
src = tmp_path / "source"
|
|
src.mkdir()
|
|
dst = tmp_path / "backup"
|
|
dst.mkdir()
|
|
|
|
# Create more zip files than limit
|
|
for i in range(7):
|
|
zip_file = dst / f"source_20240101_12000{i}.zip"
|
|
zip_file.write_bytes(b"ZIP content")
|
|
|
|
files.remove_dump(src, dst, 5)
|
|
# Should have only 5 files
|
|
assert len(list(dst.glob("*.zip"))) == 5
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# zip_target
|
|
# ---------------------------------------------------------------------- #
|
|
class TestZipTarget:
|
|
"""Test zip_target function."""
|
|
|
|
def test_zip_target_creates_zip(self, tmp_path: Path) -> None:
|
|
"""Should create zip file."""
|
|
src = tmp_path / "source"
|
|
src.mkdir()
|
|
(src / "test.txt").write_text("test content")
|
|
dst = tmp_path / "backup"
|
|
dst.mkdir()
|
|
|
|
with patch("time.strftime", return_value="_20240101_120000"):
|
|
files.zip_target(src, dst, 5)
|
|
|
|
# Should create zip file
|
|
zip_files = list(dst.glob("*.zip"))
|
|
assert len(zip_files) == 1
|
|
|
|
def test_zip_target_with_subdirectories(self, tmp_path: Path) -> None:
|
|
"""Should zip files in subdirectories."""
|
|
src = tmp_path / "source"
|
|
src.mkdir()
|
|
subdir = src / "subdir"
|
|
subdir.mkdir()
|
|
(src / "test.txt").write_text("test content")
|
|
(subdir / "nested.txt").write_text("nested content")
|
|
dst = tmp_path / "backup"
|
|
dst.mkdir()
|
|
|
|
with patch("time.strftime", return_value="_20240101_120000"):
|
|
files.zip_target(src, dst, 5)
|
|
|
|
# Should create zip file
|
|
zip_files = list(dst.glob("*.zip"))
|
|
assert len(zip_files) == 1
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# backup_folder
|
|
# ---------------------------------------------------------------------- #
|
|
class TestBackupFolder:
|
|
"""Test backup_folder function."""
|
|
|
|
def test_backup_folder_with_source_and_backup(self, tmp_path: Path) -> None:
|
|
"""Should backup folder with source and backup paths."""
|
|
source_dir = tmp_path / "source"
|
|
source_dir.mkdir()
|
|
(source_dir / "test.txt").write_text("test content")
|
|
backup_dir = tmp_path / "backup"
|
|
|
|
with patch.object(files, "zip_target") as mock_zip:
|
|
files.backup_folder(str(source_dir), str(backup_dir), 5)
|
|
assert mock_zip.called
|
|
|
|
def test_backup_folder_with_max_backups(self, tmp_path: Path) -> None:
|
|
"""Should backup folder with max backups."""
|
|
source_dir = tmp_path / "source"
|
|
source_dir.mkdir()
|
|
(source_dir / "test.txt").write_text("test content")
|
|
backup_dir = tmp_path / "backup"
|
|
|
|
with patch.object(files, "zip_target") as mock_zip:
|
|
files.backup_folder(str(source_dir), str(backup_dir), 10)
|
|
assert mock_zip.called
|
|
|
|
def test_backup_folder_source_not_exists(self, tmp_path: Path) -> None:
|
|
"""Should handle non-existent source folder."""
|
|
source_dir = tmp_path / "nonexistent"
|
|
backup_dir = tmp_path / "backup"
|
|
backup_dir.mkdir()
|
|
|
|
files.backup_folder(str(source_dir), str(backup_dir), 5)
|
|
# Should print error message and return
|
|
|
|
def test_backup_folder_creates_dst(self, tmp_path: Path) -> None:
|
|
"""Should create destination directory."""
|
|
source_dir = tmp_path / "source"
|
|
source_dir.mkdir()
|
|
(source_dir / "test.txt").write_text("test content")
|
|
backup_dir = tmp_path / "backup"
|
|
|
|
with patch.object(files, "zip_target") as mock_zip:
|
|
files.backup_folder(str(source_dir), str(backup_dir), 5)
|
|
assert backup_dir.exists()
|
|
assert mock_zip.called
|