Skip to content

armgit / peach

Public Imported from GitHub
0

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

test: avoid macOS app registration in sandbox

Peach local maintenancebd0b9fed553b
diff --git a/tests/test_netwatch.py b/tests/test_netwatch.py
index 02169cc46..e96ed6f83 100644
--- a/tests/test_netwatch.py
+++ b/tests/test_netwatch.py
@@ -1,7 +1,9 @@
+import os
import sys
import threading
import time
import unittest
+from unittest.mock import patch
from peach.netwatch import NETWORK_CHANGE, NetworkChangeWatcher
@@ -35,6 +37,20 @@ class NetworkChangeWatcherTests(unittest.TestCase):
def test_a_failing_callback_does_not_kill_the_watcher(self):
"""回调挂掉不该带走整个托盘。"""
calls = []
+ notify_read, notify_write = os.pipe()
+ self.addCleanup(os.close, notify_read)
+ self.addCleanup(os.close, notify_write)
+
+ class FakeLibSystem:
+ @staticmethod
+ def notify_register_file_descriptor(_key, descriptor, _flags, token):
+ descriptor._obj.value = notify_read
+ token._obj.value = 1
+ return 0
+
+ @staticmethod
+ def notify_cancel(_token):
+ return 0
def boom():
calls.append(1)
@@ -42,9 +58,15 @@ class NetworkChangeWatcherTests(unittest.TestCase):
watcher = NetworkChangeWatcher(boom)
self.addCleanup(watcher.stop)
- watcher.start()
- time.sleep(0.2)
- self.assertTrue(watcher._thread.is_alive())
+ with patch("peach.netwatch._libsystem", return_value=FakeLibSystem()):
+ watcher.start()
+ os.write(notify_write, b"ping")
+ for _ in range(20):
+ if calls:
+ break
+ time.sleep(0.01)
+ self.assertEqual(calls, [1])
+ self.assertTrue(watcher._thread.is_alive())
@unittest.skipIf(sys.platform == "darwin", "非 macOS 才走这条")
def test_unsupported_platform_reports_itself(self):
diff --git a/tests/test_tray.py b/tests/test_tray.py
index e49fabb6d..6dabbf176 100644
--- a/tests/test_tray.py
+++ b/tests/test_tray.py
@@ -89,7 +89,11 @@ class TrayTests(unittest.TestCase):
return UpdateResult("unconfigured", "未配置更新源", snapshot)
icon = Mock()
- tray = PeachTray(ServiceManager(tuple()), Versions())
+ # 这里只验证后台更新行为,不创建真实系统托盘。macOS 的 pystray Icon 构造会向
+ # WindowServer 注册应用;在 Codex seatbelt 沙箱内系统会直接 SIGABRT,连测试
+ # 报告都来不及生成,并弹出「Python 意外退出」。
+ with patch("peach.tray.pystray.Icon", return_value=Mock()):
+ tray = PeachTray(ServiceManager(tuple()), Versions())
tray.check_updates(icon)
self.assertTrue(completed.wait(2))
for _ in range(20):