open_actions.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
  3. import os
  4. from contextlib import contextmanager
  5. from kitty.utils import get_editor
  6. from . import BaseTest
  7. @contextmanager
  8. def patch_env(**kw):
  9. orig = os.environ.copy()
  10. for k, v in kw.items():
  11. if v is None:
  12. os.environ.pop(k, None)
  13. else:
  14. os.environ[k] = v
  15. yield
  16. os.environ.clear()
  17. os.environ.update(orig)
  18. class TestOpenActions(BaseTest):
  19. def test_parsing_of_open_actions(self):
  20. from kitty.open_actions import KeyAction, actions_for_url
  21. self.set_options()
  22. spec = '''
  23. protocol file
  24. mime text/*
  25. fragment_matches .
  26. AcTion launch $EDITOR $FILE_PATH $FRAGMENT
  27. action
  28. protocol file
  29. mime text/*
  30. action ignored
  31. ext py,txt
  32. action one
  33. action two
  34. '''
  35. def actions(url):
  36. with patch_env(FILE_PATH='notgood'):
  37. return tuple(actions_for_url(url, spec))
  38. def single(url, func, *args):
  39. acts = actions(url)
  40. self.ae(len(acts), 1)
  41. self.ae(acts[0].func, func)
  42. self.ae(acts[0].args, args)
  43. single('file://hostname/tmp/moo.txt#23', 'launch', *get_editor(), '/tmp/moo.txt', '23')
  44. single('some thing.txt', 'ignored')
  45. self.ae(actions('x:///a.txt'), (KeyAction('one', ()), KeyAction('two', ())))