168 lines
6.6 KiB
Python
168 lines
6.6 KiB
Python
"""routine/system.py (what differs on Linux and macOS, asked of either from here) and routine/crash.py
|
|
(unhandled errors logged without routine contents). Run: python3 -m unittest tests.test_system"""
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, ROOT)
|
|
from routine import crash, system # noqa: E402
|
|
|
|
HOME = {"HOME": "/home/friend"}
|
|
|
|
|
|
class Places(unittest.TestCase):
|
|
def test_linux(self):
|
|
self.assertEqual(system.data_dir("linux", HOME), "/home/friend/.local/share/routine")
|
|
self.assertEqual(system.config_dir("linux", HOME), "/home/friend/.config/routine")
|
|
self.assertEqual(system.log_dir("linux", HOME), "/home/friend/.local/state/routine")
|
|
self.assertEqual(system.log_dir("linux", dict(HOME, XDG_STATE_HOME="/st")), "/st/routine")
|
|
|
|
def test_mac(self):
|
|
sup = "/home/friend/Library/Application Support/Routine"
|
|
self.assertEqual(system.data_dir("darwin", HOME), sup)
|
|
self.assertEqual(system.config_dir("darwin", HOME), sup)
|
|
self.assertEqual(system.log_dir("darwin", HOME), "/home/friend/Library/Logs/Routine")
|
|
|
|
def test_routine_home_wins_everywhere(self):
|
|
env = dict(HOME, ROUTINE_HOME="/scratch/x")
|
|
for plat in ("linux", "darwin"):
|
|
self.assertEqual(system.data_dir(plat, env), "/scratch/x")
|
|
self.assertEqual(system.config_dir(plat, env), "/scratch/x")
|
|
self.assertEqual(system.log_dir(plat, env), "/scratch/x/logs")
|
|
|
|
def test_paths_uses_them(self):
|
|
from routine import paths
|
|
self.assertEqual(paths.DATA, system.data_dir())
|
|
self.assertEqual(paths.CONFIG, os.path.join(system.config_dir(), "config.json"))
|
|
|
|
|
|
class Features(unittest.TestCase):
|
|
def test_mac_vs_linux(self):
|
|
self.assertTrue(system.native_frame("darwin") and system.global_menubar("darwin"))
|
|
self.assertFalse(system.native_frame("linux") or system.global_menubar("linux"))
|
|
self.assertFalse(system.sync_supported("darwin"))
|
|
self.assertTrue(system.sync_supported("linux"))
|
|
|
|
def test_sound_players(self):
|
|
self.assertEqual(system.sound_players("darwin"), ["afplay"])
|
|
self.assertEqual(system.sound_players("linux"), ["pw-play", "paplay"])
|
|
|
|
def test_audio_env_linux_only(self):
|
|
env = {}
|
|
system.audio_env("darwin", env)
|
|
self.assertEqual(env, {})
|
|
system.audio_env("linux", env)
|
|
self.assertEqual(env, {"QT_AUDIO_BACKEND": "pulseaudio"})
|
|
env = {"QT_AUDIO_BACKEND": "pipewire"}
|
|
system.audio_env("linux", env)
|
|
self.assertEqual(env["QT_AUDIO_BACKEND"], "pipewire") # the user's choice wins
|
|
|
|
def test_no_gsettings_off_linux(self):
|
|
run = mock.Mock(side_effect=AssertionError("ran a command"))
|
|
self.assertEqual(system.gnome_setting("a.b", "c", "darwin", run), "")
|
|
env = {}
|
|
system.cursor_env("darwin", env, run)
|
|
self.assertEqual(env, {})
|
|
run.assert_not_called()
|
|
|
|
def test_gsettings_on_linux(self):
|
|
run = mock.Mock(return_value=subprocess.CompletedProcess([], 0, stdout="'Adwaita'\n"))
|
|
env = {"XCURSOR_SIZE": "32"}
|
|
system.cursor_env("linux", env, run)
|
|
self.assertEqual(env, {"XCURSOR_SIZE": "32", "XCURSOR_THEME": "Adwaita"})
|
|
self.assertEqual(run.call_count, 1) # the size was set already
|
|
missing = mock.Mock(side_effect=FileNotFoundError)
|
|
self.assertEqual(system.gnome_setting("a.b", "c", "linux", missing), "")
|
|
|
|
def test_shortcuts(self):
|
|
self.assertEqual(system.shortcut("history", "linux"), "Ctrl+H")
|
|
self.assertEqual(system.shortcut("history", "darwin"), "Ctrl+Y") # ⌘H hides the app
|
|
|
|
def test_instance_name(self):
|
|
self.assertEqual(system.instance_name(None), system.APP_ID)
|
|
a, b = system.instance_name("/tmp/a/very/long/path" * 10), system.instance_name("/tmp/b")
|
|
self.assertNotEqual(a, b)
|
|
self.assertNotIn("/", a)
|
|
self.assertLess(len(a), 40)
|
|
|
|
def test_mac_app_name_is_quiet_off_a_mac(self):
|
|
self.assertFalse(system.mac_app_name("Routine", "linux"))
|
|
|
|
def test_describe(self):
|
|
self.assertIn("Python", system.describe())
|
|
|
|
|
|
def raise_and_handle(exc):
|
|
try:
|
|
raise exc
|
|
except BaseException:
|
|
return crash.handle(*sys.exc_info())
|
|
|
|
|
|
class Crash(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.mkdtemp(prefix="routine-crash-")
|
|
self.notices = []
|
|
crash.reset()
|
|
crash._state.update(dir=self.tmp, notify=self.notices.append)
|
|
self.err = mock.patch.object(sys, "stderr", open(os.devnull, "w"))
|
|
self.err.start()
|
|
|
|
def tearDown(self):
|
|
sys.stderr.close()
|
|
self.err.stop()
|
|
crash._state.update(dir=None, notify=None)
|
|
|
|
def log(self):
|
|
with open(os.path.join(self.tmp, crash.LOG_NAME)) as f:
|
|
return f.read()
|
|
|
|
def test_message_kept_for_code_errors_left_out_for_others(self):
|
|
self.assertTrue(raise_and_handle(AttributeError("'QHelpEvent' object has no attribute 'position'")))
|
|
self.assertTrue(raise_and_handle(KeyError("my secret task")))
|
|
self.assertTrue(raise_and_handle(ValueError("dear diary")))
|
|
text = self.log()
|
|
self.assertIn("has no attribute 'position'", text)
|
|
self.assertNotIn("my secret task", text)
|
|
self.assertNotIn("dear diary", text)
|
|
self.assertIn("KeyError: (message left out", text)
|
|
self.assertIn("Traceback", text)
|
|
self.assertIn("Routine ", text)
|
|
|
|
def test_once_per_distinct_error(self):
|
|
for _ in range(3):
|
|
raise_and_handle(TypeError("x")) # the same line each time: one error
|
|
self.assertEqual(len(self.notices), 1)
|
|
self.assertEqual(self.log().count("TypeError"), 1)
|
|
raise_and_handle(NameError("y"))
|
|
self.assertEqual(len(self.notices), 2)
|
|
self.assertEqual(self.notices[0], os.path.join(self.tmp, crash.LOG_NAME))
|
|
|
|
def test_threads_log_without_a_notice(self):
|
|
crash.handle(*_exc_info(RuntimeError("z")), gui=False)
|
|
self.assertEqual(self.notices, [])
|
|
self.assertIn("RuntimeError", self.log())
|
|
|
|
def test_log_rolls_over(self):
|
|
path = os.path.join(self.tmp, crash.LOG_NAME)
|
|
with open(path, "w") as f:
|
|
f.write("x" * (crash.MAX_BYTES + 1))
|
|
raise_and_handle(IndexError("i"))
|
|
self.assertTrue(os.path.exists(path + ".1"))
|
|
self.assertLess(os.path.getsize(path), 10_000)
|
|
|
|
|
|
def _exc_info(exc):
|
|
try:
|
|
raise exc
|
|
except BaseException:
|
|
return sys.exc_info()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|