mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-20 23:25:46 +00:00
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
158 lines
6.3 KiB
Python
158 lines
6.3 KiB
Python
#!/usr/bin/env python
|
|
# License: GPLv3 Copyright: 2026 Kovid Goyal <kovid at kovidgoyal.net>
|
|
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
from unittest.mock import patch
|
|
|
|
from kitty.constants import kitty_exe
|
|
from kitty.rc.base import PayloadGetter, all_command_names, command_for_name
|
|
|
|
from .base import BaseTest
|
|
|
|
|
|
class Window:
|
|
def __init__(self, id: int, os_window_id: int):
|
|
self.id = id
|
|
self.os_window_id = os_window_id
|
|
|
|
|
|
class Boss:
|
|
def __init__(self, windows: list[Window]):
|
|
self.active_window = windows[0]
|
|
self.all_windows = windows
|
|
|
|
def match_windows(self, expr: str, self_window: Window | None = None):
|
|
if expr == 'all':
|
|
yield from self.all_windows
|
|
return
|
|
for window in self.all_windows:
|
|
if str(window.id) == expr:
|
|
yield window
|
|
|
|
|
|
class TestRemoteControl(BaseTest):
|
|
def test_set_os_window_title_command_is_registered(self):
|
|
self.assertIn('set_os_window_title', all_command_names())
|
|
self.ae(command_for_name('set-os-window-title').name, 'set-os-window-title')
|
|
|
|
def test_set_os_window_title_dedupes_os_windows(self):
|
|
windows = [Window(1, 11), Window(2, 11), Window(3, 22)]
|
|
boss = Boss(windows)
|
|
cmd = command_for_name('set-os-window-title')
|
|
|
|
with patch('kitty.rc.set_os_window_title.set_os_window_title_impl') as setter:
|
|
cmd.response_from_kitty(boss, windows[0], PayloadGetter(cmd, {'match': 'all', 'self': True, 'title': 'repo'}))
|
|
|
|
self.ae({call.args for call in setter.call_args_list}, {(11, 'repo'), (22, 'repo')})
|
|
|
|
def test_set_os_window_title_without_title_resets_override(self):
|
|
windows = [Window(1, 11)]
|
|
boss = Boss(windows)
|
|
cmd = command_for_name('set-os-window-title')
|
|
|
|
with patch('kitty.rc.set_os_window_title.set_os_window_title_impl') as setter:
|
|
cmd.response_from_kitty(boss, windows[0], PayloadGetter(cmd, {'self': True}))
|
|
|
|
setter.assert_called_once_with(11, '')
|
|
|
|
def test_set_colors_transparent_background_colors(self):
|
|
from kitty.fast_data_types import Color
|
|
from kitty.rc.base import RemoteControlErrorWithoutTraceback, parse_subcommand_cli
|
|
from kitty.remote_control import parse_rc_args
|
|
|
|
cmd = command_for_name('set-colors')
|
|
|
|
def payload(*args: str):
|
|
opts, items = parse_subcommand_cli(cmd, ['set-colors'] + list(args))
|
|
return cmd.message_to_kitty(parse_rc_args([])[0], opts, items)
|
|
|
|
# omitted means leave unchanged, explicitly empty means clear
|
|
self.assertNotIn('transparent_background_colors', payload('foreground=red')['colors'])
|
|
self.ae(payload('transparent_background_colors=')['colors']['transparent_background_colors'], '')
|
|
self.ae(payload('transparent_background_colors=red@0.5')['colors']['transparent_background_colors'], '#ff0000@0.5')
|
|
|
|
windows = [Window(1, 11)]
|
|
boss = Boss(windows)
|
|
boss.color_settings_at_startup = {'foreground': Color(1, 2, 3)}
|
|
boss.transparent_background_colors_at_startup = ((Color(0, 0, 0), 0.25),)
|
|
|
|
def response(p):
|
|
with patch('kitty.colors.patch_colors') as patcher:
|
|
cmd.response_from_kitty(boss, windows[0], PayloadGetter(cmd, p))
|
|
patcher.assert_called_once()
|
|
return patcher.call_args.args
|
|
|
|
self.ae(response({'colors': {'foreground': 0xFF0000}}), ({'foreground': 0xFF0000}, None, False))
|
|
colors = {'foreground': 0xFF0000, 'transparent_background_colors': 'red@0.5'}
|
|
self.ae(response({'colors': colors, 'configured': True}), ({'foreground': 0xFF0000}, ((Color(255, 0, 0), 0.5),), True))
|
|
self.ae(colors['transparent_background_colors'], 'red@0.5') # payload must not be mutated
|
|
self.ae(response({'colors': {'transparent_background_colors': ''}}), ({}, (), False))
|
|
# reset implies configured even when the client does not set it
|
|
self.ae(
|
|
response({'colors': {'transparent_background_colors': 'red@0.5'}, 'reset': True}),
|
|
({'foreground': int(Color(1, 2, 3))}, ((Color(0, 0, 0), 0.25),), True),
|
|
)
|
|
for bad in (0, 'not-a-color@0.5'):
|
|
with self.subTest(bad=bad), self.assertRaises(RemoteControlErrorWithoutTraceback):
|
|
response({'colors': {'transparent_background_colors': bad}})
|
|
|
|
|
|
TALK_THREAD_SURVIVES_ACCEPT_ERROR = """
|
|
import os, socket, sys, time
|
|
from kitty.fast_data_types import ChildMonitor
|
|
ls = socket.socket(socket.AF_UNIX)
|
|
ls.bind(sys.argv[1])
|
|
ls.listen(8)
|
|
cm = ChildMonitor(lambda *a: None, None, -1, ls.fileno())
|
|
a, b = socket.socketpair()
|
|
cm.inject_peer(os.dup(a.fileno()))
|
|
cm.fail_next_accepts(2)
|
|
c = socket.socket(socket.AF_UNIX)
|
|
c.connect(sys.argv[1])
|
|
deadline = time.monotonic() + 10
|
|
while cm.fail_next_accepts(-1) and time.monotonic() < deadline:
|
|
time.sleep(0.01)
|
|
assert cm.fail_next_accepts(-1) == 0, 'injected accept errors not consumed'
|
|
cm.inject_peer(os.dup(b.fileno()))
|
|
print('ok', flush=True)
|
|
os._exit(0)
|
|
"""
|
|
|
|
|
|
TALK_THREAD_STALLED_PEER = """
|
|
import os, socket
|
|
from kitty.fast_data_types import ChildMonitor, send_data_to_peer
|
|
cm = ChildMonitor(lambda *a: None, None)
|
|
a, a_remote = socket.socketpair()
|
|
b, b_remote = socket.socketpair()
|
|
big = os.urandom(8 * 1024 * 1024)
|
|
pa = cm.inject_peer(os.dup(a.fileno()))
|
|
send_data_to_peer(pa, big)
|
|
pb = cm.inject_peer(os.dup(b.fileno()))
|
|
send_data_to_peer(pb, b'hello')
|
|
b_remote.settimeout(10)
|
|
assert b_remote.recv(16) == b'hello'
|
|
a_remote.settimeout(10)
|
|
got = bytearray()
|
|
while len(got) < len(big):
|
|
got += a_remote.recv(1024 * 1024)
|
|
assert got == big, 'stalled peer response corrupted'
|
|
print('ok', flush=True)
|
|
os._exit(0)
|
|
"""
|
|
|
|
|
|
class TestTalkThread(BaseTest):
|
|
def test_talk_thread_not_blocked_by_stalled_peer(self):
|
|
p = subprocess.run([kitty_exe(), '+runpy', TALK_THREAD_STALLED_PEER], capture_output=True, text=True, timeout=30)
|
|
self.assertEqual(p.returncode, 0, p.stderr)
|
|
self.assertIn('ok', p.stdout, p.stderr)
|
|
|
|
def test_talk_thread_survives_accept_errors(self):
|
|
with tempfile.TemporaryDirectory() as tdir:
|
|
p = subprocess.run([kitty_exe(), '+runpy', TALK_THREAD_SURVIVES_ACCEPT_ERROR, os.path.join(tdir, 's')], capture_output=True, text=True, timeout=30)
|
|
self.assertEqual(p.returncode, 0, p.stderr)
|
|
self.assertIn('ok', p.stdout, p.stderr)
|
|
self.assertEqual(p.stderr.count('accept() on talk socket failed'), 1, p.stderr)
|