2014-07-19 11:45:44 -04:00
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import re
|
2015-03-05 05:21:09 -05:00
|
|
|
|
import shutil
|
2014-07-19 11:45:44 -04:00
|
|
|
|
import subprocess
|
|
|
|
|
import tempfile
|
|
|
|
|
import textwrap
|
|
|
|
|
import time
|
|
|
|
|
|
2015-03-05 05:21:09 -05:00
|
|
|
|
from test.constant import (ARR_D, ARR_L, ARR_R, ARR_U, BS, ESC, PYTHON3,
|
2015-07-03 15:52:50 -04:00
|
|
|
|
SEQUENCES)
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
2015-01-20 15:26:03 -05:00
|
|
|
|
|
2014-07-19 11:45:44 -04:00
|
|
|
|
def wait_until_file_exists(file_path, times=None, interval=0.01):
|
|
|
|
|
while times is None or times:
|
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
|
return True
|
|
|
|
|
time.sleep(interval)
|
|
|
|
|
if times is not None:
|
|
|
|
|
times -= 1
|
|
|
|
|
return False
|
|
|
|
|
|
2015-01-20 15:26:03 -05:00
|
|
|
|
|
2014-07-19 11:45:44 -04:00
|
|
|
|
def read_text_file(filename):
|
|
|
|
|
"""Reads the contens of a text file."""
|
|
|
|
|
if PYTHON3:
|
2015-01-20 15:26:03 -05:00
|
|
|
|
return open(filename, 'r', encoding='utf-8').read()
|
2014-07-19 11:45:44 -04:00
|
|
|
|
else:
|
2015-01-20 15:26:03 -05:00
|
|
|
|
return open(filename, 'r').read()
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_process_running(pid):
|
|
|
|
|
"""Returns true if a process with pid is running, false otherwise."""
|
2015-01-20 15:26:03 -05:00
|
|
|
|
# from
|
|
|
|
|
# http://stackoverflow.com/questions/568271/how-to-check-if-there-exists-a-process-with-a-given-pid
|
2014-07-19 11:45:44 -04:00
|
|
|
|
try:
|
|
|
|
|
os.kill(pid, 0)
|
|
|
|
|
except OSError:
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
|
|
|
|
|
2015-01-20 15:26:03 -05:00
|
|
|
|
|
2014-07-19 11:45:44 -04:00
|
|
|
|
def silent_call(cmd):
|
|
|
|
|
"""Calls 'cmd' and returns the exit value."""
|
|
|
|
|
return subprocess.call(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
|
|
2015-01-20 15:26:03 -05:00
|
|
|
|
|
2014-07-19 11:45:44 -04:00
|
|
|
|
def create_directory(dirname):
|
|
|
|
|
"""Creates 'dirname' and its parents if it does not exist."""
|
|
|
|
|
try:
|
|
|
|
|
os.makedirs(dirname)
|
|
|
|
|
except OSError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TempFileManager(object):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
|
|
|
|
|
def __init__(self, name=''):
|
|
|
|
|
self._temp_dir = tempfile.mkdtemp(prefix='UltiSnipsTest_' + name)
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
|
|
def name_temp(self, file_path):
|
|
|
|
|
return os.path.join(self._temp_dir, file_path)
|
|
|
|
|
|
|
|
|
|
def write_temp(self, file_path, content):
|
|
|
|
|
abs_path = self.name_temp(file_path)
|
|
|
|
|
create_directory(os.path.dirname(abs_path))
|
|
|
|
|
if PYTHON3:
|
2015-01-20 15:26:03 -05:00
|
|
|
|
with open(abs_path, 'w', encoding='utf-8') as f:
|
2014-07-19 11:45:44 -04:00
|
|
|
|
f.write(content)
|
|
|
|
|
else:
|
2015-01-20 15:26:03 -05:00
|
|
|
|
with open(abs_path, 'w') as f:
|
2014-07-19 11:45:44 -04:00
|
|
|
|
f.write(content)
|
|
|
|
|
return abs_path
|
|
|
|
|
|
2015-01-20 15:26:03 -05:00
|
|
|
|
def unique_name_temp(self, suffix='', prefix=''):
|
|
|
|
|
file_handler, abspath = tempfile.mkstemp(
|
|
|
|
|
suffix, prefix, self._temp_dir)
|
2014-07-19 11:45:44 -04:00
|
|
|
|
os.close(file_handler)
|
|
|
|
|
os.remove(abspath)
|
|
|
|
|
return abspath
|
|
|
|
|
|
|
|
|
|
def clear_temp(self):
|
|
|
|
|
shutil.rmtree(self._temp_dir)
|
|
|
|
|
create_directory(self._temp_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VimInterface(TempFileManager):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
|
2015-07-03 15:52:50 -04:00
|
|
|
|
def __init__(self, vim_executable, name):
|
2014-07-19 11:45:44 -04:00
|
|
|
|
TempFileManager.__init__(self, name)
|
2015-07-03 15:52:50 -04:00
|
|
|
|
self._vim_executable = vim_executable
|
2016-09-16 16:48:54 -04:00
|
|
|
|
self._version = None
|
2015-12-09 08:59:50 -05:00
|
|
|
|
|
2018-03-31 18:56:34 -04:00
|
|
|
|
@property
|
|
|
|
|
def vim_executable(self):
|
|
|
|
|
return self._vim_executable
|
|
|
|
|
|
2016-09-16 16:48:54 -04:00
|
|
|
|
def has_version(self, major, minor, patchlevel):
|
|
|
|
|
if self._version is None:
|
2015-12-09 08:59:50 -05:00
|
|
|
|
output = subprocess.check_output([
|
|
|
|
|
self._vim_executable, "--version"
|
|
|
|
|
])
|
|
|
|
|
|
2016-09-16 16:48:54 -04:00
|
|
|
|
_major = 0
|
|
|
|
|
_minor = 0
|
|
|
|
|
_patch = 0
|
2015-12-09 08:59:50 -05:00
|
|
|
|
for line in output.decode('utf-8').split("\n"):
|
2016-09-16 16:48:54 -04:00
|
|
|
|
if line.startswith("VIM - Vi IMproved"):
|
|
|
|
|
_major, _minor = map(int, line.split()[4].split('.'))
|
2015-12-09 08:59:50 -05:00
|
|
|
|
if line.startswith("Included patches:"):
|
2016-09-16 16:48:54 -04:00
|
|
|
|
_patch = int(line.split('-')[1])
|
|
|
|
|
self._version = (_major, _minor, _patch)
|
2015-12-09 08:59:50 -05:00
|
|
|
|
|
2016-09-16 16:48:54 -04:00
|
|
|
|
return self._version >= (major, minor, patchlevel)
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
|
|
def get_buffer_data(self):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
buffer_path = self.unique_name_temp(prefix='buffer_')
|
2015-07-14 15:58:30 -04:00
|
|
|
|
self.send_to_vim(ESC + ':w! %s\n' % buffer_path)
|
2014-07-19 11:45:44 -04:00
|
|
|
|
if wait_until_file_exists(buffer_path, 50):
|
|
|
|
|
return read_text_file(buffer_path)[:-1]
|
|
|
|
|
|
2015-07-14 15:58:30 -04:00
|
|
|
|
def send_to_terminal(self, s):
|
|
|
|
|
"""Types 's' into the terminal."""
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
def send_to_vim(self, s):
|
|
|
|
|
"""Types 's' into the vim instance under test."""
|
2014-07-19 11:45:44 -04:00
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
def launch(self, config=[]):
|
2015-07-18 06:49:23 -04:00
|
|
|
|
"""Returns the python version in Vim as a string, e.g. '2.7'"""
|
2015-01-20 15:26:03 -05:00
|
|
|
|
pid_file = self.name_temp('vim.pid')
|
|
|
|
|
done_file = self.name_temp('loading_done')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
if os.path.exists(done_file):
|
|
|
|
|
os.remove(done_file)
|
|
|
|
|
|
|
|
|
|
post_config = []
|
2015-01-20 15:26:03 -05:00
|
|
|
|
post_config.append('%s << EOF' % ('py3' if PYTHON3 else 'py'))
|
2015-07-18 06:49:23 -04:00
|
|
|
|
post_config.append('import vim, sys')
|
2015-01-20 15:26:03 -05:00
|
|
|
|
post_config.append(
|
|
|
|
|
"with open('%s', 'w') as pid_file: pid_file.write(vim.eval('getpid()'))" %
|
|
|
|
|
pid_file)
|
2015-07-18 06:49:23 -04:00
|
|
|
|
post_config.append("with open('%s', 'w') as done_file:" % done_file)
|
|
|
|
|
post_config.append(" done_file.write('%i.%i.%i' % sys.version_info[:3])")
|
2015-01-20 15:26:03 -05:00
|
|
|
|
post_config.append('EOF')
|
|
|
|
|
|
|
|
|
|
config_path = self.write_temp('vim_config.vim',
|
|
|
|
|
textwrap.dedent(os.linesep.join(config + post_config) + '\n'))
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
2015-07-14 15:58:30 -04:00
|
|
|
|
# Note the space to exclude it from shell history. Also we always set
|
2015-07-18 06:49:23 -04:00
|
|
|
|
# NVIM_LISTEN_ADDRESS, even when running vanilla Vim, because it will
|
2015-07-14 15:58:30 -04:00
|
|
|
|
# just not care.
|
|
|
|
|
self.send_to_terminal(""" NVIM_LISTEN_ADDRESS=/tmp/nvim %s -u %s\r\n""" % (
|
|
|
|
|
self._vim_executable, config_path))
|
2014-07-19 11:45:44 -04:00
|
|
|
|
wait_until_file_exists(done_file)
|
2015-07-18 06:49:23 -04:00
|
|
|
|
self._vim_pid = int(read_text_file(pid_file))
|
|
|
|
|
return read_text_file(done_file).strip()
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
|
|
def leave_with_wait(self):
|
2015-07-14 15:58:30 -04:00
|
|
|
|
self.send_to_vim(3 * ESC + ':qa!\n')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
while is_process_running(self._vim_pid):
|
2015-07-15 02:03:05 -04:00
|
|
|
|
time.sleep(.2)
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VimInterfaceTmux(VimInterface):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
|
2015-07-03 15:52:50 -04:00
|
|
|
|
def __init__(self, vim_executable, session):
|
|
|
|
|
VimInterface.__init__(self, vim_executable, 'Tmux')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
self.session = session
|
|
|
|
|
self._check_version()
|
|
|
|
|
|
2015-07-14 15:58:30 -04:00
|
|
|
|
def _send(self, s):
|
2014-07-19 11:45:44 -04:00
|
|
|
|
# I did not find any documentation on what needs escaping when sending
|
|
|
|
|
# to tmux, but it seems like this is all that is needed for now.
|
|
|
|
|
s = s.replace(';', r'\;')
|
|
|
|
|
|
2018-03-30 16:02:51 -04:00
|
|
|
|
if len(s) == 1:
|
|
|
|
|
silent_call(['tmux', 'send-keys', '-t', self.session, hex(ord(s))])
|
|
|
|
|
else:
|
|
|
|
|
silent_call(['tmux', 'send-keys', '-t', self.session, '-l', s])
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
2015-07-14 15:58:30 -04:00
|
|
|
|
def send_to_terminal(self, s):
|
|
|
|
|
return self._send(s)
|
|
|
|
|
|
|
|
|
|
def send_to_vim(self, s):
|
|
|
|
|
return self._send(s)
|
|
|
|
|
|
2014-07-19 11:45:44 -04:00
|
|
|
|
def _check_version(self):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
stdout, _ = subprocess.Popen(['tmux', '-V'],
|
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
|
2014-07-19 11:45:44 -04:00
|
|
|
|
if PYTHON3:
|
2015-01-20 15:26:03 -05:00
|
|
|
|
stdout = stdout.decode('utf-8')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
m = re.match(r"tmux (\d+).(\d+)", stdout)
|
|
|
|
|
if not m or not (int(m.group(1)), int(m.group(2))) >= (1, 8):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
raise RuntimeError(
|
|
|
|
|
'Need at least tmux 1.8, you have %s.' %
|
|
|
|
|
stdout.strip())
|
|
|
|
|
|
2015-07-14 15:58:30 -04:00
|
|
|
|
class VimInterfaceTmuxNeovim(VimInterfaceTmux):
|
|
|
|
|
|
|
|
|
|
def __init__(self, vim_executable, session):
|
|
|
|
|
VimInterfaceTmux.__init__(self, vim_executable, session)
|
|
|
|
|
self._nvim = None
|
|
|
|
|
|
|
|
|
|
def send_to_vim(self, s):
|
|
|
|
|
if s == ARR_L:
|
|
|
|
|
s = "<Left>"
|
|
|
|
|
elif s == ARR_R:
|
|
|
|
|
s = "<Right>"
|
|
|
|
|
elif s == ARR_U:
|
|
|
|
|
s = "<Up>"
|
|
|
|
|
elif s == ARR_D:
|
|
|
|
|
s = "<Down>"
|
|
|
|
|
elif s == BS:
|
|
|
|
|
s = "<bs>"
|
|
|
|
|
elif s == ESC:
|
|
|
|
|
s = "<esc>"
|
|
|
|
|
elif s == "<":
|
|
|
|
|
s = "<lt>"
|
|
|
|
|
self._nvim.input(s)
|
|
|
|
|
|
|
|
|
|
def launch(self, config=[]):
|
|
|
|
|
import neovim
|
2015-07-18 06:49:23 -04:00
|
|
|
|
rv = VimInterfaceTmux.launch(self, config)
|
2015-07-14 15:58:30 -04:00
|
|
|
|
self._nvim = neovim.attach('socket', path='/tmp/nvim')
|
2015-07-18 06:49:23 -04:00
|
|
|
|
return rv
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
|
|
class VimInterfaceWindows(VimInterface):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
BRACES = re.compile('([}{])')
|
|
|
|
|
WIN_ESCAPES = ['+', '^', '%', '~', '[', ']', '<', '>', '(', ')']
|
2014-07-19 11:45:44 -04:00
|
|
|
|
WIN_REPLACES = [
|
2015-01-20 15:26:03 -05:00
|
|
|
|
(BS, '{BS}'),
|
|
|
|
|
(ARR_L, '{LEFT}'),
|
|
|
|
|
(ARR_R, '{RIGHT}'),
|
|
|
|
|
(ARR_U, '{UP}'),
|
|
|
|
|
(ARR_D, '{DOWN}'),
|
|
|
|
|
('\t', '{TAB}'),
|
|
|
|
|
('\n', '~'),
|
|
|
|
|
(ESC, '{ESC}'),
|
|
|
|
|
|
|
|
|
|
# On my system ` waits for a second keystroke, so `+SPACE = "`". On
|
|
|
|
|
# most systems, `+Space = "` ". I work around this, by sending the host
|
|
|
|
|
# ` as `+_+BS. Awkward, but the only way I found to get this working.
|
|
|
|
|
('`', '`_{BS}'),
|
|
|
|
|
('´', '´_{BS}'),
|
|
|
|
|
('{^}', '{^}_{BS}'),
|
2014-07-19 11:45:44 -04:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
# import windows specific modules
|
2015-01-20 15:26:03 -05:00
|
|
|
|
import win32com.client
|
|
|
|
|
import win32gui
|
2014-07-19 11:45:44 -04:00
|
|
|
|
self.win32gui = win32gui
|
2015-01-20 15:26:03 -05:00
|
|
|
|
self.shell = win32com.client.Dispatch('WScript.Shell')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
|
|
def is_focused(self, title=None):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
cur_title = self.win32gui.GetWindowText(
|
|
|
|
|
self.win32gui.GetForegroundWindow())
|
|
|
|
|
if (title or '- GVIM') in cur_title:
|
2014-07-19 11:45:44 -04:00
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def focus(self, title=None):
|
2015-01-20 15:26:03 -05:00
|
|
|
|
if not self.shell.AppActivate(title or '- GVIM'):
|
|
|
|
|
raise Exception('Failed to switch to GVim window')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
def convert_keys(self, keys):
|
|
|
|
|
keys = self.BRACES.sub(r"{\1}", keys)
|
|
|
|
|
for k in self.WIN_ESCAPES:
|
2015-01-20 15:26:03 -05:00
|
|
|
|
keys = keys.replace(k, '{%s}' % k)
|
2014-07-19 11:45:44 -04:00
|
|
|
|
for f, r in self.WIN_REPLACES:
|
|
|
|
|
keys = keys.replace(f, r)
|
|
|
|
|
return keys
|
|
|
|
|
|
|
|
|
|
def send(self, keys):
|
2015-07-03 15:52:50 -04:00
|
|
|
|
keys = self.convert_keys(keys)
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
|
|
|
|
if not self.is_focused():
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
self.focus()
|
|
|
|
|
if not self.is_focused():
|
|
|
|
|
# This is the only way I can find to stop test execution
|
2015-01-20 15:26:03 -05:00
|
|
|
|
raise KeyboardInterrupt('Failed to focus GVIM')
|
2014-07-19 11:45:44 -04:00
|
|
|
|
|
2015-07-03 15:52:50 -04:00
|
|
|
|
self.shell.SendKeys(keys)
|