2022-09-09 13:04:07 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
2022-09-15 16:13:52 +03:00
|
|
|
import os
|
2022-09-09 13:04:07 +03:00
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from kitty.constants import kitty_tool_exe as kitty_tool
|
|
|
|
|
|
|
|
from . import BaseTest
|
|
|
|
|
|
|
|
|
|
|
|
class TestCompletion(BaseTest):
|
|
|
|
|
|
|
|
def test_completion(self):
|
2022-09-15 16:13:52 +03:00
|
|
|
with tempfile.TemporaryDirectory() as tdir:
|
|
|
|
completion(self, tdir)
|
2022-09-09 13:04:07 +03:00
|
|
|
|
|
|
|
|
2022-09-16 11:38:22 +03:00
|
|
|
def get_all_words(result):
|
|
|
|
all_words = set()
|
|
|
|
for group in result.get('groups', ()):
|
|
|
|
for m in group['matches']:
|
|
|
|
all_words.add(m['word'])
|
|
|
|
return all_words
|
|
|
|
|
|
|
|
|
2022-09-09 13:04:07 +03:00
|
|
|
def has_words(*words):
|
|
|
|
def t(self, result):
|
|
|
|
q = set(words)
|
2022-09-16 11:38:22 +03:00
|
|
|
missing = q - get_all_words(result)
|
|
|
|
self.assertFalse(missing, f'Words missing. Command line: {self.current_cmd!r}')
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
|
|
def does_not_have_words(*words):
|
|
|
|
def t(self, result):
|
|
|
|
q = set(words)
|
|
|
|
all_words = get_all_words(result)
|
|
|
|
self.assertFalse(q & all_words, f'Words unexpectedly present. Command line: {self.current_cmd!r}')
|
2022-09-09 13:04:07 +03:00
|
|
|
return t
|
|
|
|
|
|
|
|
|
2022-09-15 16:13:52 +03:00
|
|
|
def all_words(*words):
|
|
|
|
def t(self, result):
|
|
|
|
expected = set(words)
|
2022-09-16 11:38:22 +03:00
|
|
|
actual = get_all_words(result)
|
2022-09-15 16:13:52 +03:00
|
|
|
self.assertEqual(expected, actual, f'Command line: {self.current_cmd!r}')
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
|
|
def completion(self: TestCompletion, tdir: str):
|
2022-09-09 13:04:07 +03:00
|
|
|
all_cmds = []
|
|
|
|
all_argv = []
|
|
|
|
all_tests = []
|
|
|
|
|
|
|
|
def add(cmdline: str, *tests):
|
|
|
|
all_cmds.append(cmdline)
|
|
|
|
new_word = cmdline.endswith(' ')
|
|
|
|
if new_word:
|
|
|
|
cmdline = cmdline[:-1]
|
|
|
|
all_argv.append(shlex.split(cmdline))
|
|
|
|
if new_word:
|
|
|
|
all_argv[-1].append('')
|
|
|
|
all_tests.append(tests)
|
|
|
|
|
|
|
|
def run_tool():
|
2022-09-15 16:13:52 +03:00
|
|
|
env = os.environ.copy()
|
|
|
|
env['PATH'] = os.path.join(tdir, 'bin')
|
2022-09-15 17:12:21 +03:00
|
|
|
env['HOME'] = os.path.join(tdir, 'sub')
|
2022-09-16 18:23:05 +03:00
|
|
|
env['KITTY_CONFIG_DIRECTORY'] = os.path.join(tdir, 'sub')
|
2022-09-15 16:13:52 +03:00
|
|
|
cp = subprocess.run(
|
|
|
|
[kitty_tool(), '__complete__', 'json'],
|
|
|
|
check=True, stdout=subprocess.PIPE, cwd=tdir, input=json.dumps(all_argv).encode(), env=env
|
|
|
|
)
|
|
|
|
self.assertEqual(cp.returncode, 0, f'kitty-tool __complete__ failed with exit code: {cp.returncode}')
|
|
|
|
return json.loads(cp.stdout)
|
2022-09-09 13:04:07 +03:00
|
|
|
|
2022-09-16 19:02:10 +03:00
|
|
|
add('kitty ', has_words('@', '@ls', '+', '+open'))
|
2022-09-09 13:04:07 +03:00
|
|
|
add('kitty @ l', has_words('ls', 'last-used-layout', 'launch'))
|
|
|
|
add('kitty @l', has_words('@ls', '@last-used-layout', '@launch'))
|
|
|
|
|
2022-09-15 16:13:52 +03:00
|
|
|
def make_file(path, mode=None):
|
|
|
|
with open(os.path.join(tdir, path), mode='x') as f:
|
|
|
|
if mode is not None:
|
|
|
|
os.chmod(f.fileno(), mode)
|
|
|
|
|
|
|
|
os.mkdir(os.path.join(tdir, 'bin'))
|
|
|
|
os.mkdir(os.path.join(tdir, 'sub'))
|
|
|
|
make_file('bin/exe1', 0o700)
|
|
|
|
make_file('bin/exe-not1')
|
|
|
|
make_file('exe2', 0o700)
|
2022-09-15 19:19:04 +03:00
|
|
|
make_file('exe-not2.jpeg')
|
2022-09-15 16:13:52 +03:00
|
|
|
make_file('sub/exe3', 0o700)
|
2022-09-15 19:19:04 +03:00
|
|
|
make_file('sub/exe-not3.png')
|
2022-09-15 16:13:52 +03:00
|
|
|
|
|
|
|
add('kitty x', all_words())
|
|
|
|
add('kitty e', all_words('exe1'))
|
2022-09-16 06:16:26 +03:00
|
|
|
add('kitty ./', all_words('./bin/', './sub/', './exe2'))
|
2022-09-15 16:13:52 +03:00
|
|
|
add('kitty ./e', all_words('./exe2'))
|
2022-09-16 06:16:26 +03:00
|
|
|
add('kitty ./s', all_words('./sub/'))
|
2022-09-15 17:12:21 +03:00
|
|
|
add('kitty ~', all_words('~/exe3'))
|
|
|
|
add('kitty ~/', all_words('~/exe3'))
|
|
|
|
add('kitty ~/e', all_words('~/exe3'))
|
2022-09-15 16:13:52 +03:00
|
|
|
|
2022-09-15 19:19:04 +03:00
|
|
|
add('kitty @ goto-layout ', has_words('tall', 'fat'))
|
|
|
|
add('kitty @ goto-layout spli', all_words('splits'))
|
|
|
|
add('kitty @ goto-layout f f', all_words())
|
2022-09-16 06:26:07 +03:00
|
|
|
add('kitty @ set-window-logo ', all_words('exe-not2.jpeg', 'sub/'))
|
2022-09-15 19:19:04 +03:00
|
|
|
add('kitty @ set-window-logo e', all_words('exe-not2.jpeg'))
|
|
|
|
add('kitty @ set-window-logo e e', all_words())
|
2022-09-16 19:02:10 +03:00
|
|
|
add('kitty +ope', has_words('+open'))
|
|
|
|
add('kitty +open -', has_words('-1', '-T'))
|
2022-09-15 19:19:04 +03:00
|
|
|
|
2022-09-16 11:38:22 +03:00
|
|
|
add('kitty -', has_words('-c', '-1', '--'), does_not_have_words('--config', '--single-instance'))
|
|
|
|
add('kitty -c', all_words('-c'))
|
|
|
|
add('kitty --', has_words('--config', '--single-instance', '--'))
|
2022-09-16 11:58:54 +03:00
|
|
|
add('kitty --s', has_words('--session', '--start-as'))
|
2022-09-16 11:38:22 +03:00
|
|
|
add('kitty --start-as', all_words('--start-as'))
|
|
|
|
add('kitty --start-as ', all_words('minimized', 'maximized', 'fullscreen', 'normal'))
|
2022-09-16 14:59:41 +03:00
|
|
|
add('kitty -1 ', does_not_have_words('@ls', '@'))
|
|
|
|
add('kitty --directory ', all_words('bin/', 'sub/'))
|
|
|
|
add('kitty -1d ', all_words('bin/', 'sub/'))
|
2022-09-16 18:11:18 +03:00
|
|
|
add('kitty -1d', all_words('-1d'))
|
2022-09-16 19:25:31 +03:00
|
|
|
add('kitty -o a', has_words('allow_remote_control='))
|
|
|
|
add('kitty --listen-on ', all_words('unix:', 'tcp:'))
|
|
|
|
add('kitty --listen-on unix:b', all_words('unix:bin/'))
|
2022-09-16 18:11:18 +03:00
|
|
|
add('kitty --directory=', all_words('--directory=bin/', '--directory=sub/'))
|
|
|
|
add('kitty --start-as=m', all_words('--start-as=minimized', '--start-as=maximized'))
|
2022-09-16 18:23:05 +03:00
|
|
|
add('kitty @launch --ty', has_words('--type'))
|
|
|
|
add('kitty @launch --type ', has_words('window', 'background', 'overlay'))
|
|
|
|
add('kitty @launch --cwd ', has_words('current', 'oldest', 'last_reported'))
|
|
|
|
add('kitty @launch --logo ', all_words('exe-not3.png'))
|
|
|
|
add('kitty @launch --logo ~', all_words('~/exe-not3.png'))
|
2022-09-16 11:38:22 +03:00
|
|
|
|
2022-09-17 09:29:41 +03:00
|
|
|
add('kitty + ', has_words('launch', 'kitten'))
|
|
|
|
add('kitty + kitten ', has_words('icat', 'diff'))
|
|
|
|
add('kitty +kitten icat ', has_words('sub/', 'exe-not2.jpeg'))
|
|
|
|
add('kitty + kitten icat --pr', has_words('--print-window-size'))
|
|
|
|
add('kitty + kitten diff ', has_words('exe-not2.jpeg'))
|
|
|
|
add('kitty + kitten themes --', has_words('--cache-age'))
|
|
|
|
add('kitty + kitten themes D', has_words('Default'))
|
|
|
|
|
2022-09-17 10:02:02 +03:00
|
|
|
add('clone-in-kitty --ty', has_words('--type'))
|
|
|
|
make_file('editable.txt')
|
|
|
|
add('edit-in-kitty ', has_words('editable.txt'))
|
|
|
|
|
2022-09-09 13:04:07 +03:00
|
|
|
for cmd, tests, result in zip(all_cmds, all_tests, run_tool()):
|
|
|
|
self.current_cmd = cmd
|
|
|
|
for test in tests:
|
|
|
|
test(self, result)
|