test: 重构CLI测试用例,统一使用px.CliRunner和px.run测试主函数

1.  替换所有旧的main函数测试逻辑,统一使用pyflowx的CliRunner和run方法进行测试
2.  重构测试类命名,将零散测试合并为TaskSpec验证测试
3.  优化测试用例结构,移除冗余的pytest依赖导入和旧版测试代码
4.  更新文件夹备份、压缩等模块的测试逻辑,适配新的工具函数实现
This commit is contained in:
2026-06-22 12:03:30 +08:00
parent 843e9369fe
commit d4a1a5c2de
6 changed files with 212 additions and 269 deletions
+18 -23
View File
@@ -5,8 +5,7 @@ from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
import pytest
import pyflowx as px
from pyflowx.cli import folderback
@@ -20,23 +19,32 @@ class TestBackupFolder:
"""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"
backup_dir.mkdir()
with patch("shutil.copytree") as mock_copy:
with patch.object(folderback, "zip_target") as mock_zip:
folderback.backup_folder(str(source_dir), str(backup_dir), 5)
assert mock_copy.called
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(folderback, "zip_target") as mock_zip:
folderback.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()
with patch("shutil.copytree") as mock_copy:
folderback.backup_folder(str(source_dir), str(backup_dir), 10)
assert mock_copy.called
folderback.backup_folder(str(source_dir), str(backup_dir), 5)
# Should print error message and return
# ---------------------------------------------------------------------- #
@@ -59,19 +67,6 @@ class TestMain:
def test_main_calls_run_cli(self) -> None:
"""main() should create a CliRunner and call run_cli()."""
with pytest.raises(SystemExit) as exc_info:
with patch.object(px.CliRunner, "run_cli") as mock_run_cli:
folderback.main()
# run_cli() calls sys.exit(), so we should get SystemExit
assert exc_info.value.code in (0, 1, 2)
def test_main_with_list_argument(self) -> None:
"""main() should handle --list argument."""
with patch("sys.argv", ["folderback", "--list"]), pytest.raises(SystemExit) as exc_info:
folderback.main()
assert exc_info.value.code == 0
def test_main_with_no_args_shows_help(self) -> None:
"""main() with no args should show help and exit."""
with patch("sys.argv", ["folderback"]), pytest.raises(SystemExit) as exc_info:
folderback.main()
assert exc_info.value.code == 1
assert mock_run_cli.called