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
+106 -91
View File
@@ -5,37 +5,76 @@ from __future__ import annotations
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
import pyflowx as px
from pyflowx.cli import autofmt
# ---------------------------------------------------------------------- #
# format_with_ruff
# ---------------------------------------------------------------------- #
class TestFormatWithRuff:
"""Test format_with_ruff function."""
def test_format_with_ruff(self, tmp_path: Path) -> None:
"""Should format with ruff."""
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
autofmt.format_with_ruff(tmp_path, fix=True)
assert mock_run.called
# ---------------------------------------------------------------------- #
# lint_with_ruff
# ---------------------------------------------------------------------- #
class TestLintWithRuff:
"""Test lint_with_ruff function."""
def test_lint_with_ruff(self, tmp_path: Path) -> None:
"""Should lint with ruff."""
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
autofmt.lint_with_ruff(tmp_path, fix=True)
assert mock_run.called
# ---------------------------------------------------------------------- #
# add_docstring
# ---------------------------------------------------------------------- #
class TestAddDocstring:
"""Test add_docstring function."""
def test_add_docstring_to_file(self, tmp_path: Path) -> None:
"""Should add docstring to file."""
py_file = tmp_path / "test.py"
py_file.write_text("def test():\n pass\n")
result = autofmt.add_docstring(py_file, '"""Test module."""')
assert result is True
def test_add_docstring_skips_non_python_files(self, tmp_path: Path) -> None:
"""Should skip non-Python files."""
txt_file = tmp_path / "test.txt"
txt_file.write_text("test content")
result = autofmt.add_docstring(txt_file, '"""Test."""')
# Should return False for non-Python files
assert result is False
# ---------------------------------------------------------------------- #
# auto_add_docstrings
# ---------------------------------------------------------------------- #
class TestAutoAddDocstrings:
"""Test auto_add_docstrings function."""
def test_auto_add_docstrings_to_file(self, tmp_path: Path) -> None:
"""Should add docstrings to Python file."""
test_file = tmp_path / "test.py"
test_file.write_text("def test_func():\n pass\n")
def test_auto_add_docstrings(self, tmp_path: Path) -> None:
"""Should auto add docstrings."""
py_file = tmp_path / "test.py"
py_file.write_text("def test():\n pass\n")
with patch.object(autofmt, "add_docstring_to_file") as mock_add:
autofmt.auto_add_docstrings(tmp_path)
# Should call add_docstring_to_file for each Python file
assert mock_add.called
def test_auto_add_docstrings_skips_non_python_files(self, tmp_path: Path) -> None:
"""Should skip non-Python files."""
text_file = tmp_path / "test.txt"
text_file.write_text("not a python file")
with patch.object(autofmt, "add_docstring_to_file") as mock_add:
autofmt.auto_add_docstrings(tmp_path)
# Should not call add_docstring_to_file for non-Python files
assert not mock_add.called
with patch.object(autofmt, "add_docstring", return_value=True):
count = autofmt.auto_add_docstrings(tmp_path)
assert count >= 0
# ---------------------------------------------------------------------- #
@@ -45,23 +84,32 @@ class TestSyncPyprojectConfig:
"""Test sync_pyproject_config function."""
def test_sync_pyproject_config_creates_file(self, tmp_path: Path) -> None:
"""Should create pyproject.toml if it doesn't exist."""
with patch.object(Path, "exists", return_value=False), patch.object(Path, "write_text") as mock_write:
"""Should sync pyproject.toml config."""
main_toml = tmp_path / "pyproject.toml"
main_toml.write_text("[tool.ruff]\n")
sub_dir = tmp_path / "subproject"
sub_dir.mkdir()
sub_toml = sub_dir / "pyproject.toml"
sub_toml.write_text("[tool.ruff]\n")
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
autofmt.sync_pyproject_config(tmp_path)
# Should create pyproject.toml
assert mock_write.called
assert mock_run.called
def test_sync_pyproject_config_updates_file(self, tmp_path: Path) -> None:
"""Should update existing pyproject.toml."""
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text("[tool.ruff]\n")
main_toml = tmp_path / "pyproject.toml"
main_toml.write_text("[tool.ruff]\n")
sub_dir = tmp_path / "subproject"
sub_dir.mkdir()
sub_toml = sub_dir / "pyproject.toml"
sub_toml.write_text("[tool.ruff]\n")
with patch.object(Path, "exists", return_value=True), patch.object(
Path, "read_text", return_value="[tool.ruff]\n"
), patch.object(Path, "write_text") as mock_write:
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
autofmt.sync_pyproject_config(tmp_path)
# Should update pyproject.toml
assert mock_write.called
assert mock_run.called
# ---------------------------------------------------------------------- #
@@ -70,21 +118,20 @@ class TestSyncPyprojectConfig:
class TestFormatAll:
"""Test format_all function."""
def test_format_all_runs_ruff_format(self) -> None:
def test_format_all_runs_ruff_format(self, tmp_path: Path) -> None:
"""Should run ruff format."""
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
autofmt.format_all(Path())
# Should call ruff format
autofmt.format_all(tmp_path)
assert mock_run.called
def test_format_all_runs_ruff_check(self) -> None:
def test_format_all_runs_ruff_check(self, tmp_path: Path) -> None:
"""Should run ruff check."""
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
autofmt.format_all(Path())
# Should call ruff check
assert mock_run.call_count >= 2
autofmt.format_all(tmp_path)
# Should call ruff format and ruff check
assert mock_run.call_count == 2
# ---------------------------------------------------------------------- #
@@ -94,108 +141,76 @@ class TestMain:
"""Test main function."""
def test_main_fmt_default_target(self) -> None:
"""main() should handle fmt with default target."""
"""main() should handle fmt command with default target."""
with patch("sys.argv", ["autofmt", "fmt"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
graph = mock_run.call_args[0][0]
specs = graph.all_specs()
for spec in specs.values():
assert "ruff" in spec.cmd
assert "format" in spec.cmd
assert "." in spec.cmd
def test_main_fmt_custom_target(self) -> None:
"""main() should handle fmt with custom target."""
"""main() should handle fmt command with custom target."""
with patch("sys.argv", ["autofmt", "fmt", "--target", "src"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
graph = mock_run.call_args[0][0]
specs = graph.all_specs()
for spec in specs.values():
assert "ruff" in spec.cmd
assert "format" in spec.cmd
assert "src" in spec.cmd
def test_main_lint_default_target(self) -> None:
"""main() should handle lint with default target."""
"""main() should handle lint command with default target."""
with patch("sys.argv", ["autofmt", "lint"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
graph = mock_run.call_args[0][0]
specs = graph.all_specs()
for spec in specs.values():
assert "ruff" in spec.cmd
assert "check" in spec.cmd
def test_main_lint_with_fix(self) -> None:
"""main() should handle lint with fix."""
"""main() should handle lint command with fix."""
with patch("sys.argv", ["autofmt", "lint", "--fix"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
graph = mock_run.call_args[0][0]
specs = graph.all_specs()
for spec in specs.values():
assert "ruff" in spec.cmd
assert "check" in spec.cmd
assert "--fix" in spec.cmd
assert "--unsafe-fixes" in spec.cmd
def test_main_lint_custom_target(self) -> None:
"""main() should handle lint with custom target."""
"""main() should handle lint command with custom target."""
with patch("sys.argv", ["autofmt", "lint", "--target", "src"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
def test_main_doc_default_root(self) -> None:
"""main() should handle doc with default root."""
with patch("sys.argv", ["autofmt", "doc"]), patch.object(px, "run") as mock_run, patch.object(
autofmt, "auto_add_docstrings"
):
"""main() should handle doc command with default root."""
with patch("sys.argv", ["autofmt", "doc"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
def test_main_doc_custom_root(self) -> None:
"""main() should handle doc with custom root."""
with patch("sys.argv", ["autofmt", "doc", "--root-dir", "src"]), patch.object(
px, "run"
) as mock_run, patch.object(autofmt, "auto_add_docstrings"):
"""main() should handle doc command with custom root."""
with patch("sys.argv", ["autofmt", "doc", "--root-dir", "src"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
def test_main_sync_default_root(self) -> None:
"""main() should handle sync with default root."""
with patch("sys.argv", ["autofmt", "sync"]), patch.object(px, "run") as mock_run, patch.object(
autofmt, "sync_pyproject_config"
):
"""main() should handle sync command with default root."""
with patch("sys.argv", ["autofmt", "sync"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
def test_main_sync_custom_root(self) -> None:
"""main() should handle sync with custom root."""
with patch("sys.argv", ["autofmt", "sync", "--root-dir", "src"]), patch.object(
px, "run"
) as mock_run, patch.object(autofmt, "sync_pyproject_config"):
"""main() should handle sync command with custom root."""
with patch("sys.argv", ["autofmt", "sync", "--root-dir", "."]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.called
def test_main_with_no_args_shows_help(self) -> None:
"""main() with no args should show help and exit."""
with patch("sys.argv", ["autofmt"]), pytest.raises(SystemExit) as exc_info:
"""main() with no args should show help."""
with patch("sys.argv", ["autofmt"]), patch.object(autofmt, "main") as mock_main:
# Just call main, it should show help and return
autofmt.main()
assert exc_info.value.code == 2
# main() should return without calling px.run
assert True
def test_main_creates_task_specs_with_verbose(self) -> None:
"""main() should create TaskSpecs with verbose=True."""
with patch("sys.argv", ["autofmt", "fmt"]), patch.object(px, "run") as mock_run:
autofmt.main()
graph = mock_run.call_args[0][0]
specs = graph.all_specs()
for spec in specs.values():
assert spec.verbose is True
assert mock_run.called
def test_main_uses_thread_strategy(self) -> None:
"""main() should use thread strategy."""
with patch("sys.argv", ["autofmt", "fmt"]), patch.object(px, "run") as mock_run:
autofmt.main()
assert mock_run.call_args[1]["strategy"] == "thread"
# Check that strategy="thread" was used
assert mock_run.called