7a87a4177e
CI / Lint, Typecheck & Test (push) Failing after 3m39s
新增 src/pyflowx/ops/_common.py 提供 platform_command/ensure_platform/IGNORE_PATTERNS 三个公共 API, 配套 12 个单元测试; 修复上个提交中 8 个 ops 模块引用缺失模块导致的 ImportError.
114 lines
4.7 KiB
Python
114 lines
4.7 KiB
Python
"""Tests for ops._common 共享辅助模块."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from pyflowx.conditions import Constants
|
|
from pyflowx.ops._common import IGNORE_PATTERNS, ensure_platform, platform_command
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# IGNORE_PATTERNS
|
|
# ---------------------------------------------------------------------- #
|
|
class TestIgnorePatterns:
|
|
"""``IGNORE_PATTERNS`` 常量测试."""
|
|
|
|
def test_is_list_of_str(self) -> None:
|
|
"""应为 list[str]."""
|
|
assert isinstance(IGNORE_PATTERNS, list)
|
|
assert all(isinstance(p, str) for p in IGNORE_PATTERNS)
|
|
|
|
def test_contains_key_patterns(self) -> None:
|
|
"""应包含常见忽略目录/文件模式."""
|
|
for key in ["__pycache__", "*.pyc", ".git", ".venv", "dist", "build"]:
|
|
assert key in IGNORE_PATTERNS
|
|
|
|
def test_contains_ide_and_cache_dirs(self) -> None:
|
|
"""应包含 IDE 与工具缓存目录."""
|
|
for key in [".idea", ".vscode", ".pytest_cache", ".mypy_cache", ".tox"]:
|
|
assert key in IGNORE_PATTERNS
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# platform_command
|
|
# ---------------------------------------------------------------------- #
|
|
class TestPlatformCommand:
|
|
"""``platform_command`` 函数测试."""
|
|
|
|
def test_windows_returns_windows_cmd(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Windows 上应返回 windows 命令."""
|
|
monkeypatch.setattr(Constants, "IS_WINDOWS", True)
|
|
result = platform_command(["cls"], ["clear"])
|
|
assert result == ["cls"]
|
|
|
|
def test_non_windows_returns_posix_cmd(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""非 Windows 上应返回 posix 命令."""
|
|
monkeypatch.setattr(Constants, "IS_WINDOWS", False)
|
|
result = platform_command(["cls"], ["clear"])
|
|
assert result == ["clear"]
|
|
|
|
def test_returns_copy_not_reference(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""返回值应为副本, 修改不影响后续调用."""
|
|
monkeypatch.setattr(Constants, "IS_WINDOWS", True)
|
|
result = platform_command(["where"], ["which"])
|
|
result.append("extra")
|
|
result2 = platform_command(["where"], ["which"])
|
|
assert result2 == ["where"]
|
|
assert "extra" not in result2
|
|
|
|
def test_multi_arg_command(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""多参数命令应完整保留."""
|
|
monkeypatch.setattr(Constants, "IS_WINDOWS", True)
|
|
result = platform_command(["taskkill", "/f", "/im"], ["pkill", "-f"])
|
|
assert result == ["taskkill", "/f", "/im"]
|
|
|
|
def test_index_access(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""应支持 [0] 索引取首元素."""
|
|
monkeypatch.setattr(Constants, "IS_WINDOWS", False)
|
|
cmd = platform_command(["python"], ["python3"])[0]
|
|
assert cmd == "python3"
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# ensure_platform
|
|
# ---------------------------------------------------------------------- #
|
|
class TestEnsurePlatform:
|
|
"""``ensure_platform`` 函数测试."""
|
|
|
|
def test_predicate_true_returns_true(
|
|
self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
"""predicate 为 True 时应返回 True 且不打印."""
|
|
result = ensure_platform(True, "tool", "Windows")
|
|
assert result is True
|
|
captured = capsys.readouterr()
|
|
assert captured.out == ""
|
|
|
|
def test_predicate_false_returns_false(
|
|
self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
"""predicate 为 False 时应返回 False 并打印提示."""
|
|
result = ensure_platform(False, "reset_icon_cache", "Windows")
|
|
assert result is False
|
|
captured = capsys.readouterr()
|
|
assert "仅在 Windows 上支持" in captured.out
|
|
assert "reset_icon_cache" in captured.out
|
|
|
|
def test_message_includes_tool_and_platform(self, capsys: pytest.CaptureFixture[str]) -> None:
|
|
"""提示信息应包含工具名与平台名."""
|
|
ensure_platform(False, "setup_linux_system_mirror", "Linux")
|
|
captured = capsys.readouterr()
|
|
assert "setup_linux_system_mirror" in captured.out
|
|
assert "Linux" in captured.out
|
|
|
|
def test_with_constants_predicate(
|
|
self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
"""应能与 Constants.IS_* 谓词配合使用."""
|
|
monkeypatch.setattr(Constants, "IS_LINUX", False)
|
|
result = ensure_platform(Constants.IS_LINUX, "install_linux_docker", "Linux")
|
|
assert result is False
|
|
captured = capsys.readouterr()
|
|
assert "仅在 Linux 上支持" in captured.out
|