UltiSnips/test/vim_test_case.py
Holger Rapp 306f0ace5f Add a testing interface that works for Neovim.
- Remove support for python 3.2 to reduce number of test cases and because
  it actually fails with Neovim. It is not a supported version anyways.
- Due to Neovim not handling fast typing through the console properly
  (https://github.com/neovim/neovim/issues/2454), the typing is actually
  simulated through the Python client. We need to differentiate now if a
  keystroke is meant for the terminal or for the Vim session. Using
  neovim.input() introduces additional chances for races since inputs
  are not buffered but processed right away. This results in more
  retries for some tests.
- Neovim needs more parameters and configuration passed in through the
  test script. Added command line arguments for these.
- Skip an extra test under Neovim due to
  https://github.com/neovim/python-client/issues/128.
2015-07-14 21:58:30 +02:00

204 lines
7.3 KiB
Python

# encoding: utf-8
# pylint: skip-file
import os
import subprocess
import tempfile
import textwrap
import time
import unittest
from test.constant import PYTHON3, SEQUENCES
from test.vim_interface import create_directory, TempFileManager
def plugin_cache_dir():
"""The directory that we check out our bundles to."""
return os.path.join(tempfile.gettempdir(), 'UltiSnips_test_vim_plugins')
class VimTestCase(unittest.TestCase, TempFileManager):
snippets = ()
files = {}
text_before = ' --- some text before --- \n\n'
text_after = '\n\n --- some text after --- '
expected_error = ''
wanted = ''
keys = ''
sleeptime = 0.00
output = ''
plugins = []
# Skip this test for the given reason or None for not skipping it.
skip_if = lambda self: None
version = None # Will be set to vim --version output
maxDiff = None # Show all diff output, always.
vim_flavor = None # will be 'vim' or 'neovim'.
def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
TempFileManager.__init__(self, 'Case')
def runTest(self):
# Only checks the output. All work is done in setUp().
wanted = self.text_before + self.wanted + self.text_after
if self.expected_error:
self.assertRegexpMatches(self.output, self.expected_error)
return
for i in range(self.retries):
if self.output != wanted:
# Redo this, but slower
self.sleeptime += 0.02
self.tearDown()
self.setUp()
self.assertEqual(self.output, wanted)
def _extra_vim_config(self, vim_config):
"""Adds extra lines to the vim_config list."""
def _before_test(self):
"""Send these keys before the test runs.
Used for buffer local variables and other options.
"""
def _create_file(self, file_path, content):
"""Creates a file in the runtimepath that is created for this test.
Returns the absolute path to the file.
"""
return self.write_temp(file_path, textwrap.dedent(content + '\n'))
def _link_file(self, source, relative_destination):
"""Creates a link from 'source' to the 'relative_destination' in our
temp dir."""
absdir = self.name_temp(relative_destination)
create_directory(absdir)
os.symlink(source, os.path.join(absdir, os.path.basename(source)))
def setUp(self):
# TODO(sirver): this uses 'vim', but must use --vim from the commandline.
if not VimTestCase.version:
VimTestCase.version, _ = subprocess.Popen(['vim', '--version'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
if PYTHON3:
VimTestCase.version = VimTestCase.version.decode('utf-8')
if self.plugins and not self.test_plugins:
return self.skipTest('Not testing integration with other plugins.')
reason_for_skipping = self.skip_if()
if reason_for_skipping is not None:
return self.skipTest(reason_for_skipping)
vim_config = []
vim_config.append('set nocompatible')
vim_config.append('set runtimepath=$VIMRUNTIME,%s,%s' % (
os.path.dirname(os.path.dirname(__file__)), self._temp_dir))
if self.plugins:
self._link_file(
os.path.join(
plugin_cache_dir(),
'vim-pathogen',
'autoload'),
'.')
for plugin in self.plugins:
self._link_file(
os.path.join(
plugin_cache_dir(),
os.path.basename(plugin)),
'bundle')
vim_config.append('execute pathogen#infect()')
# Some configurations are unnecessary for vanilla Vim, but Neovim
# defines some defaults differently.
vim_config.append('syntax on')
vim_config.append('filetype plugin indent on')
vim_config.append('set nosmarttab')
vim_config.append('set noautoindent')
vim_config.append('set backspace=""')
vim_config.append('set clipboard=""')
vim_config.append('set encoding=utf-8')
vim_config.append('set fileencoding=utf-8')
vim_config.append('set buftype=nofile')
vim_config.append('set shortmess=at')
vim_config.append('let @" = ""')
vim_config.append('let g:UltiSnipsExpandTrigger="<tab>"')
vim_config.append('let g:UltiSnipsJumpForwardTrigger="?"')
vim_config.append('let g:UltiSnipsJumpBackwardTrigger="+"')
vim_config.append('let g:UltiSnipsListSnippets="@"')
vim_config.append(
'let g:UltiSnipsUsePythonVersion="%i"' %
(3 if PYTHON3 else 2))
vim_config.append('let g:UltiSnipsSnippetDirectories=["us"]')
if self.python_host_prog:
vim_config.append('let g:python_host_prog="%s"' % self.python_host_prog)
if self.python3_host_prog:
vim_config.append('let g:python3_host_prog="%s"' % self.python3_host_prog)
self._extra_vim_config(vim_config)
# Finally, add the snippets and some configuration for the test.
vim_config.append('%s << EOF' % ('py3' if PYTHON3 else 'py'))
vim_config.append('from UltiSnips import UltiSnips_Manager\n')
if len(self.snippets) and not isinstance(self.snippets[0], tuple):
self.snippets = (self.snippets, )
for s in self.snippets:
sv, content = s[:2]
description = ''
options = ''
priority = 0
if len(s) > 2:
description = s[2]
if len(s) > 3:
options = s[3]
if len(s) > 4:
priority = s[4]
vim_config.append('UltiSnips_Manager.add_snippet(%r, %r, %r, %r, priority=%i)' % (
sv, content, description, options, priority))
# fill buffer with default text and place cursor in between.
prefilled_text = (self.text_before + self.text_after).splitlines()
vim_config.append('import vim\n')
vim_config.append('vim.current.buffer[:] = %r\n' % prefilled_text)
vim_config.append(
'vim.current.window.cursor = (max(len(vim.current.buffer)//2, 1), 0)')
# End of python stuff.
vim_config.append('EOF')
for name, content in self.files.items():
self._create_file(name, content)
self.vim.launch(vim_config)
self._before_test()
if not self.interrupt:
# Go into insert mode and type the keys but leave Vim some time to
# react.
text = 'i' + self.keys
while text:
to_send = None
for seq in SEQUENCES:
if text.startswith(seq):
to_send = seq
break
to_send = to_send or text[0]
self.vim.send_to_vim(to_send)
time.sleep(self.sleeptime)
text = text[len(to_send):]
self.output = self.vim.get_buffer_data()
def tearDown(self):
if self.interrupt:
print('Working directory: %s' % (self._temp_dir))
return
self.vim.leave_with_wait()
self.clear_temp()
# vim:fileencoding=utf-8:foldmarker={{{#,#}}}: