# -*- coding: utf-8 -*- """测改名和卸载。 小心的事: uninstall 会删数据目录 —— 测试时**必须**把它指到临时目录, 否则会把用户真正的配对码清掉。 """ from __future__ import annotations import json import shutil import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "windows")) passed = 0 failed = 0 def check(name, ok, detail=""): global passed, failed if ok: passed += 1 print(" [PASS] " + name) else: failed += 1 print(" [FAIL] " + name + " " + str(detail)) def reg_query(name: str) -> str: proc = subprocess.run( ["reg", "query", r"HKCU\Software\Microsoft\Windows\CurrentVersion\Run", "/v", name], capture_output=True, text=True, encoding="mbcs", errors="replace") return proc.stdout if proc.returncode == 0 else "" def main() -> int: from lanremote import autostart print("一、名字改对了吗") check("自启项名是 ykxmz", autostart.VALUE_NAME == "ykxmz", autostart.VALUE_NAME) check("记得要清理的旧名", autostart.LEGACY_VALUE_NAMES == ("LanRemoteAgent",), autostart.LEGACY_VALUE_NAMES) print() print("二、真的写进注册表看看(测完会删掉)") had_before = autostart.is_enabled() if had_before: print(" (本来就有自启项,测试时会先记下来,最后恢复)") original = autostart.current_value() try: message = autostart.enable() check("enable 成功了", "已开启" in message, message[:80]) # 注册表里确实有 ykxmz out = reg_query("ykxmz") check("注册表里有 ykxmz 这一项", "ykxmz" in out, out[:100]) check("它指向真实存在的程序", "--silent" in out or ".exe" in out, out[:120]) # 任务管理器的启动项列表(StartupApproved)里能看到 proc = subprocess.run( ["reg", "query", r"HKCU\Software\Microsoft\Windows\CurrentVersion" r"\Explorer\StartupApproved\Run"], capture_output=True, text=True, encoding="mbcs", errors="replace") # 没写过 StartupApproved 也算正常(说明没被禁用过); # 关键是 Run 里有,任务管理器就会列出来。 check("Run 项存在(任务管理器就会显示)", "ykxmz" in reg_query("ykxmz")) # 旧名要清掉 check("旧名 LanRemoteAgent 不在注册表里", "LanRemoteAgent" not in reg_query("LanRemoteAgent")) print() print("三、关闭要能删掉") autostart.disable() check("disable 之后 is_enabled 变 False", not autostart.is_enabled()) check("注册表里查不到了", reg_query("ykxmz") == "") finally: # 恢复原状 if had_before: subprocess.run( ["reg", "add", r"HKCU\Software\Microsoft\Windows\CurrentVersion\Run", "/v", "ykxmz", "/t", "REG_SZ", "/d", original, "/f"], capture_output=True) print(" (已恢复原来那条自启项)") else: autostart.disable() print() print("四、卸载逻辑(数据目录指到临时目录,不动你的真数据)") from lanremote import uninstall work = Path(tempfile.mkdtemp(prefix="uninstall_test_")) try: # 造假的数据目录,并把它指过去 fake_data = work / "data" fake_data.mkdir(parents=True, exist_ok=True) (fake_data / "config.json").write_text( json.dumps({"pairing_code": "123456"}), encoding="utf-8") original_data_dir = uninstall.data_dir uninstall.data_dir = lambda: str(fake_data) result = uninstall._remove_data() check("删数据目录成功", "已删除" in result, result) check("数据目录真的没了", not fake_data.exists()) uninstall.data_dir = original_data_dir # 自启项清理 result = uninstall._remove_autostart() check("清理自启项不报错", isinstance(result, str) and result, result) # 源码模式下不该真去删自己 result = uninstall._schedule_self_delete() check("源码模式不安排自删", "源码模式" in result, result) finally: shutil.rmtree(work, ignore_errors=True) print() print("五、源码模式下 --uninstall 不会真的删程序") from lanremote.uninstall import run as do_uninstall original_data_dir = uninstall.data_dir work2 = Path(tempfile.mkdtemp(prefix="uninstall_test2_")) try: fake2 = work2 / "data" fake2.mkdir(parents=True, exist_ok=True) uninstall.data_dir = lambda: str(fake2) text = do_uninstall() check("返回了说明", "卸载完成" in text, text[:80]) check("说明了不会删源码", "源码模式" in text, text[:200]) check("真的删了数据目录(临时那个)", not fake2.exists()) finally: uninstall.data_dir = original_data_dir shutil.rmtree(work2, ignore_errors=True) print() print("=" * 62) print(f"通过 {passed} 项,失败 {failed} 项") return 0 if failed == 0 else 1 if __name__ == "__main__": raise SystemExit(main())