Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(tray): peach-tray 先解析参数再拿锁,--help 不再造出数据根

longmeidaoebbcd6c4b9e2
diff --git a/src/peach/tray.py b/src/peach/tray.py
index 56397c376..d73e76255 100644
--- a/src/peach/tray.py
+++ b/src/peach/tray.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import argparse
import ctypes
import logging
import os
@@ -985,7 +986,19 @@ def run_macos_menu_bar(manager: "ServiceManager") -> None:
watcher.stop()
-def main() -> int:
+def build_parser() -> argparse.ArgumentParser:
+ """`peach-tray` 的命令行。它没有子命令和选项,正常使用不带任何参数。"""
+ return argparse.ArgumentParser(
+ prog="peach-tray",
+ description="启动 Peach 托盘(macOS 上是菜单栏项)并接管本机服务;通常不带参数直接运行。",
+ )
+
+
+def main(argv: list[str] | None = None) -> int:
+ # 参数解析必须排在任何进程级副作用之前:`--help` 和拼错的参数都要在 HiDPI 设置、
+ # 单实例锁和目录创建之前退出,否则一条试探命令就会在磁盘上凭空造出一个数据根,
+ # 让之后的安装探测把这台机器误判成已配置。
+ build_parser().parse_args(argv)
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
enable_hidpi()
instance = SingleInstance(STATE_DIR / "peach-tray.lock")
diff --git a/tests/test_tray.py b/tests/test_tray.py
index c81cda005..228ad7a1b 100644
--- a/tests/test_tray.py
+++ b/tests/test_tray.py
@@ -1,5 +1,7 @@
+import contextlib
import importlib.util
import inspect
+import io
import os
import plistlib
import sys
@@ -28,6 +30,7 @@ from peach.tray import (
launchd_owns_this_process, ledger_menu_items, restart_tray_process,
tray_restart_required,
)
+from peach.tray import main as tray_main
from peach.sync import SyncPlan
from peach.versioning import UpdateResult, VersionSnapshot
from peach.windows_update import PendingWindowsUpdate, WindowsUpdatePreparation
@@ -678,5 +681,53 @@ class MacAppBundleTests(unittest.TestCase):
self.assertFalse((app / "Contents" / "MacOS" / "stale").exists())
+class TrayCommandLineTests(unittest.TestCase):
+ """`peach-tray` 的参数解析先于一切进程级副作用。
+
+ 新用户试探 `peach-tray --help` 时,程序不能先去拿单实例锁:那一步会在 venv 旁边
+ 凭空建出 `peach-data/state/`,之后的安装探测就把这台机器当成已配置。
+ """
+
+ def setUp(self):
+ self._tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(self._tmp.cleanup)
+ self.state_dir = Path(self._tmp.name).resolve() / "state"
+ for target in ("peach.tray.enable_hidpi", "peach.tray.SingleInstance",
+ "peach.tray.ServiceManager", "peach.tray.webbrowser"):
+ patcher = patch(target)
+ self.addCleanup(patcher.stop)
+ setattr(self, target.rsplit(".", 1)[1], patcher.start())
+ patcher = patch("peach.tray.STATE_DIR", self.state_dir)
+ self.addCleanup(patcher.stop)
+ patcher.start()
+
+ def _run(self, argv):
+ stdout, stderr = io.StringIO(), io.StringIO()
+ with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
+ with self.assertRaises(SystemExit) as raised:
+ tray_main(argv)
+ return raised.exception.code, stdout.getvalue(), stderr.getvalue()
+
+ def _assert_nothing_touched(self):
+ self.assertFalse(self.state_dir.exists(), "帮助与参数错误不得创建状态目录")
+ self.enable_hidpi.assert_not_called()
+ self.SingleInstance.assert_not_called()
+ self.ServiceManager.assert_not_called()
+ self.webbrowser.open.assert_not_called()
+
+ def test_help_exits_zero_and_leaves_the_state_directory_alone(self):
+ code, stdout, _ = self._run(["--help"])
+ self.assertEqual(code, 0)
+ self.assertIn("peach-tray", stdout)
+ self.assertIn("托盘", stdout)
+ self._assert_nothing_touched()
+
+ def test_unknown_argument_exits_two_and_leaves_the_state_directory_alone(self):
+ code, _, stderr = self._run(["--bogus"])
+ self.assertEqual(code, 2)
+ self.assertIn("--bogus", stderr)
+ self._assert_nothing_touched()
+
+
if __name__ == "__main__":
unittest.main()