mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-17 14:04:52 +02:00
Allow specifying rules to perform arbitrary actions in kitty when opening URLs
This commit is contained in:
57
kitty_tests/open_actions.py
Normal file
57
kitty_tests/open_actions.py
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python3
|
||||
# vim:fileencoding=utf-8
|
||||
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
|
||||
from . import BaseTest
|
||||
|
||||
|
||||
@contextmanager
|
||||
def patch_env(**kw):
|
||||
orig = os.environ.copy()
|
||||
for k, v in kw.items():
|
||||
if v is None:
|
||||
os.environ.pop(k, None)
|
||||
else:
|
||||
os.environ[k] = v
|
||||
yield
|
||||
os.environ.clear()
|
||||
os.environ.update(orig)
|
||||
|
||||
|
||||
class TestOpenActions(BaseTest):
|
||||
|
||||
def test_parsing_of_open_actions(self):
|
||||
from kitty.open_actions import actions_for_url, KeyAction
|
||||
spec = '''
|
||||
protocol file
|
||||
mime text/*
|
||||
has_fragment yes
|
||||
AcTion launch $EDITOR $FILE_PATH $FRAGMENT
|
||||
action
|
||||
|
||||
protocol file
|
||||
mime text/*
|
||||
action ignored
|
||||
|
||||
ext py,txt
|
||||
action one
|
||||
action two
|
||||
'''
|
||||
|
||||
def actions(url):
|
||||
with patch_env(EDITOR='editor', FILE_PATH='notgood'):
|
||||
return tuple(actions_for_url(url, spec))
|
||||
|
||||
def single(url, func, *args):
|
||||
acts = actions(url)
|
||||
self.ae(len(acts), 1)
|
||||
self.ae(acts[0].func, func)
|
||||
self.ae(acts[0].args, args)
|
||||
|
||||
single('file://hostname/tmp/moo.txt#23', 'launch', 'editor', '/tmp/moo.txt', '23')
|
||||
single('some thing.txt', 'ignored')
|
||||
self.ae(actions('x:///a.txt'), (KeyAction('one', ()), KeyAction('two', ())))
|
||||
Reference in New Issue
Block a user