diff --git a/src/sundry.py b/src/sundry.py index c9e608f..82efa04 100644 --- a/src/sundry.py +++ b/src/sundry.py @@ -8,6 +8,32 @@ from function.constant.paths import SUNDRY_LOCATION +def _can_it_run_on_non_windows_systems(tool: str, args: list[str]) -> bool: + """ + 判断该工具的操作是否可以在非 Windows 系统上运行。 + + Args: + tool: 要运行的工具 + args: 工具的参数 + + Returns: + True: 可以运行 + False: 不能运行 + """ + + if tool in [ + "移除", "remove", "自动移除", "autoremove", # 验证阶段需要 WinGet,不确定如何读取 Token + "单改", "单修改", "modify", "hash-update", # 验证清单需要 WinGet,不确定如何读取 Token + "verify", "test", "验证", "测试", # 仅 Windows + ]: + return False + + if tool in ("忽略", "检查忽略", "ignore"): # 不确定如何读取 Token + return bool(args and (args[0] in ("list", "--list", "列", "列出", "现有", "now"))) + + return True + + def main() -> int: init(autoreset=True) ajaw.load_translations() @@ -19,12 +45,7 @@ def main() -> int: tool = "help" args = [] - if (sys.platform != "win32") and (tool in ( - "移除", "remove", "自动移除", "autoremove", # 验证阶段需要 WinGet,不确定如何读取 Token - "单改", "单修改", "modify", "hash-update", # 验证清单需要 WinGet,不确定如何读取 Token - "忽略", "检查忽略", "ignore", # 不确定如何读取 Token - "verify", "test", "验证", "测试", # 仅 Windows - )): + if (sys.platform != "win32") and (not _can_it_run_on_non_windows_systems(tool, args)): print(f"{消息头.错误} 该操作仅可在 Windows 上运行") return 1 diff --git a/tests/test_sundry.py b/tests/test_sundry.py new file mode 100644 index 0000000..8cb9be2 --- /dev/null +++ b/tests/test_sundry.py @@ -0,0 +1,17 @@ +import pytest # noqa: I001 - 和 isort 冲突 + +from sundry import _can_it_run_on_non_windows_systems # pyright: ignore[reportPrivateUsage] + + +@pytest.mark.parametrize( + ("tool", "args", "excepted"), + [ + ("remove", [], False), + ("ignore", [], False), + ("ignore", ["add", "xxx"], False), + ("ignore", ["list"], True), + ("version", [], True) + ] +) +def test_can_it_run_on_non_windows_systems(tool: str, args: list[str], excepted: bool) -> None: + assert _can_it_run_on_non_windows_systems(tool, args) == excepted