"""Tests for reseticoncache tool (reset_icon_cache_run 函数).""" from __future__ import annotations import subprocess import pytest from pyflowx.conditions import Constants from pyflowx.ops.reseticoncache import reset_icon_cache_run class TestResetIconCacheRun: """``reset_icon_cache_run`` 函数测试.""" def test_non_windows_skips(self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None: """非 Windows 平台应打印提示并跳过.""" monkeypatch.setattr(Constants, "IS_WINDOWS", False) reset_icon_cache_run() captured = capsys.readouterr() assert "仅在 Windows 上支持" in captured.out def test_windows_no_localappdata_skips( self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] ) -> None: """Windows 上 LOCALAPPDATA 未设置时应跳过.""" monkeypatch.setattr(Constants, "IS_WINDOWS", True) monkeypatch.setattr("os.environ.get", lambda key, default="": "" if key == "LOCALAPPDATA" else default) reset_icon_cache_run() captured = capsys.readouterr() assert "LOCALAPPDATA" in captured.out def test_windows_executes_all_steps( self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] ) -> None: """Windows 上应依次执行 kill/delete/delete_all/restart 四步.""" monkeypatch.setattr(Constants, "IS_WINDOWS", True) local_app_data = "/tmp/fake_local_app_data" monkeypatch.setattr( "os.environ.get", lambda key, default="": local_app_data if key == "LOCALAPPDATA" else default ) # 让 icon_cache_db 和 explorer_cache_dir 都不存在, 跳过删除分支 from pathlib import Path monkeypatch.setattr(Path, "exists", lambda _self: False) ran_cmds: list[list[str]] = [] monkeypatch.setattr(subprocess, "run", lambda cmd, **_: ran_cmds.append(cmd)) reset_icon_cache_run() captured = capsys.readouterr() # 验证执行了 kill_explorer 和 restart_explorer (delete 分支因 exists=False 被跳过) assert any("taskkill" in str(cmd) for cmd in ran_cmds) assert any("explorer.exe" in str(cmd) for cmd in ran_cmds) assert any("start" in str(cmd) and "explorer.exe" in str(cmd) for cmd in ran_cmds) assert "图标缓存已重置" in captured.out